WebSocket多服务实例下的消息推送

最近在做一个项目,涉及到前后端的消息同步、推送,进而我们选择使用webSocket的方案进行实现,但是当websocket服务端部署在多个实例下,会出现前端socket意外断开导致无法收到消息的情况。手下我们先说我们的实现方案:

1.客户端(通过websocket,与后端的wensocket服务端建立链接,)请注意因为后端为多实例使用nginx进行负载均衡,需要注意的是ngx按照如下配置,负责无法进行正常的链接

server {

listen 86;

server_name 127.0.0.1;

location /websocket/222 {

proxy_connect_timeout 20s;

proxy_send_timeout 60s;

proxy_read_timeout 60s;

proxy_redirect off;

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

proxy_set_header Host host:server_port;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://websocket_backend;

}

proxy_set_header Host host:server_port;这个配置很重要,如果不配置会出现websocket无法链接。

2.注意服务端接收到客户端的心跳消息,不能使用异步推送的方式,这样会导致链接断开

复制代码
@OnMessage
public void onMessage(String message, Session session) {
    System.out.println("接收到消息:" + message);
    // 处理收到的消息
    // 可以通过session发送消息给客户端
    try {
        Map map = new HashMap();
        map.put("Received: ","8092");
        //session.getBasicRemote().sendText("Received: " + message);
        JSONObject jsonObject = new JSONObject(map);
        //session.getBasicRemote().sendText("Received: " + message);
        session.getBasicRemote().sendObject(jsonObject);
        //session.getBasicRemote().sendObject(map);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

3。因为使用的mq进行广播消息,使用exchange的机制,可以保证所有消费者只要是绑定到了对应的exchange上的队列都能消费,来解决多实例下的消息推送问题

复制代码
@RabbitHandler
@RabbitListener(bindings = @QueueBinding(value = @Queue(),
        exchange = @Exchange(value = "com.jihaixiang.test",
                type = ExchangeTypes.FANOUT)), concurrency = "10")
public void receiveMessage(String message) {
    System.out.println("Received message3: " + message);
}

4.在往mq中发送消息的时候需要保持routekeying为空,这样所有消费者都能收到消息

复制代码
rabbitTemplate.convertAndSend("com.jihaixiang.test", "", "测试消息");

5.创建exchange

复制代码
package com.jihaixiang.bootmybatis.configure;

import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author jihaixiang
 * @date 2024-03-12 22:29
 */
@Configuration
public class RabbitConfig {


    /**
     * 定义交换机
     * @return
     */
    @Bean
    public FanoutExchange orderWebsocketSendMessageExchange() {
        return new FanoutExchange("com.jihaixiang.test");
    }

}
相关推荐
byoass5 小时前
企业云盘文件预览技术深度剖析:从10种常见格式到渲染架构实战
网络·安全·架构·云计算
TechWayfarer7 小时前
知乎/微博的IP属地显示为什么偶尔错误?用IP归属地查询平台自检工具3步验证
网络·python·网络协议·tcp/ip·网络安全
Wave8457 小时前
从单片机开发看透网络底层:Wi-Fi、TCP/IP 与 HTTP 的通俗解析
网络·单片机·tcp/ip
ZYH_06017 小时前
园区网络实验作业
网络
疯狂的代M夫8 小时前
网络通信流程
网络
小江的记录本8 小时前
【网络安全】《网络安全常见攻击与防御》(附:《六大攻击核心特性横向对比表》)
java·网络·人工智能·后端·python·安全·web安全
2401_873479409 小时前
应急响应:勒索软件攻击源IP分析,如何通过IP地址查询定位辅助溯源?
网络·tcp/ip·安全·网络安全·ip
nibabaoo10 小时前
前端开发攻略---H5页面手机获取摄像头权限回显出画面并且同步到PC页面
javascript·websocket·实时音视频·实时同步·录制
拾薪11 小时前
[SuperPower] Brainingstorm - 流程控制架构分析
网络·人工智能·ai·架构·superpower·brainstorming
IMPYLH11 小时前
Linux 的 rm 命令
linux·运维·服务器·网络·bash