当前位置:网站首页>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 .
边栏推荐
- Global and Chinese market of transfer case 2022-2028: Research Report on technology, participants, trends, market size and share
- Construction of operation and maintenance system
- Incluxdb2 buckets create database
- Functional modules and application scenarios covered by the productization of user portraits
- 2022/02/14
- Global and Chinese market of optical fiber connectors 2022-2028: Research Report on technology, participants, trends, market size and share
- TPS61170QDRVRQ1
- Byte practice surface longitude
- CentOS7部署哨兵Redis(带架构图,清晰易懂)
- Global and Chinese markets for flexible chips 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Devaxpress: range selection control rangecontrol uses
![[probably the most complete in Chinese] pushgateway entry notes](/img/5a/6dcb75f5d713ff513ad6842ff53cc3.png)
[probably the most complete in Chinese] pushgateway entry notes

How does vs+qt set the software version copyright, obtain the software version and display the version number?

The state does not change after the assignment of El switch
![[set theory] inclusion exclusion principle (complex example)](/img/9a/db5a75e27516378c31531773a8a221.jpg)
[set theory] inclusion exclusion principle (complex example)

Byte practice surface longitude

Dataframe returns the whole row according to the value

Idea does not specify an output path for the module

Incluxdb2 buckets create database
![[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller](/img/a4/2156b61fbf50db65fdf59c8f5538f8.png)
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
随机推荐
Explanation of time complexity and space complexity
Search in the two-dimensional array of leetcode sword offer (10)
Remote server background hangs nohup
Global and Chinese market of trimethylamine 2022-2028: Research Report on technology, participants, trends, market size and share
Chapter 04_ Logical architecture
Redis cache penetration, cache breakdown, cache avalanche solution
【日常训练】395. 至少有 K 个重复字符的最长子串
Troubleshooting method of CPU surge
App全局异常捕获
4-29——4.32
视觉上位系统设计开发(halcon-winform)
[combinatorics] permutation and combination (set permutation, step-by-step processing example)
Kubernetes will show you from beginning to end
Leetcode sword offer find the number I (nine) in the sorted array
Construction of operation and maintenance system
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
[graphics] adaptive shadow map
TPS61170QDRVRQ1
Global and Chinese markets for flexible chips 2022-2028: Research Report on technology, participants, trends, market size and share
TPS61170QDRVRQ1