From 10b79b527b8bf9ac3fbd546435bd323f6e770cd1 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 1 Jul 2019 01:40:37 +0800 Subject: [PATCH] =?UTF-8?q?TODO:=20=E5=8A=A8=E6=80=81=E6=B3=9B=E5=8C=96?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usergw/biz/filters/DubboFilter.java | 20 ++--- .../service/usergw/config/ConfigGateway.java | 80 +++++++++++-------- .../src/main/resources/gateway.yaml | 4 +- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/DubboFilter.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/DubboFilter.java index c197910..48b2736 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/DubboFilter.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/DubboFilter.java @@ -3,7 +3,6 @@ package cn.ecpark.service.usergw.biz.filters; import java.nio.ByteBuffer; import java.util.Map; - import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ProtocolConfig; import org.apache.dubbo.config.ReferenceConfig; @@ -20,12 +19,6 @@ import com.alibaba.fastjson.*; import reactor.core.publisher.Mono; - - - - - - @Component public class DubboFilter implements GlobalFilter, Ordered { @@ -37,8 +30,7 @@ public class DubboFilter implements GlobalFilter, Ordered { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { exchange.getRequest(); - ReferenceConfig reference = new - ReferenceConfig(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存 + ReferenceConfig reference = new ReferenceConfig(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存 reference.setApplication(new ApplicationConfig("dubbo-exchange")); // reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181")); @@ -48,16 +40,14 @@ public class DubboFilter implements GlobalFilter, Ordered { ReferenceConfigCache cache = ReferenceConfigCache.getCache(); GenericService gs = cache.get(reference); - - - - Object result = gs.$invoke("Hello", new String[]{}, new Object[]{}); + Object result = gs.$invoke("Hello", new String[] {}, new Object[] {}); if (result != null) { ServerHttpResponse response = exchange.getResponse(); - return response.writeWith( Mono.just(response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes())))); + return response.writeWith( + Mono.just(response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes())))); } return chain.filter(exchange); } - + } \ No newline at end of file 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 274beb5..443cd1b 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 @@ -45,6 +45,7 @@ public class ConfigGateway implements RouteDefinitionLocator { private Http2Dubbo http2Dubbo; @Override + @SuppressWarnings("unchecked") public Flux getRouteDefinitions() { // WebFluxConfigurationSupport a; @@ -113,44 +114,18 @@ public class ConfigGateway implements RouteDefinitionLocator { Object unknownRoutes = configDefault.get("routes"); if (unknownRoutes != null) { - List routes = (ArrayList) unknownRoutes; - for (LinkedHashMap iter : routes) { + List>> routes = (ArrayList>>) unknownRoutes; + for (LinkedHashMap> iter : routes) { RouteDefinition rd = new RouteDefinition(); - // 设置id - Object id = iter.get("id"); - if (id != null) { - rd.setId((String) id); - } - - // 设置uri - Object uri = iter.get("uri"); - if (uri != null) { - rd.setUri(URI.create((String) uri)); - } - - // 设置uri - Object order = iter.get("order"); - if (order != null) { - rd.setOrder((int) order); - } + + // 设置基础属性 + this.ParseAndSetBase(rd, iter); // predicates: 下的相关属性 - List unknownPredicatesYaml = (ArrayList) iter.get("predicates"); - if (unknownPredicatesYaml != null) { - List predicatesYaml = (ArrayList) unknownPredicatesYaml; - for (String predicateString : predicatesYaml) { - predicates.add(new PredicateDefinition(predicateString)); - } - } + this.ParseAndAddPredicates(predicates, iter, "predicates"); // filters: 下的相关属性 - List unknownFiltersYaml = (ArrayList) iter.get("filters"); - if (unknownFiltersYaml != null) { - List filtersYaml = (ArrayList) unknownFiltersYaml; - for (String filterString : filtersYaml) { - filters.add(new FilterDefinition(filterString)); - } - } + this.ParseAndAddFilters(filters, iter, "filters"); rd.setPredicates(predicates); rd.setFilters(filters); @@ -165,6 +140,45 @@ public class ConfigGateway implements RouteDefinitionLocator { return null; } + private void ParseAndSetBase(RouteDefinition rd, LinkedHashMap> iter) { + // 设置id + Object id = iter.get("id"); + if (id != null) { + rd.setId((String) id); + } + + // 设置uri + Object uri = iter.get("uri"); + if (uri != null) { + rd.setUri(URI.create((String) uri)); + } + + // 设置uri + Object order = iter.get("order"); + if (order != null) { + rd.setOrder((int) order); + } + } + + private void ParseAndAddPredicates(List predicates, LinkedHashMap> iter, String yamlField) { + List predicatesYaml = iter.get(yamlField); + if (predicatesYaml != null) { + for (String predicateString : predicatesYaml) { + predicates.add(new PredicateDefinition(predicateString)); + } + } + } + + private void ParseAndAddFilters(List filters, LinkedHashMap> iter, String yamlField) { + List filtersYaml = iter.get(yamlField); + if (filtersYaml != null) { + for (String filterString : filtersYaml) { + filters.add(new FilterDefinition(filterString)); + } + } + } + + private void createHttp2Dubbo(Map configDubbo) { try { diff --git a/usergw-service/src/main/resources/gateway.yaml b/usergw-service/src/main/resources/gateway.yaml index 54d6615..1192c8d 100644 --- a/usergw-service/src/main/resources/gateway.yaml +++ b/usergw-service/src/main/resources/gateway.yaml @@ -18,6 +18,6 @@ dubbo: routes: - id: test path: /dubbo/hello - interface: com.xxx.xxx - version: 2.0.0 + interface: ocean.demo.api.IExchange + version: 1.0.0 \ No newline at end of file