redis 的发布订阅解决分布式下的websocket session 共享问题

1.项目背景 :使用websocket 和前端通信但是后端是分布式部署,session 无法共享,并且websocketSession 无法序列化,所以无法把session存到redis 中。

2.基础代码:

java 复制代码
@Configuration
@Slf4j
public class RedisConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic(Constants.REDIS_CHANNEL));
        return container;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter(RedisReceiver receiver) {
        return new MessageListenerAdapter(receiver, "onMessage");
    }


}
java 复制代码
@Slf4j
@Component
public class RedisReceiver implements MessageListener {

    @Autowired
    private TestHandler testHandler;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        try {
            log.info("RedisReceiver onMessage:{}",new String(message.getBody(), StandardCharsets.UTF_8));
            String channel = new String(message.getChannel());
            String content = new String(message.getBody(), StandardCharsets.UTF_8);
            if (Constants.REDIS_CHANNEL.endsWith(channel)) {
                testHandler.sendMessageToClient(content);
            }
        } catch (Exception e) {
            log.error("RedisReceiver 消息处理失败",e);
        }
    }

}
java 复制代码
@Slf4j
@Component
public class TestHandler extends TextWebSocketHandler {

    private static final ConcurrentHashMap<String, WebSocketSession> SESSION_DISPATCH_HOLDER = new ConcurrentHashMap<>();


    @Resource
    private StringRedisTemplate stringRedisTemplate;



    @Override
    public void afterConnectionEstablished(@NonNull WebSocketSession session) throws Exception {
        super.afterConnectionEstablished(session);
        SESSION_DISPATCH_HOLDER.put(session.getId(), session);
        log.info("webSocket already connected:{}",session.getId());
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
        SESSION_DISPATCH_HOLDER.remove(session.getId());
        log.info("webSocket disConnect:{}",session.getId());
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) {
        log.info("handleMessage enter message:{}", JsonUtils.objToJsonStr(message.getPayload()));
        TextMessage textMessage = new TextMessage("");
        try {
            session.sendMessage(textMessage);
        } catch (IOException e) {
            log.error("发送消息失败", e);
        }
    }

 

  
    public void sendMessageToClient(String contentList) {
        log.info("sendMessageToClient:{}", contentList);
        if (CollectionUtils.isEmpty(SESSION_DISPATCH_HOLDER)) {
            log.info("没有连接中的客户端");
            return;
        }
        SESSION_DISPATCH_HOLDER.values().parallelStream().forEach(session -> {
            try {
                TextMessage textMessage = new TextMessage(contentList);
                session.sendMessage(textMessage);
                log.info("websocket 消息推送成功:{}", contentList);
            } catch (Exception e) {
                log.error("发送消息失败", e);
            }
        });
    }



}
相关推荐
刽子手发艺9 分钟前
WebSocket详解、WebSocket入门案例
网络·websocket·网络协议
登云时刻1 小时前
Kubernetes集群外连接redis集群和使用redis-shake工具迁移数据(二)
redis·容器·kubernetes
只因在人海中多看了你一眼3 小时前
分布式缓存 + 数据存储 + 消息队列知识体系
分布式·缓存
Dlwyz4 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
zhixingheyi_tian6 小时前
Spark 之 Aggregate
大数据·分布式·spark
工业甲酰苯胺6 小时前
Redis性能优化的18招
数据库·redis·性能优化
求积分不加C7 小时前
-bash: ./kafka-topics.sh: No such file or directory--解决方案
分布式·kafka
nathan05297 小时前
javaer快速上手kafka
分布式·kafka
Oak Zhang9 小时前
sharding-jdbc自定义分片算法,表对应关系存储在mysql中,缓存到redis或者本地
redis·mysql·缓存
门牙咬脆骨10 小时前
【Redis】redis缓存击穿,缓存雪崩,缓存穿透
数据库·redis·缓存