当前位置:网站首页>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 .
边栏推荐
- Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)
- Tensor ellipsis (three points) slice
- High quality workplace human beings must use software to recommend, and you certainly don't know the last one
- SQL server installation location cannot be changed
- B2020 分糖果
- [ue4] Niagara's indirect draw
- Matlab r2011b neural network toolbox precautions
- 5-1 blocking / non blocking, synchronous / asynchronous
- Yolov5 advanced seven target tracking latest environment construction (II)
- Can‘t connect to MySQL server on ‘localhost‘
猜你喜欢

Zero copy underlying analysis

C string format (decimal point retention / decimal conversion, etc.)
![[pytorch learning notes] datasets and dataloaders](/img/c0/9cd539caff34db3cccc44505bbe3c5.png)
[pytorch learning notes] datasets and dataloaders
![[engine development] in depth GPU and rendering optimization (basic)](/img/71/abf09941eb06cd91784df50891fe29.jpg)
[engine development] in depth GPU and rendering optimization (basic)
![[probably the most complete in Chinese] pushgateway entry notes](/img/5a/6dcb75f5d713ff513ad6842ff53cc3.png)
[probably the most complete in Chinese] pushgateway entry notes
![[transform] [NLP] first proposed transformer. The 2017 paper](/img/33/f639ab527d5adedfdc39f8d8117c3e.png)
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team

redis缓存穿透,缓存击穿,缓存雪崩解决方案

ASTC texture compression (adaptive scalable texture compression)

基础SQL教程

Kubernetes带你从头到尾捋一遍
随机推荐
【pytorch学习笔记】Datasets and Dataloaders
. Net six design principles personal vernacular understanding, please correct if there is any error
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
[graphics] hair simulation in tressfx
Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
视觉上位系统设计开发(halcon-winform)-1.流程节点设计
QT program font becomes larger on computers with different resolutions, overflowing controls
406. Reconstruct the queue according to height
Detailed comments on MapReduce instance code on the official website
Leetcode the smallest number of the rotation array of the offer of the sword (11)
Using TCL (tool command language) to manage Tornado (for VxWorks) can start the project
[ue4] material and shader permutation
解决pushgateway数据多次推送会覆盖的问题
Didi off the shelf! Data security is national security
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
[pytorch learning notes] transforms
【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention
[combinatorics] permutation and combination (set permutation, step-by-step processing example)
【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
Global and Chinese market of trimethylamine 2022-2028: Research Report on technology, participants, trends, market size and share