RouteDefinitionLocator 执行两次

This commit is contained in:
huangsimin 2019-07-05 18:39:50 +08:00
parent 6b32e160a8
commit 57355309b7
5 changed files with 65 additions and 31 deletions

View File

@ -15,21 +15,21 @@ import org.springframework.web.reactive.function.server.ServerResponse;
/** /**
* Route * Route
*/ */
@Configuration // @Configuration
@Component // @Component
public class Route { public class Route {
@Bean // @Bean
public RouterFunction<ServerResponse> Predicates() { // public RouterFunction<ServerResponse> Predicates() {
RouterFunction<ServerResponse> route = RouterFunctions.route( // RouterFunction<ServerResponse> route = RouterFunctions.route(
RequestPredicates.path("/test/hello"), // RequestPredicates.path("/test/hello"),
request -> ServerResponse.ok().body(BodyInserters.fromObject("hello"))); // request -> ServerResponse.ok().body(BodyInserters.fromObject("hello")));
return route; // return route;
} // }
@Bean // @Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { // public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes().build(); // return builder.routes().build();
} // }
} }

View File

@ -16,6 +16,7 @@ import java.util.function.BiConsumer;
import org.apache.dubbo.config.ReferenceConfig; import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.filter.FilterDefinition; import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition; import org.springframework.cloud.gateway.route.RouteDefinition;
@ -38,7 +39,6 @@ import reactor.core.publisher.Flux;
* ConfigGateway * ConfigGateway
*/ */
@Component @Component
@Controller
@Slf4j @Slf4j
public class ConfigGateway implements RouteDefinitionLocator { public class ConfigGateway implements RouteDefinitionLocator {
@ -51,20 +51,10 @@ public class ConfigGateway implements RouteDefinitionLocator {
@Autowired @Autowired
private ApplicationContext appContext; private ApplicationContext appContext;
@Bean
public GenericServicePool genericServicePool() {
return new GenericServicePool();
}
@Bean
public DubboGatewayFilterFactory dubboFilterFactory() {
return new DubboGatewayFilterFactory();
}
public ConfigGateway() { public ConfigGateway() {
specialField.put("application", ConfigSpecialFunction::setApplication); specialField.put("application", ConfigSpecialFunction::setApplication);
specialField.put("registry", ConfigSpecialFunction::setRegistry); specialField.put("registry", ConfigSpecialFunction::setRegistry);
ignoreKey.add("predicates"); ignoreKey.add("predicates");
ignoreKey.add("filters"); ignoreKey.add("filters");
} }
@ -74,6 +64,9 @@ public class ConfigGateway implements RouteDefinitionLocator {
public Flux<RouteDefinition> getRouteDefinitions() { public Flux<RouteDefinition> getRouteDefinitions() {
// WebFluxConfigurationSupport a; // WebFluxConfigurationSupport a;
Object inputStream = null; Object inputStream = null;
String[] gatewayConfigPathList = { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" }; String[] gatewayConfigPathList = { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
for (String gatewayConfigPath : gatewayConfigPathList) { for (String gatewayConfigPath : gatewayConfigPathList) {
@ -82,7 +75,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
break; break;
} }
} }
if (inputStream != null) { if (inputStream != null) {
Map<String, Object> configYaml = new Yaml().load((InputStream) inputStream); Map<String, Object> configYaml = new Yaml().load((InputStream) inputStream);
if (configYaml != null) { if (configYaml != null) {
@ -115,6 +108,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
this.configHttp2Dubbo(routeList, dubboYaml); this.configHttp2Dubbo(routeList, dubboYaml);
} }
if (!routeList.isEmpty()) { if (!routeList.isEmpty()) {
return Flux.fromIterable(routeList); return Flux.fromIterable(routeList);
} }
@ -338,8 +332,8 @@ public class ConfigGateway implements RouteDefinitionLocator {
private void ParseAndAddDubboFilters(String dubboUri, List<FilterDefinition> filters, LinkedHashMap<String, List<String>> iter, private void ParseAndAddDubboFilters(String dubboUri, List<FilterDefinition> filters, LinkedHashMap<String, List<String>> iter,
String yamlField) { String yamlField) {
List<String> filtersYaml = iter.get(yamlField); List<String> filtersYaml = iter.get(yamlField);
filters.addAll(defaultFilters);
if (filtersYaml != null) { if (filtersYaml != null) {
filters.addAll(defaultFilters);
for (String filterString : filtersYaml) { for (String filterString : filtersYaml) {
FilterDefinition fd = new FilterDefinition(filterString); FilterDefinition fd = new FilterDefinition(filterString);
log.info(fd.getName()); log.info(fd.getName());
@ -347,8 +341,8 @@ public class ConfigGateway implements RouteDefinitionLocator {
filters.add(fd); filters.add(fd);
} }
} }
filters.add(new FilterDefinition("Dubbo=" + dubboUri));
} }
filters.add(new FilterDefinition("Dubbo=" + dubboUri));
} }
private static void callMethod(ReferenceConfig ref, String name, String params) throws Exception { private static void callMethod(ReferenceConfig ref, String name, String params) throws Exception {

View File

@ -10,7 +10,11 @@ import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig; import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
import cn.ecpark.service.usergw.biz.filters.factory.DubboGatewayFilterFactory;
import cn.ecpark.service.usergw.utils.Convert; import cn.ecpark.service.usergw.utils.Convert;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -20,6 +24,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class ConfigSpecialFunction { public class ConfigSpecialFunction {
// public static BiConsumer<ReferenceConfig<GenericService>, String> // public static BiConsumer<ReferenceConfig<GenericService>, String>
// setApplication = (ref, cfgValue) -> { // setApplication = (ref, cfgValue) -> {
// ref.setApplication(new ApplicationConfig(cfgValue)); // ref.setApplication(new ApplicationConfig(cfgValue));

View File

@ -0,0 +1,20 @@
package cn.ecpark.service.usergw.config.bean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
import cn.ecpark.service.usergw.biz.filters.factory.DubboGatewayFilterFactory;
@Component
public class ConfigBean {
@Bean
public GenericServicePool genericServicePool() {
return new GenericServicePool();
}
@Bean
public DubboGatewayFilterFactory dubboFilterFactory() {
return new DubboGatewayFilterFactory();
}
}

View File

@ -9,6 +9,20 @@ default:
predicates: predicates:
- Path=/get - Path=/get
- Header=XX, \d+ - Header=XX, \d+
- id: path_route12
uri: http://httpbin.org:80/*
order: 9
predicates:
- Path=/get
- Header=XX, \d+
- id: path_route13
uri: http://httpbin.org:80/*
order: 9
predicates:
- Path=/get
- Header=XX, \d+
# - id: redirect_to # - id: redirect_to
# uri: http://localhost/test/** # uri: http://localhost/test/**
# order: 11 # order: 11
@ -20,12 +34,12 @@ dubbo:
- id: test - id: test
order: 10 order: 10
application: dubbo-exchange application: dubbo-exchange
# method:
connections: 4
group: test group: test
registry: zookeeper://127.0.0.1:2181 # registry: zookeeper://127.0.0.1:2181
interface: ocean.demo.api.IExchange interface: ocean.demo.api.IExchange
version: 1.0.0 version: 1.0.0
predicates: predicates:
- Path=/dubbo/hello - Path=/dubbo/hello
filters:
- Dubbo=http213313