当前位置:网站首页>socket. IO build distributed web push server
socket. IO build distributed web push server
2022-07-03 15:12:00 【Brother Xing plays with the clouds】
socket.io It is more popular at present web Real-time push frame , It is based on nodejs Language development , The underlying use engine.io Realization . With the help of nodejs The asynchronous nature of language , It has achieved good performance . But for a single instance socket.io The carrying capacity is still limited , It can only accommodate at most 3000 individual long-polling Way of client connection .
take socket.io Conduct Distributed There are two difficulties in expansion :
1. When load balancing, the connection must always be connected to a node
If the client uses long-polling Connect by long rotation training , Then each rotation training will produce a new request , If there is no restriction . It is possible to connect to colony New in socket.io Node , Cause an exception to occur .
resolvent : Use nginx Of ip_hash Realization session sticky , Let the client always connect to colony On the inner node .
2. Message push between multiple instances
When colony A node in wants to connect to colony When all clients of send messages , When some clients are load balanced ip_hash May be assigned to other nodes , At this time, you need to publish push messages to other nodes , Let other nodes push to the client at the same time .
resolvent : Use redis The publish and subscribe function of is similar to socket.io-redis Open source library , Realize message push between nodes .
Ready to install software :
nginx, nodejs, redis And one. socket.io application , Like a chat The server , See the official website here for an example .
Specific steps :
1. take socket.io The application is deployed into two instances , For example, assign different port numbers to each instance on the same host 4000, 5000:
http.listen(4000, function(){ console.log('listening on *:4000'); });
2. To configure nginx file , Set up load balancing proxy
upstream chat_nodes { ip_hash; server 127.0.0.1:4000; server 127.0.0.1:5000; }
And reverse proxy settings ( Note that in order to support websocket agreement , Need to nginx To upgrade to 1.3.12 Above version
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; }
After configuration , restart nginx.
3. install nodejs modular socket.io-redis
sudo npm install socket.io-redis
4. In the original socket.io Initialization in the application io Join at io Of redis Adapter :
var redis = require('socket.io-redis'); io.adapter(redis({ host: 'localhost', port: 6379 }));
5. Restart each socket.io application , To test .
Other considerations :
because nginx Reverse proxy mechanism and socket.io Automatic reconnection mechanism , The above architecture also has high availability , That is, when a node down operation period , Clients previously connected to this node will automatically reconnect to other nodes .
The number of nodes can be increased or decreased at any time , There is no need to suspend service , Only need to modify nginx The configuration can be .
nginx Of ip_hash Is based on ip Calculated in the first three paragraphs of , in other words ip Only D Two clients with different segments must be connected to the same The server On , You should pay attention to this when testing .
Can pass redis Subscription and publishing services to achieve the same with other systems colony Communication for , Complete the management of the cluster .
边栏推荐
- What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry
- Yolov5 advanced 8 format conversion between high and low versions
- Matlab r2011b neural network toolbox precautions
- Dataframe returns the whole row according to the value
- Solve the problem that pushgateway data will be overwritten by multiple push
- 4-20-4-23 concurrent server, TCP state transition;
- Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
- 在MapReduce中利用MultipleOutputs输出多个文件
- TPS61170QDRVRQ1
- Halcon与Winform学习第一节
猜你喜欢
Incluxdb2 buckets create database
[opengl] advanced chapter of texture - principle of flowmap
【pytorch学习笔记】Datasets and Dataloaders
Matplotlib drawing label cannot display Chinese problems
【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team
Construction of operation and maintenance system
Série yolov5 (i) - - netron, un outil de visualisation de réseau
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
随机推荐
ASTC texture compression (adaptive scalable texture compression)
406. Reconstruct the queue according to height
Global and Chinese market of transfer case 2022-2028: Research Report on technology, participants, trends, market size and share
官网MapReduce实例代码详细批注
redis缓存穿透,缓存击穿,缓存雪崩解决方案
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
B2020 分糖果
高并发下之redis锁优化实战
Yolov5系列(一)——網絡可視化工具netron
视觉上位系统设计开发(halcon-winform)-6.节点与宫格
[ue4] cascading shadow CSM
Yolov5 series (I) -- network visualization tool netron
[opengl] advanced chapter of texture - principle of flowmap
[pytorch learning notes] transforms
Kubernetes带你从头到尾捋一遍
Devaxpress: range selection control rangecontrol uses
Redis主从、哨兵、集群模式介绍
【日常训练】395. 至少有 K 个重复字符的最长子串
什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式