优化RPC调用

dev/数据库统合
648540858 2024-12-12 12:42:57 +08:00
parent 9d96658696
commit 0a06ae9475
10 changed files with 96 additions and 79 deletions

View File

@ -1,5 +1,6 @@
package com.genersoft.iot.vmp; package com.genersoft.iot.vmp;
import com.genersoft.iot.vmp.jt1078.util.ClassUtil;
import com.genersoft.iot.vmp.utils.GitUtil; import com.genersoft.iot.vmp.utils.GitUtil;
import com.genersoft.iot.vmp.utils.SpringBeanFactory; import com.genersoft.iot.vmp.utils.SpringBeanFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -33,6 +34,7 @@ public class VManageBootstrap extends SpringBootServletInitializer {
public static void main(String[] args) { public static void main(String[] args) {
VManageBootstrap.args = args; VManageBootstrap.args = args;
VManageBootstrap.context = SpringApplication.run(VManageBootstrap.class, args); VManageBootstrap.context = SpringApplication.run(VManageBootstrap.class, args);
ClassUtil.context = VManageBootstrap.context;
GitUtil gitUtil = SpringBeanFactory.getBean("gitUtil"); GitUtil gitUtil = SpringBeanFactory.getBean("gitUtil");
if (gitUtil == null) { if (gitUtil == null) {
log.info("获取版本信息失败"); log.info("获取版本信息失败");
@ -62,6 +64,5 @@ public class VManageBootstrap extends SpringBootServletInitializer {
); );
SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig(); SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
sessionCookieConfig.setHttpOnly(true); sessionCookieConfig.setHttpOnly(true);
} }
} }

View File

@ -7,9 +7,7 @@ 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.RedisRpcMessage;
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcRequest; import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcRequest;
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse; import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse;
import com.genersoft.iot.vmp.jt1078.util.ClassUtil; import com.genersoft.iot.vmp.service.redisMsg.dto.RpcController;
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcController;
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcMapping;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
@ -19,11 +17,9 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -53,34 +49,37 @@ public class RedisRpcConfig implements MessageListener {
private final static Map<String, RedisRpcClassHandler> protocolHash = new HashMap<>(); private final static Map<String, RedisRpcClassHandler> protocolHash = new HashMap<>();
// 启动时执行 public void addHandler(String path, RedisRpcClassHandler handler) {
@PostConstruct protocolHash.put(path, handler);
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 run(String... args) throws Exception {
// 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();
// Object bean = ClassUtil.getBean(controllerPath, handlerClass);
// // 扫描其下的方法
// 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(bean, method));
// }
// }
//
// }
//
// }
// for (String s : protocolHash.keySet()) {
// System.out.println(s);
// }
// if (log.isDebugEnabled()) {
// log.debug("消息ID缓存表 protocolHash:{}", protocolHash);
// }
// }
@Override @Override
public void onMessage(Message message, byte[] pattern) { public void onMessage(Message message, byte[] pattern) {
@ -100,7 +99,7 @@ public class RedisRpcConfig implements MessageListener {
log.error("[redis rpc 解析失败] {}", JSON.toJSONString(redisRpcMessage)); log.error("[redis rpc 解析失败] {}", JSON.toJSONString(redisRpcMessage));
} }
} catch (Exception e) { } catch (Exception e) {
log.error("[redis rpc 解析异常] ", e); log.error("[redis rpc 解析异常] {}",new String(msg.getBody()), e);
} }
} }
}); });
@ -122,7 +121,7 @@ public class RedisRpcConfig implements MessageListener {
} }
log.info("[redis-rpc] << {}", request); log.info("[redis-rpc] << {}", request);
RedisRpcClassHandler redisRpcClassHandler = protocolHash.get(request.getUri()); RedisRpcClassHandler redisRpcClassHandler = protocolHash.get(request.getUri());
Class<?> objectClass = redisRpcClassHandler.getObjectClass(); RpcController controller = redisRpcClassHandler.getController();
Method method = redisRpcClassHandler.getMethod(); Method method = redisRpcClassHandler.getMethod();
// 没有携带目标ID的可以理解为哪个wvp有结果就哪个回复携带目标ID但是如果是不存在的uri则直接回复404 // 没有携带目标ID的可以理解为哪个wvp有结果就哪个回复携带目标ID但是如果是不存在的uri则直接回复404
if (userSetting.getServerId().equals(request.getToId())) { if (userSetting.getServerId().equals(request.getToId())) {
@ -133,7 +132,7 @@ public class RedisRpcConfig implements MessageListener {
sendResponse(response); sendResponse(response);
return; return;
} }
RedisRpcResponse response = (RedisRpcResponse)method.invoke(objectClass, request); RedisRpcResponse response = (RedisRpcResponse)method.invoke(controller, request);
if(response != null) { if(response != null) {
sendResponse(response); sendResponse(response);
} }
@ -141,7 +140,7 @@ public class RedisRpcConfig implements MessageListener {
if (method == null) { if (method == null) {
return; return;
} }
RedisRpcResponse response = (RedisRpcResponse)method.invoke(objectClass, request); RedisRpcResponse response = (RedisRpcResponse)method.invoke(controller, request);
if (response != null) { if (response != null) {
sendResponse(response); sendResponse(response);
} }
@ -234,6 +233,8 @@ public class RedisRpcConfig implements MessageListener {
return callbacks.size(); return callbacks.size();
} }
// @Scheduled(fixedRate = 1000) //每1秒执行一次 // @Scheduled(fixedRate = 1000) //每1秒执行一次
// public void execute(){ // public void execute(){
// logger.info("callbacks的长度: " + callbacks.size()); // logger.info("callbacks的长度: " + callbacks.size());

View File

@ -1,5 +1,6 @@
package com.genersoft.iot.vmp.conf.redis.bean; package com.genersoft.iot.vmp.conf.redis.bean;
import com.genersoft.iot.vmp.service.redisMsg.dto.RpcController;
import lombok.Data; import lombok.Data;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -7,11 +8,11 @@ import java.lang.reflect.Method;
@Data @Data
public class RedisRpcClassHandler { public class RedisRpcClassHandler {
private Class<?> objectClass; private RpcController controller;
private Method method; private Method method;
public RedisRpcClassHandler(Class<?> objectClass, Method method) { public RedisRpcClassHandler(RpcController controller, Method method) {
this.objectClass = objectClass; this.controller = controller;
this.method = method; this.method = method;
} }
} }

View File

@ -1,6 +1,7 @@
package com.genersoft.iot.vmp.jt1078.util; package com.genersoft.iot.vmp.jt1078.util;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver;
@ -12,6 +13,12 @@ import java.util.List;
@Slf4j @Slf4j
public class ClassUtil { public class ClassUtil {
public static ConfigurableApplicationContext context;
public static <T> T getBean(String beanName, Class<T> clazz) {
return context.getBean(beanName, clazz);
}
public static Object getBean(Class<?> clazz) { public static Object getBean(Class<?> clazz) {
if (clazz != null) { if (clazz != null) {
try { try {

View File

@ -1,35 +1,28 @@
package com.genersoft.iot.vmp.service.redisMsg.control; package com.genersoft.iot.vmp.service.redisMsg.control;
import com.alibaba.fastjson2.JSONObject;
import com.genersoft.iot.vmp.common.StreamInfo;
import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.conf.redis.RedisRpcConfig; import com.genersoft.iot.vmp.conf.redis.RedisRpcConfig;
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcMessage; 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.RedisRpcRequest;
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse; import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse;
import com.genersoft.iot.vmp.gb28181.bean.CommonGBChannel; import com.genersoft.iot.vmp.gb28181.bean.CommonGBChannel;
import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo;
import com.genersoft.iot.vmp.gb28181.service.IGbChannelPlayService; import com.genersoft.iot.vmp.gb28181.service.IGbChannelPlayService;
import com.genersoft.iot.vmp.gb28181.service.IGbChannelService; import com.genersoft.iot.vmp.gb28181.service.IGbChannelService;
import com.genersoft.iot.vmp.gb28181.session.SSRCFactory;
import com.genersoft.iot.vmp.media.bean.MediaServer;
import com.genersoft.iot.vmp.media.event.hook.Hook;
import com.genersoft.iot.vmp.media.event.hook.HookSubscribe;
import com.genersoft.iot.vmp.media.event.hook.HookType;
import com.genersoft.iot.vmp.media.service.IMediaServerService;
import com.genersoft.iot.vmp.service.ISendRtpServerService;
import com.genersoft.iot.vmp.service.bean.InviteErrorCode; import com.genersoft.iot.vmp.service.bean.InviteErrorCode;
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcController; import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcController;
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcMapping; import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcMapping;
import com.genersoft.iot.vmp.service.redisMsg.dto.RpcController;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.sip.message.Response; import javax.sip.message.Response;
@Component
@Slf4j @Slf4j
@RedisRpcController("channel") @RedisRpcController("channel")
public class RedisRpcChannelPlayController { public class RedisRpcChannelPlayController extends RpcController {
@Autowired @Autowired
private UserSetting userSetting; private UserSetting userSetting;

View File

@ -5,27 +5,26 @@ import com.genersoft.iot.vmp.conf.exception.ControllerException;
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcRequest; import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcRequest;
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse; import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse;
import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo; import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo;
import com.genersoft.iot.vmp.gb28181.service.IGbChannelPlayService;
import com.genersoft.iot.vmp.gb28181.service.IGbChannelService;
import com.genersoft.iot.vmp.gb28181.session.SSRCFactory; import com.genersoft.iot.vmp.gb28181.session.SSRCFactory;
import com.genersoft.iot.vmp.media.bean.MediaInfo; import com.genersoft.iot.vmp.media.bean.MediaInfo;
import com.genersoft.iot.vmp.media.bean.MediaServer; import com.genersoft.iot.vmp.media.bean.MediaServer;
import com.genersoft.iot.vmp.media.event.hook.HookSubscribe;
import com.genersoft.iot.vmp.media.service.IMediaServerService; import com.genersoft.iot.vmp.media.service.IMediaServerService;
import com.genersoft.iot.vmp.service.ISendRtpServerService; import com.genersoft.iot.vmp.service.ISendRtpServerService;
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcController; import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcController;
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcMapping; import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcMapping;
import com.genersoft.iot.vmp.service.redisMsg.dto.RpcController;
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult; import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component;
import javax.sip.message.Response; import javax.sip.message.Response;
@Component
@Slf4j @Slf4j
@RedisRpcController("sendRtp") @RedisRpcController("sendRtp")
public class RedisRpcSendRtpController { public class RedisRpcSendRtpController extends RpcController {
@Autowired @Autowired
private SSRCFactory ssrcFactory; private SSRCFactory ssrcFactory;
@ -39,17 +38,6 @@ public class RedisRpcSendRtpController {
@Autowired @Autowired
private UserSetting userSetting; private UserSetting userSetting;
@Autowired
private HookSubscribe hookSubscribe;
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
@Autowired
private IGbChannelService channelService;
@Autowired
private IGbChannelPlayService channelPlayService;
/** /**
* *

View File

@ -8,8 +8,6 @@ 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.RedisRpcRequest;
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse; import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcResponse;
import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo; import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo;
import com.genersoft.iot.vmp.gb28181.service.IGbChannelPlayService;
import com.genersoft.iot.vmp.gb28181.service.IGbChannelService;
import com.genersoft.iot.vmp.gb28181.session.SSRCFactory; import com.genersoft.iot.vmp.gb28181.session.SSRCFactory;
import com.genersoft.iot.vmp.media.bean.MediaServer; import com.genersoft.iot.vmp.media.bean.MediaServer;
import com.genersoft.iot.vmp.media.event.hook.Hook; import com.genersoft.iot.vmp.media.event.hook.Hook;
@ -19,13 +17,16 @@ import com.genersoft.iot.vmp.media.service.IMediaServerService;
import com.genersoft.iot.vmp.service.ISendRtpServerService; import com.genersoft.iot.vmp.service.ISendRtpServerService;
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcController; import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcController;
import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcMapping; import com.genersoft.iot.vmp.service.redisMsg.dto.RedisRpcMapping;
import com.genersoft.iot.vmp.service.redisMsg.dto.RpcController;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
@Slf4j @Slf4j
@RedisRpcController("streamPush") @RedisRpcController("streamPush")
public class RedisRpcStreamPushController { public class RedisRpcStreamPushController extends RpcController {
@Autowired @Autowired
private SSRCFactory ssrcFactory; private SSRCFactory ssrcFactory;
@ -45,11 +46,6 @@ public class RedisRpcStreamPushController {
@Autowired @Autowired
private RedisTemplate<Object, Object> redisTemplate; private RedisTemplate<Object, Object> redisTemplate;
@Autowired
private IGbChannelService channelService;
@Autowired
private IGbChannelPlayService channelPlayService;
private void sendResponse(RedisRpcResponse response){ private void sendResponse(RedisRpcResponse response){
log.info("[redis-rpc] >> {}", response); log.info("[redis-rpc] >> {}", response);

View File

@ -1,13 +1,10 @@
package com.genersoft.iot.vmp.service.redisMsg.dto; package com.genersoft.iot.vmp.service.redisMsg.dto;
import org.springframework.stereotype.Component;
import java.lang.annotation.*; import java.lang.annotation.*;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
@Component
public @interface RedisRpcController { public @interface RedisRpcController {
/** /**
* *

View File

@ -0,0 +1,33 @@
package com.genersoft.iot.vmp.service.redisMsg.dto;
import com.genersoft.iot.vmp.conf.redis.RedisRpcConfig;
import com.genersoft.iot.vmp.conf.redis.bean.RedisRpcClassHandler;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
public class RpcController {
@Autowired
private RedisRpcConfig redisRpcConfig;
@PostConstruct
public void init() {
String controllerPath = this.getClass().getAnnotation(RedisRpcController.class).value();
// 扫描其下的方法
Method[] methods = this.getClass().getDeclaredMethods();
for (Method method : methods) {
RedisRpcMapping annotation = method.getAnnotation(RedisRpcMapping.class);
if (annotation != null) {
String methodPath = annotation.value();
if (methodPath != null) {
redisRpcConfig.addHandler(controllerPath + "/" + methodPath, new RedisRpcClassHandler(this, method));
}
}
}
}
}

View File

@ -33,9 +33,9 @@
download='推流通道导入.zip'>下载模板</a> download='推流通道导入.zip'>下载模板</a>
</el-button> </el-button>
<el-button icon="el-icon-delete" size="mini" style="margin-right: 1rem;" <el-button icon="el-icon-delete" size="mini" style="margin-right: 1rem;"
:disabled="multipleSelection.length === 0" type="danger" @click="batchDel">批量移除 :disabled="multipleSelection.length === 0" type="danger" @click="batchDel">移除
</el-button> </el-button>
<el-button icon="el-icon-plus" size="mini" style="margin-right: 1rem;" type="primary" @click="addStream"> <el-button icon="el-icon-plus" size="mini" style="margin-right: 1rem;" type="primary" @click="addStream">
</el-button> </el-button>
<el-button icon="el-icon-refresh-right" circle size="mini" @click="refresh()"></el-button> <el-button icon="el-icon-refresh-right" circle size="mini" @click="refresh()"></el-button>
</div> </div>