im问题修复-redis统一key,保证不同服务器保存相同的客服

This commit is contained in:
liyj 2025-11-18 16:52:45 +08:00
parent a047598703
commit a2e0854a7d

View File

@ -24,7 +24,7 @@ public class DistributedSessionService {
private static final String USER_SERVER_KEY = "im:user:server:"; private static final String USER_SERVER_KEY = "im:user:server:";
private static final String USER_SESSIONS_KEY = "im:user:sessions:"; private static final String USER_SESSIONS_KEY = "im:user:sessions:";
private static final String SERVER_USERS_KEY = "im:server:users:"; private static final String SERVER_USERS_KEY = "im:server:users:kf";
private static final String GROUP_SESSIONS_KEY = "im:group:sessions:"; private static final String GROUP_SESSIONS_KEY = "im:group:sessions:";
/** /**
@ -33,7 +33,7 @@ public class DistributedSessionService {
public void registerUserSession(String userId, String sessionId, Map<String, Object> attributes) { public void registerUserSession(String userId, String sessionId, Map<String, Object> attributes) {
String userKey = USER_SESSIONS_KEY + userId; String userKey = USER_SESSIONS_KEY + userId;
String serverKey = USER_SERVER_KEY + userId; String serverKey = USER_SERVER_KEY + userId;
String serverUsersKey = SERVER_USERS_KEY + getServerId(); String serverUsersKey = SERVER_USERS_KEY;
// 存储用户会话信息 // 存储用户会话信息
Map<String, Object> sessionInfo = new HashMap<>(); Map<String, Object> sessionInfo = new HashMap<>();
@ -61,7 +61,7 @@ public class DistributedSessionService {
*/ */
public void unregisterUserSession(String userId, String sessionId) { public void unregisterUserSession(String userId, String sessionId) {
String userKey = USER_SESSIONS_KEY + userId; String userKey = USER_SESSIONS_KEY + userId;
String serverUsersKey = SERVER_USERS_KEY + getServerId(); String serverUsersKey = SERVER_USERS_KEY;
// 从用户会话中移除 // 从用户会话中移除
redisTemplate.opsForHash().delete(userKey, sessionId); redisTemplate.opsForHash().delete(userKey, sessionId);
@ -146,7 +146,7 @@ public class DistributedSessionService {
*/ */
public List<Integer> getOnlineUserIds() { public List<Integer> getOnlineUserIds() {
// 方法1: 通过服务器用户集合获取推荐 // 方法1: 通过服务器用户集合获取推荐
String serverUsersKey = SERVER_USERS_KEY + getServerId(); String serverUsersKey = SERVER_USERS_KEY;
Set<Object> userIds = redisTemplate.opsForSet().members(serverUsersKey); Set<Object> userIds = redisTemplate.opsForSet().members(serverUsersKey);
if (userIds != null && !userIds.isEmpty()) { if (userIds != null && !userIds.isEmpty()) {