由于示例,远程服务地址配置在properties文件,通过QMConfig类加载,最优方式是上节介绍过,放在共享内存上,只需要维护一份数据即可,如放在redis上

    /** 服务地址<service,address> **/
@FieldValue("QM.REMOTE_SERVICE")
public Map<String, String> REMOTE_SERVICE = new HashMap<>(); public boolean isRemoteService(Class<?> target) {
if (!target.isInterface()) {
target = ReflectUtil.getInterfaceForAnno(target, QModel.class);
}
return REMOTE_SERVICE.containsKey(target.getName());
}

qm.properties 加上

QM.REMOTE_SERVICE={"com.eyu.onequeue.rpc.ITestEnhance":"xxx.xxx.xxx.xxx:22222"}

QRpcFactory.class 添加代码

    public static <T> T loadSystemService(Class<T> target) {
T ret = null;
if (QMConfig.getInstance().isRemoteService(target)) {
// read config
String address = QMConfig.getInstance().REMOTE_SERVICE.get(target.getName());
ret = loadSendProxy(target, address);
} else {
ret = loadLocalProxy(target);
}
return ret;
}

测试代码:

    @Test
public void testSystemService() {
Set<Class<?>> values = new HashSet<>();
PackageScanner.scan((clz) -> {
QModel modelAnno = ReflectUtil.getAnno(clz, QModel.class);
if (modelAnno == null) {
return;
}
values.add(clz);
} , "com.eyu.onequeue"); for (Class<?> clz : values) {
if (clz.isInterface()) {
if (!QMConfig.getInstance().isRemoteService(clz)) {
continue;
}
QRpcFactory.registerSendProxy(clz);
System.out.println("registerSendProxy : " + clz);
} else {
if (QMConfig.getInstance().isRemoteService(clz)) {
continue;
}
try {
QRpcFactory.registerReceiveProxy(clz.newInstance());
System.out.println("registerReceiveProxy : " + clz);
} catch (Exception e) {
e.printStackTrace();
}
}
} //test removet
ITestEnhance obj = QRpcFactory.loadSystemService(ITestEnhance.class);
obj.a(1d);
//test local
TestObject obj1 = QRpcFactory.loadSystemService(TestObject.class);
obj1.a(1, "b");
}
05-11 10:49