我正在寻找替代方案来使用socket.io来缩放我的快车应用程序。问题是我不想使用redis作为socket.io存储。除了使用Clusterhub之外,是否还有其他可能性将socket.io聚簇?如何在没有redis的情况下缩放socket.io
编辑:我试图用fakeredis作为替代Redis的,但现在看来似乎不socket.io工作。从ActionHero.js我知道faye-websocket和fakeredis一起工作。
我正在寻找替代方案来使用socket.io来缩放我的快车应用程序。问题是我不想使用redis作为socket.io存储。除了使用Clusterhub之外,是否还有其他可能性将socket.io聚簇?如何在没有redis的情况下缩放socket.io
编辑:我试图用fakeredis作为替代Redis的,但现在看来似乎不socket.io工作。从ActionHero.js我知道faye-websocket和fakeredis一起工作。
这很可能取决于你的socket.io使用和扩展你想实现(集群VS扩展到多台机器)的类型。
所以,这里是我做了什么来扩展我们的套接字使用到多个服务器。
我们在负载均衡器后面有3个服务器,当一个套接字连接它时,它连接到3个服务器中的任何一个,三个服务器有一个内存中的套接字列表,并且这三个服务器有一个内部服务器地址例如[server1,server2,server3]。
我做的基本上是环(内部我们称之为“插座环”):
我使用以确定下一个节点(next_node.js)的算法为:
var nodes = process.env.NODES.split(',');
//this is usually: http://server1/,http://server2/,http://server3/
var url = require('url');
var current = require("os").hostname();
//origin is the node that started the lookup
exports.get = function (origin) {
var next_node_i = nodes.map(function (uri) {
return url.parse(uri).hostname;
}).reduce(function (prev, curr, i, arr){
return curr === current && i < arr.length - 1 ? i + 1 : prev;
}, 0);
var next_node = nodes[next_node_i];
if (origin && url.parse(next_node).hostname === origin) {
// if the next node is equal to the first node initiating the lookup
// it means the socket we are looking for is not connect to any node.
return null;
}
return next_node;
};
注意事项:
在我看来,这是一个非常简单的方法,实现了很多socket.io用例缩放,可能有很多其他的场景中,这是不是一种选择,但我希望这给一些想法。
你不使用Redis而不是实现自己的算法的原因是什么? –
如果你熟悉Azure的服务,一些在Azure团队的家伙已经去写service bus store for socket.io的自由。
redis有什么问题? – TheHippo
redis没有问题。我只是不想为我的节点应用程序提供任何专有软件,因为我想将其作为节点webkit应用程序提供。 – fraherm
Redis是开源的,其他任何东西都是专有的。如果你说你想把它作为node-webkit应用程序来传递,我会假设它将被部署到桌面PC上。 Node和socket.io能够处理10.000甚至更多的套接字连接。你确定你需要更多的方式吗? – TheHippo