当前位置:网站首页>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的订阅发布服务来实现其他系统同集群的通信,完成集群的管理工作。
边栏推荐
- 零拷贝底层剖析
- The first character of leetcode sword offer that only appears once (12)
- [opengl] face pinching system
- The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!
- Global and Chinese market of transfer case 2022-2028: Research Report on technology, participants, trends, market size and share
- 4-20-4-23 concurrent server, TCP state transition;
- 4-33--4-35
- Finally, someone explained the financial risk management clearly
- Web server code parsing - thread pool
- NOI OPENJUDGE 1.3(06)
猜你喜欢
CentOS7部署哨兵Redis(带架构图,清晰易懂)
Functional modules and application scenarios covered by the productization of user portraits
【可能是全中文网最全】pushgateway入门笔记
[set theory] inclusion exclusion principle (complex example)
Composite type (custom type)
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
链表有环,快慢指针走3步可以吗
[pytorch learning notes] datasets and dataloaders
Unity hierarchical bounding box AABB tree
Yolov5 series (I) -- network visualization tool netron
随机推荐
Introduction to opengl4.0 tutorial computing shaders
4-33--4-35
TPS61170QDRVRQ1
4-29——4.32
[combinatorics] permutation and combination (set permutation, step-by-step processing example)
[ue4] geometry drawing pipeline
C language to implement a password manager (under update)
什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式
C # realizes the login interface, and the password asterisk is displayed (hide the input password)
C language DUP function
How does vs+qt set the software version copyright, obtain the software version and display the version number?
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Incluxdb2 buckets create database
Niuke bm83 string deformation (case conversion, string inversion, string replacement)
Zzuli:1059 highest score
5-1 blocking / non blocking, synchronous / asynchronous
Simulation of LS -al command in C language
Global and Chinese market of optical fiber connectors 2022-2028: Research Report on technology, participants, trends, market size and share
How to color ordinary landscape photos, PS tutorial
Unity hierarchical bounding box AABB tree