添加了大量注释, 并通过测试.

This commit is contained in:
eson 2019-07-10 02:52:52 +08:00
parent e26d0c17a6
commit d62a9aee6f
2 changed files with 71 additions and 17 deletions

View File

@ -1,7 +1,6 @@
package cn.ecpark.service.usergw.config; package cn.ecpark.service.usergw.config;
import java.io.InputStream; import java.io.InputStream;
import java.lang.annotation.Documented;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -47,9 +46,15 @@ public class ConfigGateway implements RouteDefinitionLocator {
private HashMap<String, BiConsumer<ReferenceConfig<GenericService>, Object>> specialField = new HashMap<String, BiConsumer<ReferenceConfig<GenericService>, Object>>(); private HashMap<String, BiConsumer<ReferenceConfig<GenericService>, Object>> specialField = new HashMap<String, BiConsumer<ReferenceConfig<GenericService>, Object>>();
private Set<String> ignoreKey = new HashSet<>(); private Set<String> ignoreKey = new HashSet<>();
/**
* spring boot 上下文
*/
@Autowired @Autowired
private ApplicationContext appContext; private ApplicationContext appContext;
/**
* ConfigGateway 构造函数
*/
public ConfigGateway() { public ConfigGateway() {
specialField.put("application", ConfigSpecialFunction::setApplication); specialField.put("application", ConfigSpecialFunction::setApplication);
specialField.put("registry", ConfigSpecialFunction::setRegistry); specialField.put("registry", ConfigSpecialFunction::setRegistry);
@ -212,7 +217,6 @@ public class ConfigGateway implements RouteDefinitionLocator {
* 默认配置一起生效 * 默认配置一起生效
* @param defaultYaml 配置Yaml.default-filters对象 * @param defaultYaml 配置Yaml.default-filters对象
*/ */
@SuppressWarnings("unchecked")
private void getDefaultFilter(List<FilterDefinition> filters, List<String> defaultFiltersYaml) { private void getDefaultFilter(List<FilterDefinition> filters, List<String> defaultFiltersYaml) {
// default-filters: 下的相关设置 // default-filters: 下的相关设置
if (defaultFiltersYaml != null) { 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") @SuppressWarnings("unchecked")
private String parseDubboUriAndSetBase(RouteDefinition rd, LinkedHashMap<String, List<String>> iter) { private String parseDubboUriAndSetBase(RouteDefinition rd, LinkedHashMap<String, List<String>> iter) {
// 设置id // 设置id
@ -282,7 +292,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
String UriString = "dubbo://"; String UriString = "dubbo://";
// TODO: 以后可以完善dubbo-{flag}形式的判断 // TODO: 以后可以完善dubbo-flag形式的判断
Object methods = iter.get("methods"); Object methods = iter.get("methods");
if (methods == null || ((List<Object>) methods).size() == 0) { if (methods == null || ((List<Object>) methods).size() == 0) {
UriString = "dubbo-allowed://"; UriString = "dubbo-allowed://";
@ -332,6 +342,12 @@ public class ConfigGateway implements RouteDefinitionLocator {
return UriString; return UriString;
} }
/**
*
* @param predicates List<{@link PredicateDefinition}> 断言列表
* @param iter restful.routes.predicates
* @param yamlField 根据域的key 获取 predicates 字符串列
*/
private void parseAndAddPredicates(List<PredicateDefinition> predicates, LinkedHashMap<String, List<String>> iter, private void parseAndAddPredicates(List<PredicateDefinition> predicates, LinkedHashMap<String, List<String>> iter,
String yamlField) { String yamlField) {
List<String> predicatesYaml = iter.get(yamlField); List<String> 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<FilterDefinition> filters, LinkedHashMap<String, List<String>> iter, private void parseAndAddFilters(List<FilterDefinition> filters, LinkedHashMap<String, List<String>> iter,
String yamlField) { String yamlField) {
List<String> filtersYaml = iter.get(yamlField); List<String> 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<FilterDefinition> filters, private void parseAndAddDubboFilters(String dubboUri, List<FilterDefinition> filters,
LinkedHashMap<String, List<String>> iter, String yamlField) { LinkedHashMap<String, List<String>> iter, String yamlField) {
List<String> filtersYaml = iter.get(yamlField); List<String> filtersYaml = iter.get(yamlField);

View File

@ -20,35 +20,60 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class ConfigSpecialFunction { public class ConfigSpecialFunction {
public static void setDefault(ReferenceConfig<GenericService> 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<GenericService> ref, String methodName, Object params)
throws Exception { throws Exception {
methodName = "set" + Convert.firstUpperCase(methodName); methodName = "set" + Convert.firstUpperCase(methodName);
log.info("method:{}, value:{}", methodName, cfgValue); log.info("method:{}, value:{}", methodName, params);
Method method = ref.getClass().getMethod(methodName, cfgValue.getClass()); Method method = ref.getClass().getMethod(methodName, params.getClass());
method.invoke(ref, cfgValue); method.invoke(ref, params);
} }
public static void setApplication(ReferenceConfig<GenericService> 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<GenericService> ref, Object params) {
ref.setApplication(new ApplicationConfig((String) params));
} }
public static void setRegistry(ReferenceConfig<GenericService> 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<GenericService> ref, Object params) {
ref.setRegistry(new RegistryConfig((String) params));
} }
/**
* 把设置method域的一些 函数名 参数类型 缓存起来. 没设置的运行通过
* @param ref {@link ReferenceConfig} {@link GenericService} 泛调用的接口
* @param params 传递参数 {@link ReferenceConfig#setApplication(ApplicationConfig)}
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void setMethods(ReferenceConfig<GenericService> ref, Object cfgValue) { public static void setMethods(ReferenceConfig<GenericService> ref, Object params) {
List<HashMap<String, Object>> cfgs = (List<HashMap<String, Object>>) cfgValue; List<HashMap<String, Object>> cfgs = (List<HashMap<String, Object>>) params;
for (HashMap<String, Object> cfg : cfgs) { for (HashMap<String, Object> cfg : cfgs) {
String name = (String) cfg.get("name"); String name = (String) cfg.get("name");
if (DubboGatewayFilterFactory.mehtods.containsKey(name)) { if (DubboGatewayFilterFactory.mehtods.containsKey(name)) {
log.warn("method {} is duplicate", name); log.warn("method {} is duplicate", name);
} }
List<String> params = (List<String>) cfg.get("param-types"); List<String> paramTypes = (List<String>) cfg.get("param-types");
if (params == null) { if (paramTypes == null) {
params = new LinkedList<>(); // 不用ArrayList 减少内存的使用. paramTypes = new LinkedList<>(); // 不用ArrayList 减少内存的使用.
} }
DubboGatewayFilterFactory.mehtods.put(name, params); DubboGatewayFilterFactory.mehtods.put(name, paramTypes);
} }
} }