优化媒体节点redis数据格式

pull/1412/head
648540858 2024-04-09 22:52:06 +08:00
parent 54b878d2e1
commit 82ca3a778a
3 changed files with 17 additions and 17 deletions

View File

@ -12,9 +12,9 @@ public class VideoManagerConstants {
public static final String WVP_SERVER_STREAM_PREFIX = "VMP_SIGNALLING_STREAM_"; public static final String WVP_SERVER_STREAM_PREFIX = "VMP_SIGNALLING_STREAM_";
public static final String MEDIA_SERVER_PREFIX = "VMP_MEDIA_SERVER_INFO_"; public static final String MEDIA_SERVER_PREFIX = "VMP_MEDIA_SERVER:";
public static final String MEDIA_SERVERS_ONLINE_PREFIX = "VMP_MEDIA_ONLINE_SERVERS_"; public static final String ONLINE_MEDIA_SERVERS_PREFIX = "VMP_ONLINE_MEDIA_SERVERS:";
public static final String DEVICE_PREFIX = "VMP_DEVICE_"; public static final String DEVICE_PREFIX = "VMP_DEVICE_";

View File

@ -125,7 +125,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
ssrcFactory.initMediaServerSSRC(mediaServer.getId(), null); ssrcFactory.initMediaServerSSRC(mediaServer.getId(), null);
} }
// 查询redis是否存在此mediaServer // 查询redis是否存在此mediaServer
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServer.getId(); String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + ":" + mediaServer.getId();
Boolean hasKey = redisTemplate.hasKey(key); Boolean hasKey = redisTemplate.hasKey(key);
if (hasKey != null && ! hasKey) { if (hasKey != null && ! hasKey) {
redisTemplate.opsForValue().set(key, mediaServer); redisTemplate.opsForValue().set(key, mediaServer);
@ -261,7 +261,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
if (mediaServerInRedis == null || !ssrcFactory.hasMediaServerSSRC(mediaServerInDataBase.getId())) { if (mediaServerInRedis == null || !ssrcFactory.hasMediaServerSSRC(mediaServerInDataBase.getId())) {
ssrcFactory.initMediaServerSSRC(mediaServerInDataBase.getId(),null); ssrcFactory.initMediaServerSSRC(mediaServerInDataBase.getId(),null);
} }
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerInDataBase.getId(); String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + ":" + mediaServerInDataBase.getId();
redisTemplate.opsForValue().set(key, mediaServerInDataBase); redisTemplate.opsForValue().set(key, mediaServerInDataBase);
if (mediaServerInDataBase.isStatus()) { if (mediaServerInDataBase.isStatus()) {
resetOnlineServerItem(mediaServerInDataBase); resetOnlineServerItem(mediaServerInDataBase);
@ -277,8 +277,8 @@ public class MediaServerServiceImpl implements IMediaServerService {
@Override @Override
public List<MediaServer> getAllOnlineList() { public List<MediaServer> getAllOnlineList() {
List<MediaServer> result = new ArrayList<>(); List<MediaServer> result = new ArrayList<>();
List<Object> mediaServerKeys = RedisUtil.scan(redisTemplate, String.format("%S*", VideoManagerConstants.MEDIA_SERVER_PREFIX+ userSetting.getServerId() + "_" )); List<Object> mediaServerKeys = RedisUtil.scan(redisTemplate, String.format("%S*", VideoManagerConstants.MEDIA_SERVER_PREFIX+ userSetting.getServerId() + ":" ));
String onlineKey = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); String onlineKey = VideoManagerConstants.ONLINE_MEDIA_SERVERS_PREFIX + userSetting.getServerId();
for (Object mediaServerKey : mediaServerKeys) { for (Object mediaServerKey : mediaServerKeys) {
String key = (String) mediaServerKey; String key = (String) mediaServerKey;
MediaServer mediaServer = JsonUtil.redisJsonToObject(redisTemplate, key, MediaServer.class); MediaServer mediaServer = JsonUtil.redisJsonToObject(redisTemplate, key, MediaServer.class);
@ -326,14 +326,14 @@ public class MediaServerServiceImpl implements IMediaServerService {
@Override @Override
public List<MediaServer> getAllOnline() { public List<MediaServer> getAllOnline() {
String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); String key = VideoManagerConstants.ONLINE_MEDIA_SERVERS_PREFIX + userSetting.getServerId();
Set<Object> mediaServerIdSet = redisTemplate.opsForZSet().reverseRange(key, 0, -1); Set<Object> mediaServerIdSet = redisTemplate.opsForZSet().reverseRange(key, 0, -1);
List<MediaServer> result = new ArrayList<>(); List<MediaServer> result = new ArrayList<>();
if (mediaServerIdSet != null && mediaServerIdSet.size() > 0) { if (mediaServerIdSet != null && mediaServerIdSet.size() > 0) {
for (Object mediaServerId : mediaServerIdSet) { for (Object mediaServerId : mediaServerIdSet) {
String mediaServerIdStr = (String) mediaServerId; String mediaServerIdStr = (String) mediaServerId;
String serverKey = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerIdStr; String serverKey = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + ":" + mediaServerIdStr;
result.add((MediaServer) redisTemplate.opsForValue().get(serverKey)); result.add((MediaServer) redisTemplate.opsForValue().get(serverKey));
} }
} }
@ -351,7 +351,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
if (mediaServerId == null) { if (mediaServerId == null) {
return null; return null;
} }
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerId; String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + ":" + mediaServerId;
return JsonUtil.redisJsonToObject(redisTemplate, key, MediaServer.class); return JsonUtil.redisJsonToObject(redisTemplate, key, MediaServer.class);
} }
@ -363,7 +363,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
@Override @Override
public void clearMediaServerForOnline() { public void clearMediaServerForOnline() {
String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); String key = VideoManagerConstants.ONLINE_MEDIA_SERVERS_PREFIX + userSetting.getServerId();
redisTemplate.delete(key); redisTemplate.delete(key);
} }
@ -401,7 +401,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
@Override @Override
public void resetOnlineServerItem(MediaServer serverItem) { public void resetOnlineServerItem(MediaServer serverItem) {
// 更新缓存 // 更新缓存
String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); String key = VideoManagerConstants.ONLINE_MEDIA_SERVERS_PREFIX + userSetting.getServerId();
// 使用zset的分数作为当前并发量 默认值设置为0 // 使用zset的分数作为当前并发量 默认值设置为0
if (redisTemplate.opsForZSet().score(key, serverItem.getId()) == null) { // 不存在则设置默认值 已存在则重置 if (redisTemplate.opsForZSet().score(key, serverItem.getId()) == null) { // 不存在则设置默认值 已存在则重置
redisTemplate.opsForZSet().add(key, serverItem.getId(), 0L); redisTemplate.opsForZSet().add(key, serverItem.getId(), 0L);
@ -424,14 +424,14 @@ public class MediaServerServiceImpl implements IMediaServerService {
if (mediaServerId == null) { if (mediaServerId == null) {
return; return;
} }
String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); String key = VideoManagerConstants.ONLINE_MEDIA_SERVERS_PREFIX + userSetting.getServerId();
redisTemplate.opsForZSet().incrementScore(key, mediaServerId, 1); redisTemplate.opsForZSet().incrementScore(key, mediaServerId, 1);
} }
@Override @Override
public void removeCount(String mediaServerId) { public void removeCount(String mediaServerId) {
String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); String key = VideoManagerConstants.ONLINE_MEDIA_SERVERS_PREFIX + userSetting.getServerId();
redisTemplate.opsForZSet().incrementScore(key, mediaServerId, - 1); redisTemplate.opsForZSet().incrementScore(key, mediaServerId, - 1);
} }
@ -441,7 +441,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
*/ */
@Override @Override
public MediaServer getMediaServerForMinimumLoad(Boolean hasAssist) { public MediaServer getMediaServerForMinimumLoad(Boolean hasAssist) {
String key = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); String key = VideoManagerConstants.ONLINE_MEDIA_SERVERS_PREFIX + userSetting.getServerId();
Long size = redisTemplate.opsForZSet().zCard(key); Long size = redisTemplate.opsForZSet().zCard(key);
if (size == null || size == 0) { if (size == null || size == 0) {
logger.info("获取负载最低的节点时无在线节点"); logger.info("获取负载最低的节点时无在线节点");
@ -520,8 +520,8 @@ public class MediaServerServiceImpl implements IMediaServerService {
@Override @Override
public void delete(String id) { public void delete(String id) {
mediaServerMapper.delOne(id); mediaServerMapper.delOne(id);
redisTemplate.opsForZSet().remove(VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(), id); redisTemplate.opsForZSet().remove(VideoManagerConstants.ONLINE_MEDIA_SERVERS_PREFIX + userSetting.getServerId(), id);
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + id; String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + ":" + id;
redisTemplate.delete(key); redisTemplate.delete(key);
// 发送节点移除通知 // 发送节点移除通知
MediaServerDeleteEvent event = new MediaServerDeleteEvent(this); MediaServerDeleteEvent event = new MediaServerDeleteEvent(this);

View File

@ -2,4 +2,4 @@ spring:
application: application:
name: wvp name: wvp
profiles: profiles:
active: abl active: local