From d62a9aee6fe6f1d4a220b63fdd842ec52b475c71 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Wed, 10 Jul 2019 02:52:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=A4=A7=E9=87=8F?= =?UTF-8?q?=E6=B3=A8=E9=87=8A,=20=E5=B9=B6=E9=80=9A=E8=BF=87=E6=B5=8B?= =?UTF-8?q?=E8=AF=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/usergw/config/ConfigGateway.java | 35 ++++++++++-- .../usergw/config/ConfigSpecialFunction.java | 53 ++++++++++++++----- 2 files changed, 71 insertions(+), 17 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 a9a2cf5..b02004e 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,7 +1,6 @@ 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; @@ -47,9 +46,15 @@ public class ConfigGateway implements RouteDefinitionLocator { private HashMap, Object>> specialField = new HashMap, Object>>(); private Set ignoreKey = new HashSet<>(); + /** + * spring boot 上下文 + */ @Autowired private ApplicationContext appContext; + /** + * ConfigGateway 构造函数 + */ public ConfigGateway() { specialField.put("application", ConfigSpecialFunction::setApplication); specialField.put("registry", ConfigSpecialFunction::setRegistry); @@ -212,7 +217,6 @@ public class ConfigGateway implements RouteDefinitionLocator { * 默认配置一起生效 * @param defaultYaml 配置Yaml.default-filters对象 */ - @SuppressWarnings("unchecked") private void getDefaultFilter(List filters, List defaultFiltersYaml) { // default-filters: 下的相关设置 if (defaultFiltersYaml != null) { @@ -247,6 +251,12 @@ public class ConfigGateway implements RouteDefinitionLocator { } } + /** + * + * @param rd {@link RouteDefinition} + * @param iter Yaml.dubbo.routes + * @return dubboUri eg. dubbo://application/group/com.a.b:1.0.0 + */ @SuppressWarnings("unchecked") private String parseDubboUriAndSetBase(RouteDefinition rd, LinkedHashMap> iter) { // 设置id @@ -282,7 +292,7 @@ public class ConfigGateway implements RouteDefinitionLocator { String UriString = "dubbo://"; - // TODO: 以后可以完善dubbo-{flag}形式的判断 + // TODO: 以后可以完善dubbo-flag形式的判断 Object methods = iter.get("methods"); if (methods == null || ((List) methods).size() == 0) { UriString = "dubbo-allowed://"; @@ -332,6 +342,12 @@ public class ConfigGateway implements RouteDefinitionLocator { return UriString; } + /** + * + * @param predicates List<{@link PredicateDefinition}> 断言列表 + * @param iter restful.routes.predicates + * @param yamlField 根据域的key 获取 predicates 字符串列 + */ private void parseAndAddPredicates(List predicates, LinkedHashMap> iter, String yamlField) { List predicatesYaml = iter.get(yamlField); @@ -342,6 +358,12 @@ public class ConfigGateway implements RouteDefinitionLocator { } } + /** + * + * @param filters List<{@link FilterDefinition}> 过滤器列表 + * @param iter restful.routes.filters + * @param yamlField 根据域的key 获取 filters 字符串列 + */ private void parseAndAddFilters(List filters, LinkedHashMap> iter, String yamlField) { List filtersYaml = iter.get(yamlField); @@ -355,6 +377,13 @@ public class ConfigGateway implements RouteDefinitionLocator { } } + /** + * + * @param dubboUri 获取返回的字符串 {@link #parseDubboUriAndSetBase(RouteDefinition, LinkedHashMap)} + * @param filters List<{@link FilterDefinition}> 过滤器列表 + * @param iter dubbo.routes.filters + * @param yamlField 根据域的key 获取 filters 字符串列 + */ private void parseAndAddDubboFilters(String dubboUri, List filters, LinkedHashMap> iter, String yamlField) { List filtersYaml = iter.get(yamlField); diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigSpecialFunction.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigSpecialFunction.java index e94aa5a..2a73844 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigSpecialFunction.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigSpecialFunction.java @@ -20,35 +20,60 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class ConfigSpecialFunction { - public static void setDefault(ReferenceConfig ref, String methodName, Object cfgValue) + /** + * 默认调用ref的函数, 通过反射方法 + * @param ref {@link ReferenceConfig} {@link GenericService} 泛调用的接口 + * @param methodName 方法名 用于调用接口的方法 + * @param params 方法的参数 + * @throws IllegalAccessException + * @throws IllegalArgumentException + * @throws InvocationTargetException + * @throws Exception + */ + public static void setDefault(ReferenceConfig ref, String methodName, Object params) throws Exception { methodName = "set" + Convert.firstUpperCase(methodName); - log.info("method:{}, value:{}", methodName, cfgValue); - Method method = ref.getClass().getMethod(methodName, cfgValue.getClass()); - method.invoke(ref, cfgValue); + log.info("method:{}, value:{}", methodName, params); + Method method = ref.getClass().getMethod(methodName, params.getClass()); + method.invoke(ref, params); } - public static void setApplication(ReferenceConfig ref, Object cfgValue) { - ref.setApplication(new ApplicationConfig((String) cfgValue)); + /** + * 遇到application 做特殊的 函数处理 + * @param ref {@link ReferenceConfig} {@link GenericService} 泛调用的接口 + * @param params 传递参数 {@link ReferenceConfig#setApplication(ApplicationConfig)} + */ + public static void setApplication(ReferenceConfig ref, Object params) { + ref.setApplication(new ApplicationConfig((String) params)); } - public static void setRegistry(ReferenceConfig ref, Object cfgValue) { - ref.setRegistry(new RegistryConfig((String) cfgValue)); + /** + * 遇到registry 做特殊的 函数处理 + * @param ref {@link ReferenceConfig} {@link GenericService} 泛调用的接口 + * @param params 传递参数 {@link ReferenceConfig#setRegistry(RegistryConfig)} + */ + public static void setRegistry(ReferenceConfig ref, Object params) { + ref.setRegistry(new RegistryConfig((String) params)); } + /** + * 把设置method域的一些 函数名 参数类型 缓存起来. 没设置的运行通过 + * @param ref {@link ReferenceConfig} {@link GenericService} 泛调用的接口 + * @param params 传递参数 {@link ReferenceConfig#setApplication(ApplicationConfig)} + */ @SuppressWarnings("unchecked") - public static void setMethods(ReferenceConfig ref, Object cfgValue) { - List> cfgs = (List>) cfgValue; + public static void setMethods(ReferenceConfig ref, Object params) { + List> cfgs = (List>) params; for (HashMap cfg : cfgs) { String name = (String) cfg.get("name"); if (DubboGatewayFilterFactory.mehtods.containsKey(name)) { log.warn("method {} is duplicate", name); } - List params = (List) cfg.get("param-types"); - if (params == null) { - params = new LinkedList<>(); // 不用ArrayList 减少内存的使用. + List paramTypes = (List) cfg.get("param-types"); + if (paramTypes == null) { + paramTypes = new LinkedList<>(); // 不用ArrayList 减少内存的使用. } - DubboGatewayFilterFactory.mehtods.put(name, params); + DubboGatewayFilterFactory.mehtods.put(name, paramTypes); } }