当前位置:网站首页>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 .
边栏推荐
- SQL server installation location cannot be changed
- There are links in the linked list. Can you walk three steps faster or slower
- TPS61170QDRVRQ1
- 视觉上位系统设计开发(halcon-winform)-5.相机
- Tensor ellipsis (three points) slice
- C # realizes the login interface, and the password asterisk is displayed (hide the input password)
- Global and Chinese market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of Bus HVAC systems 2022-2028: Research Report on technology, participants, trends, market size and share
- . Net six design principles personal vernacular understanding, please correct if there is any error
- Composite type (custom type)
猜你喜欢

【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现

What is machine reading comprehension? What are the applications? Finally someone made it clear

The state does not change after the assignment of El switch
![[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention](/img/94/a9c7010fe9f14454469609ac4dd871.png)
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention

4-20-4-23 concurrent server, TCP state transition;

Yolov5系列(一)——网络可视化工具netron

Vs+qt application development, set software icon icon
![[engine development] in depth GPU and rendering optimization (basic)](/img/71/abf09941eb06cd91784df50891fe29.jpg)
[engine development] in depth GPU and rendering optimization (basic)

【可能是全中文网最全】pushgateway入门笔记

5.4-5.5
随机推荐
B2020 分糖果
Explanation of time complexity and space complexity
Kubernetes 进阶训练营 Pod基础
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Chapter 14 class part 1
[opengl] advanced chapter of texture - principle of flowmap
什么是Label encoding?one-hot encoding ,label encoding两种编码该如何区分和使用?
4-24--4-28
Global and Chinese markets of AC electromechanical relays 2022-2028: Research Report on technology, participants, trends, market size and share
Tencent internship interview sorting
[pytorch learning notes] datasets and dataloaders
Pytoch deep learning and target detection practice notes
Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
TPS61170QDRVRQ1
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
The first character of leetcode sword offer that only appears once (12)
视觉上位系统设计开发(halcon-winform)
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
Nppexec get process return code
Zero copy underlying analysis