当前位置:网站首页>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的订阅发布服务来实现其他系统同集群的通信,完成集群的管理工作。
边栏推荐
- Global and Chinese market of lighting control components 2022-2028: Research Report on technology, participants, trends, market size and share
- B2020 分糖果
- Remote server background hangs nohup
- [opengl] geometry shader
- SQL server安装位置改不了
- Mmdetection learning rate and batch_ Size relationship
- 4-29——4.32
- Zero copy underlying analysis
- 2021-10-16 initial programming
- Leetcode sword offer find the number I (nine) in the sorted array
猜你喜欢

How to color ordinary landscape photos, PS tutorial

【pytorch学习笔记】Datasets and Dataloaders
![[set theory] inclusion exclusion principle (complex example)](/img/9a/db5a75e27516378c31531773a8a221.jpg)
[set theory] inclusion exclusion principle (complex example)

什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用

How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03

什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式

What is machine reading comprehension? What are the applications? Finally someone made it clear

B2020 分糖果

cpu飙升排查方法
![[ue4] HISM large scale vegetation rendering solution](/img/a2/2ff2462207e3c3e8364a092765040c.jpg)
[ue4] HISM large scale vegetation rendering solution
随机推荐
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Global and Chinese market of solder bars 2022-2028: Research Report on technology, participants, trends, market size and share
[Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
Influxdb2 sources add data sources
Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
[opengl] bone animation blending effect
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
[combinatorics] permutation and combination (set combination, one-to-one correspondence model analysis example)
NOI OPENJUDGE 1.4(15)
Global and Chinese market of Bus HVAC systems 2022-2028: Research Report on technology, participants, trends, market size and share
Joomla! CMS 3.0~3.4.6 RCE
链表有环,快慢指针走3步可以吗
Global and Chinese market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
Neon global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
【pytorch学习笔记】Transforms
Finally, someone explained the financial risk management clearly
[opengl] advanced chapter of texture - principle of flowmap
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
Pytoch deep learning and target detection practice notes
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03