javascript Node.js、多线程和 Socket.io
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8563401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js, multi-threading and Socket.io
提问by David Chouinard
I'm looking to get Socket.io to work multi-threaded with native load balancing("cluster") in Node.js v.0.6.0 and later.
我希望 Socket.io在 Node.js v.0.6.0 及更高版本中使用本机负载平衡(“集群”)进行多线程工作。
From what I understand, Socket.io uses Redis to store its internal data. My understanding is this: instead of spawning a new Redis instance for every worker, we want to force the workers to use the same Redis instance as the master. Thus, connection data would be shared across all workers.
据我了解,Socket.io 使用 Redis 来存储其内部数据。我的理解是:我们不想为每个工作人员生成一个新的 Redis 实例,而是希望强制工作人员使用与主设备相同的 Redis 实例。因此,连接数据将在所有工作人员之间共享。
Something like this in the master:
在大师中是这样的:
RedisInstance = new io.RedisStore;
The we must somehow pass RedisInstance
to the workers and do the following:
我们必须以某种方式传递RedisInstance
给工人并执行以下操作:
io.set('store', RedisInstance);
Inspired by this implementationusing the old, 3rd party cluster module, I have the following non-working implementation:
受此使用旧的第 3 方集群模块的实现的启发,我有以下非工作实现:
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
var sio = require('socket.io')
, RedisStore = sio.RedisStore
, io = sio.listen(8080, options);
// Somehow pass this information to the workers
io.set('store', new RedisStore);
} else {
// Do the work here
io.sockets.on('connection', function (socket) {
socket.on('chat', function (data) {
socket.broadcast.emit('chat', data);
})
});
}
Thoughts? I might be going completely in the wrong direction, anybody can point to some ideas?
想法?我可能完全走错了方向,有人可以指出一些想法吗?
采纳答案by alessioalex
Actually your code should look like this:
实际上你的代码应该是这样的:
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
var sio = require('socket.io')
, RedisStore = sio.RedisStore
, io = sio.listen(8080, options);
// Somehow pass this information to the workers
io.set('store', new RedisStore);
// Do the work here
io.sockets.on('connection', function (socket) {
socket.on('chat', function (data) {
socket.broadcast.emit('chat', data);
})
});
}
Another option is to open Socket.IO to listen on multiple ports and have something like HAProxy load-balance stuff. Anyway you know the most important thing: using RedisStore to scale outside a process!
另一种选择是打开 Socket.IO 以侦听多个端口并使用 HAProxy 负载平衡之类的东西。无论如何,您知道最重要的事情:使用 RedisStore 在进程外扩展!
Resources:
资源:
http://nodejs.org/docs/latest/api/cluster.html
How can I scale socket.io?
How to reuse redis connection in socket.io?
Node: Scale socket.io / nowjs - scale across different instances
http://delicious.com/alessioaw/socket.io
http://nodejs.org/docs/latest/api/cluster.html
如何扩展 socket.io?
如何在 socket.io 中重用 redis 连接?
节点:扩展 socket.io / nowjs - 跨不同实例扩展
http://delicious.com/alessioaw/socket.io