如何在没有redis的情况下缩放socket.io [英] How to scale socket.io without redis

查看:114
本文介绍了如何在没有redis的情况下缩放socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种替代方法来使用socket.io扩展我的快速应用程序。问题是我不想使用redis作为socket.io存储。除了 Clusterhub 之外,还有其他可能性来集群socket.io吗?

I'm currently searching for an alternative to scale my express app with socket.io. The problem is that I don't want to use redis as socket.io store. Are there any other possibilities to cluster socket.io except with Clusterhub?

编辑:我尝试使用 fakeredis 作为redis的替代品,但似乎它不适用于socket.io。从 ActionHero.js 我知道faye-websocket与fakeredis一起使用。

I tried to use fakeredis as replacement for redis, but it seems like it doesn't work with socket.io. From ActionHero.js I know that faye-websocket works with fakeredis.

推荐答案

这可能取决于您的socket.io使用情况和要实现的缩放类型(集群vs扩展到多台机器)。

This might well depends on your socket.io usage and the type of scaling you want to achieve (cluster vs scaling to multiple machines).

所以,这是我将socket.io的用法扩展到多个服务器。

So, here is what I did to scale our usage of socket.io to multiples servers.

在负载平衡器后面有3台服务器,当套接字连接到3台服务器中的任一台时,三台服务器都有一个内存列表,三台服务器有一个内部服务器地址的订单列表[server1,server2,server3]。

We have 3 servers behind a load balancer, when a socket connects it connect to any of the 3 servers, the three server has an in memory list of the sockets, and the three servers have an order list of internal server address e.g. [server1, server2, server3].

我所做的基本上是一个环(内部我们称之为套筒):

What I do basically is a ring (internally we call it the "ring of sockets"):


  • 如果我需要从server1发送一个事件到套接字,我先看看套接字是否连接到该server1,如果我没有向下一个发送http请求服务器(server2)将检查套接字是否存在,如果不存在,它将向server3发送相同的请求,依此类推,直到到达原点,在这种情况下您可能会发生错误。

  • 如果我需要广播一个消息,它几乎是一样的,我从一个服务器开始,然后调用其他的http端点。

我用来确定下一个节点(next_node.js)的算法是:

The algorithm I use to determine the next node (next_node.js) is:

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用例的扩展,可能还有很多其他的情况,这不是一个选项,但我希望这给了一些想法。

In my opinion this is a very easy way to achieve scaling in a lot of socket.io use cases, there might be a lot of other scenarios where this is not an option but I hope this give some ideas.

这篇关于如何在没有redis的情况下缩放socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆