在多核服务器中将 socket.io 与集群一起使用的好方法? [英] Good way to use socket.io with cluster in multi-core server?
问题描述
我创建了一个在单核中运行良好的 nodejs 应用程序.此应用程序使用 socket.io 进行轻松快速的通信.
I have created a nodejs application which works fine in a single core. This application uses socket.io for light&fast communication.
现在,我想垂直扩展我的应用程序,通过使用 nodejs 中的 cluster
模块使其能够在多核系统上运行.
Now, I wanted to scale my application vertically, by enabling it to run on multi-core system using cluster
module in nodejs.
我的应用程序中的所有内容都将数据存储在 Redis 中,因此,处理一些基本数据没有问题&会议.
Everything in my application stores data in Redis, therefore, there isn't a problem dealing with some basic data & sessions.
然而,当我使用集群产生多个工作进程时,似乎每个工作人员都有自己的套接字处理.
However, when I spawn multiple worker processes using cluster, it seems that each worker has its own socket handling.
例如,假设有一个名为客人"的聊天室.
For example, lets assume there is a chat room called 'guest' room.
用户 A 和用户 B 连接到房间,他们被分配到不同的工作进程.
User A and user B connects to the room, and they are distributed to the different worker process.
由于它们在不同的进程中,并且这些进程不共享套接字侦听器,因此用户 A 和用户 B 无法相互交谈.
Since they are in different processs and these processes do not share sockets listener, there is no way for user A and user B to talk to each other.
解决这个问题的好方法是什么?socket.io 是否支持多核系统?
What is a good approach to solve this problem? Does socket.io supports multi-core system?
socket.io 是否只能用于单核?
Is socket.io only for single core use?
推荐答案
您可以照常使用 socket.io,但在后台使用 redis 存储.它还将支持 socket.io 上的多个实例,而无需任何外部库.它甚至支持跨多个实例的房间.
You could use socket.io as normal but with a redis store in the background. It will also support multiple instances on socket.io without any external library. It even supports rooms over multiple instances.
如何使用 redis 设置 socket.io 的链接:Using Multiples Nodes/Processes with socket.io
Link to how to set up socket.io with redis: Using Multiples Nodes/Processes with socket.io
这篇关于在多核服务器中将 socket.io 与集群一起使用的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!