[redis下的多wvp] 优化服务间方法调用
This commit is contained in:
@@ -3,10 +3,13 @@ package com.genersoft.iot.vmp.conf.redis;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.genersoft.iot.vmp.common.CommonCallback;
|
||||
import com.genersoft.iot.vmp.conf.UserSetting;
|
||||
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcClassHandler;
|
||||
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcMessage;
|
||||
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcRequest;
|
||||
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse;
|
||||
import com.genersoft.iot.vmp.service.redisMsg.control.RedisRpcController;
|
||||
import com.genersoft.iot.vmp.jt1078.util.ClassUtil;
|
||||
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcController;
|
||||
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcMapping;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
@@ -16,8 +19,11 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -36,9 +42,6 @@ public class RedisRpcConfig implements MessageListener {
|
||||
@Autowired
|
||||
private UserSetting userSetting;
|
||||
|
||||
@Autowired
|
||||
private RedisRpcController redisRpcController;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<Object, Object> redisTemplate;
|
||||
|
||||
@@ -48,6 +51,37 @@ public class RedisRpcConfig implements MessageListener {
|
||||
@Autowired
|
||||
private ThreadPoolTaskExecutor taskExecutor;
|
||||
|
||||
private final static Map<String, RedisRpcClassHandler> protocolHash = new HashMap<>();
|
||||
|
||||
// 启动时执行
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
List<Class<?>> classList = ClassUtil.getClassList("com.genersoft.iot.vmp.service.redisMsg.control", RedisRpcController.class);
|
||||
for (Class<?> handlerClass : classList) {
|
||||
String controllerPath = handlerClass.getAnnotation(RedisRpcController.class).value();
|
||||
// 扫描其下的方法
|
||||
Method[] methods = handlerClass.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
RedisRpcMapping annotation = method.getAnnotation(RedisRpcMapping.class);
|
||||
if (annotation != null) {
|
||||
String methodPath = annotation.value();
|
||||
if (methodPath != null) {
|
||||
protocolHash.put(controllerPath + "/" + methodPath, new RedisRpcClassHandler(handlerClass, method));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
for (String s : protocolHash.keySet()) {
|
||||
System.out.println(s);
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("消息ID缓存表 protocolHash:{}", protocolHash);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
boolean isEmpty = taskQueue.isEmpty();
|
||||
@@ -87,7 +121,9 @@ public class RedisRpcConfig implements MessageListener {
|
||||
return;
|
||||
}
|
||||
log.info("[redis-rpc] << {}", request);
|
||||
Method method = getMethod(request.getUri());
|
||||
RedisRpcClassHandler redisRpcClassHandler = protocolHash.get(request.getUri());
|
||||
Class<?> objectClass = redisRpcClassHandler.getObjectClass();
|
||||
Method method = redisRpcClassHandler.getMethod();
|
||||
// 没有携带目标ID的可以理解为哪个wvp有结果就哪个回复,携带目标ID,但是如果是不存在的uri则直接回复404
|
||||
if (userSetting.getServerId().equals(request.getToId())) {
|
||||
if (method == null) {
|
||||
@@ -97,7 +133,7 @@ public class RedisRpcConfig implements MessageListener {
|
||||
sendResponse(response);
|
||||
return;
|
||||
}
|
||||
RedisRpcResponse response = (RedisRpcResponse)method.invoke(redisRpcController, request);
|
||||
RedisRpcResponse response = (RedisRpcResponse)method.invoke(objectClass, request);
|
||||
if(response != null) {
|
||||
sendResponse(response);
|
||||
}
|
||||
@@ -105,7 +141,7 @@ public class RedisRpcConfig implements MessageListener {
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
RedisRpcResponse response = (RedisRpcResponse)method.invoke(redisRpcController, request);
|
||||
RedisRpcResponse response = (RedisRpcResponse)method.invoke(objectClass, request);
|
||||
if (response != null) {
|
||||
sendResponse(response);
|
||||
}
|
||||
@@ -116,17 +152,6 @@ public class RedisRpcConfig implements MessageListener {
|
||||
|
||||
}
|
||||
|
||||
private Method getMethod(String name) {
|
||||
// 启动后扫描所有的路径注解
|
||||
Method[] methods = redisRpcController.getClass().getMethods();
|
||||
for (Method method : methods) {
|
||||
if (method.getName().equals(name)) {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sendResponse(RedisRpcResponse response){
|
||||
log.info("[redis-rpc] >> {}", response);
|
||||
response.setToId(userSetting.getServerId());
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.genersoft.iot.vmp.conf.redis.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@Data
|
||||
public class RedisRpcClassHandler {
|
||||
|
||||
private Class<?> objectClass;
|
||||
private Method method;
|
||||
|
||||
public RedisRpcClassHandler(Class<?> objectClass, Method method) {
|
||||
this.objectClass = objectClass;
|
||||
this.method = method;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user