当前位置:网站首页>socket.io搭建分布式Web推送服务器
socket.io搭建分布式Web推送服务器
2022-07-03 15:04:00 【星哥玩云】
socket.io是目前较为流行的web实时推送框架,其基于nodejs语言开发,底层用engine.io实现。 借助nodejs语言异步的特性,其获得了不错的性能。但单个实例的socket.io依然承载能力有限,最多只能容纳3000个long-polling方式的客户端进行连接。
1. 进行负载均衡时连接必须保证始终连到一个节点上
如果客户端采用long-polling长轮训方式进行连接,则每次轮训都会产生一个新的请求,若不进行限制。就有可能连接到集群内新的 socket.io节点上,导致异常的发生。
解决方法:使用nginx的ip_hash实现session sticky ,让客户端始终连接到集群内一台节点上。
2. 多个实例之间的消息推送
当集群内某台节点想要向连接到集群的所有客户端发送消息时,某些客户端因为负载均衡时ip_hash可能被分配到了其他的节点上,这时就需要向其他节点发布推送消息,让其他节点的同时向客户端进行推送。
解决方法:使用redis的发布与订阅功能与socket.io-redis开源库,实现节点间消息推送。
准备安装的软件:
nginx, nodejs, redis以及一个socket.io应用,如一个聊天服务器,例子请见官网这里。
具体步骤:
1.将socket.io应用部署成两个实例,如在同一台主机上为每个实例分配不同的端口号4000, 5000:
http.listen(4000, function(){ console.log('listening on *:4000'); });
2.配置nginx文件,设置负载均衡proxy
upstream chat_nodes { ip_hash; server 127.0.0.1:4000; server 127.0.0.1:5000; }
以及反向代理设置 (注意为了支持websocket协议,需将nginx升级至1.3.12版本以上
location / { proxy_pass http://chat_nodes; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
完成配置后,重启nginx。
3.安装nodejs模块 socket.io-redis
sudo npm install socket.io-redis
4.在原来socket.io应用中初始化io的位置加入io的redis适配器:
var redis = require('socket.io-redis'); io.adapter(redis({ host: 'localhost', port: 6379 }));
5. 重启各个socket.io应用,进行测试。
其他注意点:
由于nginx的反向代理机制和socket.io的自动重连机制,上述架构还具备高可用的特性,即当某个节点down机时,原先连接到该节点上的客户端会自动重连至其它节点上。
节点的数量可以随时增减,不需要暂停服务,只需修改nginx配置即可。
nginx的ip_hash是基于ip的前三段进行计算的,也就是说ip只有D段不同的两台客户端一定会连接到同一台服务器上,这点测试的时候需要注意。
可以通过redis的订阅发布服务来实现其他系统同集群的通信,完成集群的管理工作。
边栏推荐
- Several sentences extracted from the book "leather bag"
- [engine development] in depth GPU and rendering optimization (basic)
- Implement Gobang with C language
- Chapter 14 class part 1
- Composite type (custom type)
- 2021-10-16 initial programming
- Byte practice plane longitude 2
- My QT learning path -- how qdatetimeedit is empty
- Functional modules and application scenarios covered by the productization of user portraits
- NOI OPENJUDGE 1.5(23)
猜你喜欢

Troubleshooting method of CPU surge
![[opengl] advanced chapter of texture - principle of flowmap](/img/dd/6208122fcc578caaf098301b185e03.jpg)
[opengl] advanced chapter of texture - principle of flowmap

The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!

C language fcntl function

What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding

Série yolov5 (i) - - netron, un outil de visualisation de réseau

Yolov5 series (I) -- network visualization tool netron

C language to realize mine sweeping

【微信小程序】WXSS 模板样式

4-24--4-28
随机推荐
The first character of leetcode sword offer that only appears once (12)
Série yolov5 (i) - - netron, un outil de visualisation de réseau
TPS61170QDRVRQ1
Yolov5 advanced nine target tracking example 1
mmdetection 学习率与batch_size关系
C language fcntl function
[pytorch learning notes] datasets and dataloaders
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
Tencent internship interview sorting
Introduction to opengl4.0 tutorial computing shaders
Pytoch deep learning and target detection practice notes
运维体系的构建
[engine development] in depth GPU and rendering optimization (basic)
什么是Label encoding?one-hot encoding ,label encoding两种编码该如何区分和使用?
NOI OPENJUDGE 1.3(06)
What is one hot encoding? In pytoch, there are two ways to turn label into one hot coding
B2020 分糖果
Qt development - scrolling digital selector commonly used in embedded system
Neon global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share