group实现

This commit is contained in:
huangsimin 2019-07-05 11:54:07 +08:00
parent dd55ff2f90
commit 342e18766e
2 changed files with 65 additions and 12 deletions

View File

@ -47,7 +47,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
HashMap<String, BiConsumer<ReferenceConfig<GenericService> , String>> specialField = new HashMap<String, BiConsumer<ReferenceConfig<GenericService> , String>>();
@Autowired
private ApplicationContext applicationContext;
private ApplicationContext appContext;
@Bean
public GenericServicePool genericServicePool() {
@ -200,7 +200,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
} catch (Exception e) {
// TODO: 任何错误都直接终止退出
log.error(e.toString());
SpringApplication.exit(applicationContext);
SpringApplication.exit(appContext);
}
// gs.$invoke(method, parameterTypes, args)
@ -261,11 +261,50 @@ public class ConfigGateway implements RouteDefinitionLocator {
}
}
String uriString = "dubbo://";
// 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>();
String UriString = "dubbo://";
Object application = iter.get("application");
if (application != null) {
BiConsumer<ReferenceConfig<GenericService>, String> doFunc = specialField.get("application");
if(doFunc != null) {
doFunc.accept(reference, (String)application);
}
UriString += (String)application + "/";
}
Object group = iter.get("group");
if (group != null) {
rd.setOrder((int) group);
BiConsumer<ReferenceConfig<GenericService>, String> doFunc = specialField.get((String)group);
if(doFunc != null) {
doFunc.accept(reference, (String)application);
} else {
try {
ConfigSpecialFunction.setDefault(reference, "group", (String)group);
} catch (Exception e) {
log.error("配置解析错误 字段: {}:{}", "group", group);
}
}
UriString += (String)application + "/";
}
Object registry = iter.get("registry");
@ -273,10 +312,9 @@ public class ConfigGateway implements RouteDefinitionLocator {
rd.setOrder((int) registry);
}
Object application = iter.get("application");
if (registry != null) {
rd.setOrder((int) application);
}
return "";
}

View File

@ -1,22 +1,37 @@
package cn.ecpark.service.usergw.config;
import java.lang.reflect.Method;
import java.util.function.BiConsumer;
import java.util.function.Function;
import javax.el.MethodNotFoundException;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService;
import cn.ecpark.service.usergw.utils.Convert;
import lombok.extern.slf4j.Slf4j;
/**
* ConfigSpecialFunction
*/
@Slf4j
public class ConfigSpecialFunction {
// public static BiConsumer<ReferenceConfig<GenericService>, String> setApplication = (ref, cfgValue) -> {
// public static BiConsumer<ReferenceConfig<GenericService>, String>
// setApplication = (ref, cfgValue) -> {
// ref.setApplication(new ApplicationConfig(cfgValue));
// };
public static void setApplication(ReferenceConfig<GenericService> ref, String cfgValue) {
ref.setApplication(new ApplicationConfig(cfgValue));
}
public static void setDefault(ReferenceConfig<GenericService> ref, String methodName, String cfgValue) throws Exception {
cfgValue = "set" + Convert.firstUpperCase(methodName);
log.info(methodName,cfgValue);
Method method = ref.getClass().getMethod(methodName);
method.invoke(ref, cfgValue);
}
}