diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java index db706d3..bc8488d 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java @@ -1,6 +1,7 @@ package cn.ecpark.service.usergw.config; import java.io.InputStream; +import java.lang.annotation.Documented; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; @@ -42,9 +43,9 @@ public class ConfigGateway implements RouteDefinitionLocator { @Value("${yame.config}") private String yameConfigPath; - List defaultFilters = new ArrayList<>(); - HashMap, Object>> specialField = new HashMap, Object>>(); - Set ignoreKey = new HashSet<>(); + private List defaultFilters = new ArrayList<>(); + private HashMap, Object>> specialField = new HashMap, Object>>(); + private Set ignoreKey = new HashSet<>(); @Autowired private ApplicationContext appContext; @@ -82,14 +83,15 @@ public class ConfigGateway implements RouteDefinitionLocator { Map configYaml = new Yaml().load((InputStream) inputStream); if (configYaml != null) { - Map defaultYaml = (Map) configYaml.get("restful"); + Map defaultFiltersYaml = (Map) configYaml.get("default-filters"); + Map restfulYaml = (Map) configYaml.get("restful"); Map dubboYaml = (Map) configYaml.get("dubbo"); List routeList = new ArrayList(); - if (defaultYaml != null && !defaultYaml.isEmpty()) { - this.getDefaultFilter(defaultFilters, defaultYaml); - this.configDefault(routeList, defaultYaml); + if (restfulYaml != null && !restfulYaml.isEmpty()) { + this.getDefaultFilter(defaultFilters, defaultFiltersYaml); + this.configRestful(routeList, restfulYaml); } if (dubboYaml != null && !dubboYaml.isEmpty()) { @@ -106,11 +108,16 @@ public class ConfigGateway implements RouteDefinitionLocator { return Flux.empty(); } + /** + * + * @param routeList 路由列表 + * @param restfulYaml 配置Yaml.restful对象 + */ @SuppressWarnings("unchecked") - private void configDefault(List routeList, Map defaultYaml) { - if (defaultYaml != null) { + private void configRestful(List routeList, Map restfulYaml) { + if (restfulYaml != null) { - Object unknownRoutes = defaultYaml.get("routes"); + Object unknownRoutes = restfulYaml.get("routes"); if (unknownRoutes != null) { List>> routes = (ArrayList>>) unknownRoutes; for (LinkedHashMap> iter : routes) { @@ -137,6 +144,11 @@ public class ConfigGateway implements RouteDefinitionLocator { } } + /** + * + * @param routeList 路由列表 + * @param configDubbo 配置Yaml.dubbo对象 + */ @SuppressWarnings("unchecked") private void configHttp2Dubbo(List routeList, Map configDubbo) { @@ -180,6 +192,11 @@ public class ConfigGateway implements RouteDefinitionLocator { } + /** + * + * @param filters {@link #defaultFilters} 全局的默认过滤列表, 可以和Spring Cloud 默认配置一起生效 + * @param defaultYaml 配置Yaml.default-filters对象 + */ @SuppressWarnings("unchecked") private void getDefaultFilter(List filters, Map defaultYaml) { // default-filters: 下的相关设置 @@ -193,6 +210,11 @@ public class ConfigGateway implements RouteDefinitionLocator { } } + /*** + * + * @param rd 路由列表 + * @param iter Yaml.restful.routes + */ private void parseAndSetBase(RouteDefinition rd, LinkedHashMap> iter) { // 设置id Object id = iter.get("id"); diff --git a/usergw-service/src/main/resources/gateway.yaml b/usergw-service/src/main/resources/gateway.yaml index d5202b9..6ab62d4 100644 --- a/usergw-service/src/main/resources/gateway.yaml +++ b/usergw-service/src/main/resources/gateway.yaml @@ -1,6 +1,8 @@ + +default-filters: + - AddResponseHeader=X-Response-Default-Foo, Default-Bar + restful: - default-filters: - - AddResponseHeader=X-Response-Default-Foo, Default-Bar routes: - id: path_route uri: http://httpbin.org:80/* diff --git a/usergw-service/src/main/resources/test-gateway.yaml b/usergw-service/src/main/resources/test-gateway.yaml index a09c78c..ff360e8 100644 --- a/usergw-service/src/main/resources/test-gateway.yaml +++ b/usergw-service/src/main/resources/test-gateway.yaml @@ -1,6 +1,7 @@ +default-filters: + - AddResponseHeader=X-Response-Default-Foo, Default-Bar + restful: - default-filters: - - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar routes: - id: path_route uri: http://httpbin.org:80/* diff --git a/usergw-service/src/test/java/resources/gateway.yaml b/usergw-service/src/test/java/resources/gateway.yaml index a09c78c..7e9c5b4 100644 --- a/usergw-service/src/test/java/resources/gateway.yaml +++ b/usergw-service/src/test/java/resources/gateway.yaml @@ -1,6 +1,7 @@ +default-filters: + - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar + restful: - default-filters: - - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar routes: - id: path_route uri: http://httpbin.org:80/* diff --git a/usergw-service/src/test/java/resources/test-gateway.yaml b/usergw-service/src/test/java/resources/test-gateway.yaml index 5549add..39bd62a 100644 --- a/usergw-service/src/test/java/resources/test-gateway.yaml +++ b/usergw-service/src/test/java/resources/test-gateway.yaml @@ -1,6 +1,7 @@ +default-filters: + - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar + restful: - default-filters: - - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar routes: - id: path_route uri: http://httpbin.org:80/*