From 7c56c84758bf18dc8c18fe5e2d791372ba40da70 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Tue, 9 Jul 2019 18:50:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A,=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8E.=20=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=952=E4=B8=AA=E9=94=99=E8=AF=AF=20TODO:=20fix=202=20error?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/usergw/config/ConfigGateway.java | 42 ++++++++++++++----- .../src/main/resources/gateway.yaml | 6 ++- .../src/main/resources/test-gateway.yaml | 5 ++- .../src/test/java/resources/gateway.yaml | 5 ++- .../src/test/java/resources/test-gateway.yaml | 5 ++- 5 files changed, 45 insertions(+), 18 deletions(-) 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/*