TODO: 函数配置, 参数配置
This commit is contained in:
parent
342e18766e
commit
6b32e160a8
|
@ -50,42 +50,8 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
|
||||||
|
|
||||||
return (exchange, chain) -> {
|
return (exchange, chain) -> {
|
||||||
|
|
||||||
String application = "dubbo-exchange"; // 必须
|
|
||||||
// reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
|
|
||||||
String rgroup = "group";
|
|
||||||
String rinterface = "ocean.demo.api.IExchange"; // 弱类型接口名 必须
|
|
||||||
String rversion = "1.0.0";
|
|
||||||
String rmethod = "Say";
|
|
||||||
int rconnections = 3;
|
|
||||||
|
|
||||||
List<String> rparamTypes = new ArrayList<String>();
|
|
||||||
List<String> rparams = new ArrayList<String>();
|
|
||||||
|
|
||||||
String rkey = "";
|
|
||||||
rkey += application;
|
|
||||||
|
|
||||||
if (rgroup != null && !rgroup.isEmpty()) {
|
|
||||||
rkey += "/" + rgroup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rkey += "/" + rinterface;
|
|
||||||
rkey += ":" + rversion;
|
|
||||||
|
|
||||||
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
||||||
GenericService gs = gsPool.get(rkey);
|
GenericService gs = gsPool.get(uri);
|
||||||
if(gs == null) {
|
|
||||||
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
|
|
||||||
reference.setApplication(new ApplicationConfig("dubbo-exchange")); // 必须
|
|
||||||
// reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
|
|
||||||
reference.setGroup("test");
|
|
||||||
reference.setInterface("ocean.demo.api.IExchange"); // 弱类型接口名 必须
|
|
||||||
reference.setVersion("1.0.0"); // 必须
|
|
||||||
reference.setConnections(rconnections);
|
|
||||||
reference.setGeneric(true); // 声明为泛化接口
|
|
||||||
// gsPool.add(rkey, reference.get());
|
|
||||||
gs = reference.get();
|
|
||||||
gsPool.put(rkey, gs);
|
|
||||||
}
|
|
||||||
// special
|
// special
|
||||||
Object result = gs.$invoke("Say", new String[] { "java.lang.String" }, new Object[] { "222" });
|
Object result = gs.$invoke("Say", new String[] { "java.lang.String" }, new Object[] { "222" });
|
||||||
// gsPool.add(rkey, gs);
|
// gsPool.add(rkey, gs);
|
||||||
|
|
|
@ -5,22 +5,23 @@ import java.lang.reflect.Method;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import org.apache.dubbo.config.ReferenceConfig;
|
import org.apache.dubbo.config.ReferenceConfig;
|
||||||
import org.apache.dubbo.rpc.service.GenericService;
|
import org.apache.dubbo.rpc.service.GenericService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
||||||
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
||||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||||
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
|
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
@ -28,8 +29,8 @@ import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
|
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
|
||||||
import cn.ecpark.service.usergw.biz.filters.factory.DubboGatewayFilterFactory;
|
import cn.ecpark.service.usergw.biz.filters.factory.DubboGatewayFilterFactory;
|
||||||
import cn.ecpark.service.usergw.impl.http.Http2Dubbo;
|
|
||||||
import cn.ecpark.service.usergw.utils.Convert;
|
import cn.ecpark.service.usergw.utils.Convert;
|
||||||
|
import cn.ecpark.service.usergw.utils.Extract;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
@ -45,6 +46,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
|
|
||||||
List<FilterDefinition> defaultFilters = new ArrayList<>();
|
List<FilterDefinition> defaultFilters = new ArrayList<>();
|
||||||
HashMap<String, BiConsumer<ReferenceConfig<GenericService> , String>> specialField = new HashMap<String, BiConsumer<ReferenceConfig<GenericService> , String>>();
|
HashMap<String, BiConsumer<ReferenceConfig<GenericService> , String>> specialField = new HashMap<String, BiConsumer<ReferenceConfig<GenericService> , String>>();
|
||||||
|
Set<String> ignoreKey = new HashSet<>();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext appContext;
|
private ApplicationContext appContext;
|
||||||
|
@ -59,9 +61,12 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
return new DubboGatewayFilterFactory();
|
return new DubboGatewayFilterFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ConfigGateway() {
|
public ConfigGateway() {
|
||||||
specialField.put("application", ConfigSpecialFunction::setApplication);
|
specialField.put("application", ConfigSpecialFunction::setApplication);
|
||||||
|
specialField.put("registry", ConfigSpecialFunction::setRegistry);
|
||||||
|
|
||||||
|
ignoreKey.add("predicates");
|
||||||
|
ignoreKey.add("filters");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -180,13 +185,13 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
RouteDefinition rd = new RouteDefinition();
|
RouteDefinition rd = new RouteDefinition();
|
||||||
|
|
||||||
// 设置基础属性
|
// 设置基础属性
|
||||||
String uri = this.ParseDubboUriAndSetBase(rd, iter);
|
String dubboUri = this.ParseDubboUriAndSetBase(rd, iter);
|
||||||
|
|
||||||
// predicates: 下的相关属性
|
// predicates: 下的相关属性
|
||||||
this.ParseAndAddPredicates(predicates, iter, "predicates");
|
this.ParseAndAddPredicates(predicates, iter, "predicates");
|
||||||
|
|
||||||
// filters: 下的相关属性
|
// filters: 下的相关属性
|
||||||
this.ParseAndAddDubboFilters(filters, iter, "filters");
|
this.ParseAndAddDubboFilters(dubboUri, filters, iter, "filters");
|
||||||
|
|
||||||
rd.setPredicates(predicates);
|
rd.setPredicates(predicates);
|
||||||
rd.setFilters(filters);
|
rd.setFilters(filters);
|
||||||
|
@ -198,9 +203,9 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO: 任何错误都直接终止退出
|
|
||||||
log.error(e.toString());
|
log.error(e.toString());
|
||||||
SpringApplication.exit(appContext);
|
log.warn("App is exit");
|
||||||
|
((ConfigurableApplicationContext)appContext).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// gs.$invoke(method, parameterTypes, args)
|
// gs.$invoke(method, parameterTypes, args)
|
||||||
|
@ -233,6 +238,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
rd.setId((String) id);
|
rd.setId((String) id);
|
||||||
}
|
}
|
||||||
|
iter.remove("id");
|
||||||
|
|
||||||
// 设置uri
|
// 设置uri
|
||||||
Object uri = iter.get("uri");
|
Object uri = iter.get("uri");
|
||||||
|
@ -246,12 +252,14 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
} else {
|
} else {
|
||||||
rd.setUri(URI.create("dubbo://yame"));
|
rd.setUri(URI.create("dubbo://yame"));
|
||||||
}
|
}
|
||||||
|
iter.remove("uri");
|
||||||
|
|
||||||
// 设置uri
|
// 设置uri
|
||||||
Object order = iter.get("order");
|
Object order = iter.get("order");
|
||||||
if (order != null) {
|
if (order != null) {
|
||||||
rd.setOrder((int) order);
|
rd.setOrder((int) order);
|
||||||
}
|
}
|
||||||
|
iter.remove("order");
|
||||||
|
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
String uriString = (String) uri;
|
String uriString = (String) uri;
|
||||||
|
@ -261,62 +269,49 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
|
||||||
// GenericService gs = gsPool.get(rkey);
|
|
||||||
// if(gs == null) {
|
|
||||||
// ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
|
|
||||||
// reference.setApplication(new ApplicationConfig("dubbo-exchange")); // 必须
|
|
||||||
// // reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
|
|
||||||
// reference.setGroup("test");
|
|
||||||
// reference.setInterface("ocean.demo.api.IExchange"); // 弱类型接口名 必须
|
|
||||||
// reference.setVersion("1.0.0"); // 必须
|
|
||||||
// reference.setConnections(rconnections);
|
|
||||||
// reference.setGeneric(true); // 声明为泛化接口
|
|
||||||
// // gsPool.add(rkey, reference.get());
|
|
||||||
// gs = reference.get();
|
|
||||||
// gsPool.put(rkey, gs);
|
|
||||||
// }
|
|
||||||
|
|
||||||
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
|
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
|
||||||
String UriString = "dubbo://";
|
reference.setConnections(3);
|
||||||
|
|
||||||
|
String UriString = "dubbo://";
|
||||||
Object application = iter.get("application");
|
Object application = iter.get("application");
|
||||||
if (application != null) {
|
if (application != null) {
|
||||||
BiConsumer<ReferenceConfig<GenericService>, String> doFunc = specialField.get("application");
|
BiConsumer<ReferenceConfig<GenericService>, String> doFunc = specialField.get("application");
|
||||||
if(doFunc != null) {
|
if(doFunc != null) {
|
||||||
doFunc.accept(reference, (String)application);
|
doFunc.accept(reference, (String)application);
|
||||||
}
|
}
|
||||||
UriString += (String)application + "/";
|
|
||||||
}
|
}
|
||||||
|
iter.remove("application");
|
||||||
Object group = iter.get("group");
|
|
||||||
if (group != null) {
|
for( Entry<String, List<String>> entry : iter.entrySet() ) {
|
||||||
BiConsumer<ReferenceConfig<GenericService>, String> doFunc = specialField.get((String)group);
|
// Object group = iter.get("group");
|
||||||
if(doFunc != null) {
|
String key = entry.getKey();
|
||||||
doFunc.accept(reference, (String)application);
|
if(ignoreKey.contains(key)) {
|
||||||
} else {
|
continue;
|
||||||
try {
|
}
|
||||||
ConfigSpecialFunction.setDefault(reference, "group", (String)group);
|
Object value = entry.getValue();
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("配置解析错误 字段: {}:{}", "group", group);
|
if (value != null) {
|
||||||
}
|
BiConsumer<ReferenceConfig<GenericService>, String> doFunc = specialField.get((String)key);
|
||||||
|
if(doFunc != null) {
|
||||||
|
doFunc.accept(reference, (String)value);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
ConfigSpecialFunction.setDefault(reference, key, value);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("配置解析错误 字段: {}:{}\n{}", key, value, e.toString());
|
||||||
|
log.warn("App is exit");
|
||||||
|
((ConfigurableApplicationContext)appContext).close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
UriString += (String)application + "/";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UriString += Extract.getReferenceConfigKey(reference);
|
||||||
|
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
||||||
|
reference.setGeneric(true);
|
||||||
|
gsPool.put(UriString, reference.get());
|
||||||
|
|
||||||
Object registry = iter.get("registry");
|
return UriString;
|
||||||
if (registry != null) {
|
|
||||||
rd.setOrder((int) registry);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ParseAndAddPredicates(List<PredicateDefinition> predicates, LinkedHashMap<String, List<String>> iter,
|
private void ParseAndAddPredicates(List<PredicateDefinition> predicates, LinkedHashMap<String, List<String>> iter,
|
||||||
|
@ -340,7 +335,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ParseAndAddDubboFilters(List<FilterDefinition> filters, LinkedHashMap<String, List<String>> iter,
|
private void ParseAndAddDubboFilters(String dubboUri, List<FilterDefinition> filters, LinkedHashMap<String, List<String>> iter,
|
||||||
String yamlField) {
|
String yamlField) {
|
||||||
List<String> filtersYaml = iter.get(yamlField);
|
List<String> filtersYaml = iter.get(yamlField);
|
||||||
if (filtersYaml != null) {
|
if (filtersYaml != null) {
|
||||||
|
@ -350,10 +345,9 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
log.info(fd.getName());
|
log.info(fd.getName());
|
||||||
if (!fd.getName().equals("Dubbo")) {
|
if (!fd.getName().equals("Dubbo")) {
|
||||||
filters.add(fd);
|
filters.add(fd);
|
||||||
} else {
|
|
||||||
filters.add(fd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
filters.add(new FilterDefinition("Dubbo=" + dubboUri));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import javax.el.MethodNotFoundException;
|
||||||
|
|
||||||
import org.apache.dubbo.config.ApplicationConfig;
|
import org.apache.dubbo.config.ApplicationConfig;
|
||||||
import org.apache.dubbo.config.ReferenceConfig;
|
import org.apache.dubbo.config.ReferenceConfig;
|
||||||
|
import org.apache.dubbo.config.RegistryConfig;
|
||||||
import org.apache.dubbo.rpc.service.GenericService;
|
import org.apache.dubbo.rpc.service.GenericService;
|
||||||
|
|
||||||
import cn.ecpark.service.usergw.utils.Convert;
|
import cn.ecpark.service.usergw.utils.Convert;
|
||||||
|
@ -24,14 +25,18 @@ public class ConfigSpecialFunction {
|
||||||
// ref.setApplication(new ApplicationConfig(cfgValue));
|
// ref.setApplication(new ApplicationConfig(cfgValue));
|
||||||
// };
|
// };
|
||||||
|
|
||||||
public static void setApplication(ReferenceConfig<GenericService> ref, String cfgValue) {
|
public static void setApplication(ReferenceConfig<GenericService> ref, Object cfgValue) {
|
||||||
ref.setApplication(new ApplicationConfig(cfgValue));
|
ref.setApplication(new ApplicationConfig((String)cfgValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setDefault(ReferenceConfig<GenericService> ref, String methodName, String cfgValue) throws Exception {
|
public static void setRegistry(ReferenceConfig<GenericService> ref, Object cfgValue) {
|
||||||
cfgValue = "set" + Convert.firstUpperCase(methodName);
|
ref.setRegistry(new RegistryConfig((String)cfgValue));
|
||||||
log.info(methodName,cfgValue);
|
}
|
||||||
Method method = ref.getClass().getMethod(methodName);
|
|
||||||
|
public static void setDefault(ReferenceConfig<GenericService> ref, String methodName, Object cfgValue) 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);
|
method.invoke(ref, cfgValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,4 +13,5 @@ public class Convert {
|
||||||
}
|
}
|
||||||
return new String(ch);
|
return new String(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package cn.ecpark.service.usergw.utils;
|
||||||
|
|
||||||
|
import org.apache.dubbo.common.utils.StringUtils;
|
||||||
|
import org.apache.dubbo.config.ReferenceConfig;
|
||||||
|
import org.apache.dubbo.rpc.service.GenericService;
|
||||||
|
|
||||||
|
public class Extract {
|
||||||
|
public static String getReferenceConfigKey(ReferenceConfig<GenericService> referenceConfig) {
|
||||||
|
String iName = referenceConfig.getInterface();
|
||||||
|
if (StringUtils.isBlank(iName)) {
|
||||||
|
Class<?> clazz = referenceConfig.getInterfaceClass();
|
||||||
|
iName = clazz.getName();
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(iName)) {
|
||||||
|
throw new IllegalArgumentException("No interface info in ReferenceConfig" + referenceConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder ret = new StringBuilder();
|
||||||
|
ret.append(referenceConfig.getApplication().getName()).append("/");
|
||||||
|
if (!StringUtils.isBlank(referenceConfig.getGroup())) {
|
||||||
|
ret.append(referenceConfig.getGroup()).append("/");
|
||||||
|
}
|
||||||
|
ret.append(iName);
|
||||||
|
if (!StringUtils.isBlank(referenceConfig.getVersion())) {
|
||||||
|
ret.append(":").append(referenceConfig.getVersion());
|
||||||
|
}
|
||||||
|
return ret.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,6 +20,7 @@ dubbo:
|
||||||
- id: test
|
- id: test
|
||||||
order: 10
|
order: 10
|
||||||
application: dubbo-exchange
|
application: dubbo-exchange
|
||||||
|
group: test
|
||||||
registry: zookeeper://127.0.0.1:2181
|
registry: zookeeper://127.0.0.1:2181
|
||||||
interface: ocean.demo.api.IExchange
|
interface: ocean.demo.api.IExchange
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
|
Loading…
Reference in New Issue
Block a user