当前位置:网站首页>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的订阅发布服务来实现其他系统同集群的通信,完成集群的管理工作。
边栏推荐
- [Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
- Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text
- How to color ordinary landscape photos, PS tutorial
- 【pytorch学习笔记】Transforms
- Fundamentals of PHP deserialization
- What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding
- Qt development - scrolling digital selector commonly used in embedded system
- C string format (decimal point retention / decimal conversion, etc.)
- Write a 2-minute countdown.
- el-switch 赋值后状态不变化
猜你喜欢
Vs+qt application development, set software icon icon
[Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
[graphics] real shading in Unreal Engine 4
远程服务器后台挂起 nohup
How to color ordinary landscape photos, PS tutorial
Rasterization: a practical implementation (2)
Tencent internship interview sorting
Yolov5系列(一)——網絡可視化工具netron
QT - draw something else
There are links in the linked list. Can you walk three steps faster or slower
随机推荐
How to color ordinary landscape photos, PS tutorial
【微信小程序】WXSS 模板样式
Global and Chinese markets for infrared solutions (for industrial, civil, national defense and security applications) 2022-2028: Research Report on technology, participants, trends, market size and sh
Bucket sorting in C language
链表有环,快慢指针走3步可以吗
4-20-4-23 concurrent server, TCP state transition;
NOI OPENJUDGE 1.5(23)
C language to realize mine sweeping
Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
Troubleshooting method of CPU surge
Joomla! CMS 3.0~3.4.6 RCE
Neon global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for sterile packaging 2022-2028: Research Report on technology, participants, trends, market size and share
运维体系的构建
Explanation of time complexity and space complexity
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
What is machine reading comprehension? What are the applications? Finally someone made it clear
TPS61170QDRVRQ1
There are links in the linked list. Can you walk three steps faster or slower