🐛 Fixing a bug. close #3734 common-websocket 在分布式环境多个实例中的基于redis消息分发逻辑错误
This commit is contained in:
1 parent
b9f57016c8
commit
ef0fded50f
3 files changed
+4
-20
No files matched your search
+2
-3
@@ -14,7 +14,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
@@ -32,8 +31,8 @@ public class WebSocketHandlerConfig {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(SessionKeyGenerator.class)
|
||||
public SessionKeyGenerator sessionKeyGenerator(ServiceInstance serviceInstance) {
|
||||
return new PigxSessionKeyGenerator(serviceInstance);
|
||||
public SessionKeyGenerator sessionKeyGenerator() {
|
||||
return new PigxSessionKeyGenerator();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+2
-5
@@ -3,7 +3,6 @@ package com.pig4cloud.pigx.common.websocket.custom;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUser;
|
||||
import com.pig4cloud.pigx.common.websocket.holder.SessionKeyGenerator;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
@@ -15,8 +14,6 @@ import org.springframework.web.socket.WebSocketSession;
|
||||
@RequiredArgsConstructor
|
||||
public class PigxSessionKeyGenerator implements SessionKeyGenerator {
|
||||
|
||||
private final ServiceInstance instance;
|
||||
|
||||
/**
|
||||
* 获取当前session的唯一标识
|
||||
* @param webSocketSession 当前session
|
||||
@@ -29,8 +26,8 @@ public class PigxSessionKeyGenerator implements SessionKeyGenerator {
|
||||
|
||||
if (obj instanceof PigxUser) {
|
||||
PigxUser user = (PigxUser) obj;
|
||||
// IP:port:userId 作为唯一区分
|
||||
return String.format("%s:%s:%s", instance.getHost(), instance.getPort(), user.getId());
|
||||
// userId 作为唯一区分
|
||||
return user.getId();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
-12
@@ -1,14 +1,9 @@
|
||||
package com.pig4cloud.pigx.common.websocket.distribute;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 消息分发器
|
||||
*
|
||||
@@ -26,13 +21,6 @@ public class RedisMessageDistributor implements MessageDistributor {
|
||||
*/
|
||||
@Override
|
||||
public void distribute(MessageDO messageDO) {
|
||||
// 包装 sessionKey 适配分布式多环境
|
||||
ServiceInstance instance = SpringContextHolder.getBean(ServiceInstance.class);
|
||||
List<Object> sessionKeyList = messageDO.getSessionKeys().stream()
|
||||
.map(key -> String.format("%s:%s:%s", instance.getHost(), instance.getPort(), key))
|
||||
.collect(Collectors.toList());
|
||||
messageDO.setSessionKeys(sessionKeyList);
|
||||
|
||||
String str = JSONUtil.toJsonStr(messageDO);
|
||||
stringRedisTemplate.convertAndSend(RedisWebsocketMessageListener.CHANNEL, str);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user