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
*/
@Configuration
@Component
// @Configuration
// @Component
public class Route {
@Bean
public RouterFunction<ServerResponse> Predicates() {
RouterFunction<ServerResponse> route = RouterFunctions.route(
RequestPredicates.path("/test/hello"),
request -> ServerResponse.ok().body(BodyInserters.fromObject("hello")));
return route;
}
// @Bean
// public RouterFunction<ServerResponse> Predicates() {
// RouterFunction<ServerResponse> route = RouterFunctions.route(
// RequestPredicates.path("/test/hello"),
// request -> ServerResponse.ok().body(BodyInserters.fromObject("hello")));
// return route;
// }
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes().build();
}
// @Bean
// public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
// 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.rpc.service.GenericService;
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.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
@ -38,7 +39,6 @@ import reactor.core.publisher.Flux;
* ConfigGateway
*/
@Component
@Controller
@Slf4j
public class ConfigGateway implements RouteDefinitionLocator {
@ -51,20 +51,10 @@ public class ConfigGateway implements RouteDefinitionLocator {
@Autowired
private ApplicationContext appContext;
@Bean
public GenericServicePool genericServicePool() {
return new GenericServicePool();
}
@Bean
public DubboGatewayFilterFactory dubboFilterFactory() {
return new DubboGatewayFilterFactory();
}
public ConfigGateway() {
specialField.put("application", ConfigSpecialFunction::setApplication);
specialField.put("registry", ConfigSpecialFunction::setRegistry);
ignoreKey.add("predicates");
ignoreKey.add("filters");
}
@ -74,6 +64,9 @@ public class ConfigGateway implements RouteDefinitionLocator {
public Flux<RouteDefinition> getRouteDefinitions() {
// WebFluxConfigurationSupport a;
Object inputStream = null;
String[] gatewayConfigPathList = { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
for (String gatewayConfigPath : gatewayConfigPathList) {
@ -82,7 +75,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
break;
}
}
if (inputStream != null) {
Map<String, Object> configYaml = new Yaml().load((InputStream) inputStream);
if (configYaml != null) {
@ -115,6 +108,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
this.configHttp2Dubbo(routeList, dubboYaml);
}
if (!routeList.isEmpty()) {
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,
String yamlField) {
List<String> filtersYaml = iter.get(yamlField);
filters.addAll(defaultFilters);
if (filtersYaml != null) {
filters.addAll(defaultFilters);
for (String filterString : filtersYaml) {
FilterDefinition fd = new FilterDefinition(filterString);
log.info(fd.getName());
@ -347,8 +341,8 @@ public class ConfigGateway implements RouteDefinitionLocator {
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 {

View File

@ -10,7 +10,11 @@ import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
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 lombok.extern.slf4j.Slf4j;
@ -20,6 +24,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ConfigSpecialFunction {
// public static BiConsumer<ReferenceConfig<GenericService>, String>
// setApplication = (ref, 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:
- Path=/get
- 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
# uri: http://localhost/test/**
# order: 11
@ -20,12 +34,12 @@ dubbo:
- id: test
order: 10
application: dubbo-exchange
# method:
connections: 4
group: test
registry: zookeeper://127.0.0.1:2181
# registry: zookeeper://127.0.0.1:2181
interface: ocean.demo.api.IExchange
version: 1.0.0
predicates:
- Path=/dubbo/hello
filters:
- Dubbo=http213313