当前位置:网站首页>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的订阅发布服务来实现其他系统同集群的通信,完成集群的管理工作。
边栏推荐
- 【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
- Devaxpress: range selection control rangecontrol uses
- Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text
- Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial
- Joomla! CMS 3.0~3.4.6 RCE
- Global and Chinese market of trimethylamine 2022-2028: Research Report on technology, participants, trends, market size and share
- [engine development] in depth GPU and rendering optimization (basic)
- QT program font becomes larger on computers with different resolutions, overflowing controls
- Vs+qt application development, set software icon icon
- [opengl] face pinching system
猜你喜欢

5.4-5.5

The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
![[graphics] hair simulation in tressfx](/img/41/cef55811463d3a25a29ddab5278af0.jpg)
[graphics] hair simulation in tressfx

C language to realize mine sweeping
![[ue4] HISM large scale vegetation rendering solution](/img/a2/2ff2462207e3c3e8364a092765040c.jpg)
[ue4] HISM large scale vegetation rendering solution

PS tips - draw green earth with a brush

QT - draw something else

Bucket sorting in C language
![[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018](/img/2b/b23aeab584f89be6678c0fe059d4b6.png)
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018

Yolov5系列(一)——网络可视化工具netron
随机推荐
Pytoch deep learning and target detection practice notes
Simulation of LS -al command in C language
【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
Open under vs2019 UI file QT designer flash back problem
Fundamentals of PHP deserialization
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team
Global and Chinese market of optical fiber connectors 2022-2028: Research Report on technology, participants, trends, market size and share
使用Tengine解决负载均衡的Session问题
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
Incluxdb2 buckets create database
[set theory] inclusion exclusion principle (complex example)
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Tensor ellipsis (three points) slice
Unity hierarchical bounding box AABB tree
4-33--4-35
Global and Chinese market of air cargo logistics 2022-2028: Research Report on technology, participants, trends, market size and share
[graphics] real shading in Unreal Engine 4
What is label encoding? How to distinguish and use one hot encoding and label encoding?
Troubleshooting method of CPU surge
5-1 blocking / non blocking, synchronous / asynchronous