23

好吧,我已经花了一个多星期的时间试图弄清楚这一点,但无济于事,所以如果有人有线索,你就是英雄。这不是一个容易回答的问题,除非我是个笨蛋。

我正在使用 node-http-proxy 将粘性会话代理到在不同端口上运行的 16 个 node.js 工作人员。

我使用 Socket.IO 的 Web Sockets 来处理一堆不同类型的请求,也使用传统的请求。

当我通过 node-http-proxy 将服务器切换到代理时,有时会出现一个新问题,我的 Socket.IO 会话无法建立连接。

我真的无法在我的一生中稳定地复制它,唯一的方法是将它从多个客户端抛出大量流量到服务器。

如果我重新加载用户的浏览器,它有时可以重新连接,有时不能。

粘性会话

我必须代理粘性会话,因为我的应用程序基于每个工作人员进行身份验证,因此它根据其 Connect.SID cookie 路由请求(我正在使用 connect/express)。

好的,一些代码

这是我的 proxy.js 文件,它在节点中运行并路由到每个工作人员:

var http = require('http');
var httpProxy = require('http-proxy');

// What ports the proxy is routing to.
var data = {
  proxyPort: 8888,
  currentPort: 8850,
  portStart: 8850,
  portEnd: 8865,
};

// Just gives the next port number.
nextPort = function() {
  var next = data.currentPort++;
  next = (next > data.portEnd) ? data.portStart : next;
  data.currentPort = next;
  return data.currentPort;
};

// A hash of Connect.SIDs for sticky sessions.
data.routes = {}

var svr = httpProxy.createServer(function (req, res, proxy) {

  var port = false;

  // parseCookies is just a little function
  // that... parses cookies.
  var cookies = parseCookies(req);  

  // If there is an SID passed from the browser.
  if (cookies['connect.sid'] !== undefined) {

    var ip = req.connection.remoteAddress;

    if (data.routes[cookies['connect.sid']] !== undefined) {

      // If there is already a route assigned to this SID,
      // make that route's port the assigned port.
      port = data.routes[cookies['connect.sid']].port;
    } else {

      // If there isn't a route for this SID,
      // create the route object and log its
      // assigned port.
      port = data.currentPort;
      data.routes[cookies['connect.sid']] = {
        port: port,
      }

      nextPort();
    }

  } else {

    // Otherwise assign a random port, it will/
    // pick up a connect SID on the next go.
    // This doesn't really happen.
    port = nextPort();
  }

  // Now that we have the chosen port, 
  // proxy the request.
  proxy.proxyRequest(req, res, {
    host: '127.0.0.1',
    port: port
  });
}).listen(data.proxyPort);

// Now we handle WebSocket requests.
// Basically, I feed off of the above route
// logic and try to route my WebSocket to the
// same server regular requests are going to.
svr.on('upgrade', function (req, socket, head) {

  var cookies = parseCookies(req);  
  var port = false;

  // Make sure there is a Connect.SID,
  if (cookies['connect.sid'] != undefined) {

    // Make sure there is a route...
    if (data.routes[cookies['connect.sid']] !== undefined) {

      // Assign the appropriate port.
      port = data.routes[cookies['connect.sid']].port;
    } else {

      // this has never, ever happened, i've been logging it.
    }
  } else {

    // this has never, ever happened, i've been logging it.
  };

  if (port === false) {

    // this has never happened...
  };

  // So now route the WebSocket to the same port
  // as the regular requests are getting.
  svr.proxy.proxyWebSocketRequest(req, socket, head, {
    host: 'localhost',
    port: port
  });

});

客户端/现象

套接字连接如下:

var socket = io.connect('http://whatever:8888');

登录大约 10 秒后,我在此侦听器上收到此错误,这并没有多大帮助。

socket.on('error', function (data) {
  // this is what gets triggered. ->
  // Firefox can't establish a connection to the server at ws://whatever:8888/socket.io/1/websocket/Nnx08nYaZkLY2N479KX0.
});

浏览器发送的 Socket.IO GET 请求永远不会回来 - 它只是挂起等待,即使在错误回来之后,所以它看起来像一个超时错误。服务器从不响应。

服务器端 - 工作人员

这就是工作人员接收套接字请求的方式。很简单。所有工人都有相同的代码,所以你认为他们中的一个会收到请求并确认它......

app.sio.socketio.sockets.on('connection', function (socket) {
  // works... some of the time! all of my workers run this
  // exact same process.
});

概括

这是很多数据,我怀疑是否有人愿意面对它,但我完全被难住了,不知道接下来在哪里检查,下一步记录,无论如何,要解决它。我已经尝试了我所知道的一切以查看问题所在,但无济于事。

更新

好的,我相当确定问题出在node-http-proxy github 主页上的以下声明中:

node-http-proxy 与 <= 0.8.x 兼容,如果您正在寻找 >= 0.10 兼容版本,请查看 caronte

我正在运行 Node.js v0.10.13,这种现象与一些人在 github 问题上对此主题的评论完全相同:它只是随机丢弃 websocket 连接。

我已经尝试实现 caronte,即“较新”的分支,但它根本没有记录,我已经尽我最大的努力将他们的文档拼凑成一个可行的解决方案,但我无法让它转发 websockets,我的 Socket。 IO 降级为轮询。

关于如何实现和工作还有其他想法吗?node-http-proxy 昨天有 8200 次下载!肯定有人在使用今年的 Node 版本并代理 websockets....

我到底在寻找什么

我想完成一个代理服务器(最好是 Node),它代理多个 node.js 工作人员,并通过基于浏览器 cookie 的粘性会话路由请求。该代理需要稳定地支持传统请求以及 Web 套接字。

或者...

如果可行,我不介意通过集群节点工作人员完成上述任务。我唯一真正的要求是根据请求标头中的 cookie 维护粘性会话。

如果有比我正在尝试的更好的方法来完成上述工作,我完全赞成。

4

3 回答 3

4

一般来说,我不认为 node 不是最常用的代理服务器选项,我使用nginx作为 node 的前端服务器,这是一个非常好的组合。以下是安装和使用 nginx 粘性会话模块的一些说明。

这是一个轻量级的前端服务器,具有类似 json 的配置,可靠且经过很好的测试。

如果您想提供静态页面 css,nginx 也快得多。配置缓存标头、根据域、粘性会话、压缩 css 和 javascript 等将流量重定向到多个服务器是理想的选择。

您还可以考虑像HAProxy这样的纯负载平衡开源解决方案。无论如何,我不相信 node 是最好的工具,最好只用它来实现你的后端,并在它前面放置像 nginx 这样的东西来处理通常的前端服务器任务。

于 2013-12-08T20:33:31.330 回答
3

我同意六氰化物。对我来说,通过像 redis 或某种消息查询系统这样的服务对工作人员进行排队是最有意义的。工作人员将通过 Web 节点(被代理)通过 Redis Pub/Sub 功能排队。工作人员将通过“数据”事件实时回调错误、完成或流数据。也许看看图书馆kue。您也可以推出自己的类似库。RabbitMQ 是另一个用于类似目的的系统。

如果您已经在使用该技术,我会使用 socket.io,但您需要使用工具来达到预期目的。Redis 或 MQ 系统将是最有意义的,并与 websockets(socket.io) 完美搭配以创建实时、有洞察力的应用程序。

通过 Elastic LoadBalancer for aws 支持会话关联(粘性会话),这支持 webSockets。PaaS 提供商 ( Modulus ) 正是这样做的。还有satalite为 node-http-proxy 提供粘性会话,但是我不知道它是否支持 webSockets。

于 2013-12-08T03:28:10.513 回答
2

我自己一直在研究与此非常相似的东西,目的是动态生成(和销毁)Node.js 集群节点。

免责声明:我仍然不建议使用 Node 执行此操作;对于您正在寻找的那种设计架构,nginx 更稳定,甚至更稳定的是 HAProxy(非常成熟,并且很容易支持粘性会话代理)。正如@tsturzl 所指出的,有satellite,但鉴于下载量较低,我会小心行事(至少在生产环境中)。

也就是说,由于您似乎已经使用 Node 设置了所有内容,因此重建和重新架构可能比它的价值更多。因此,要caronte使用 NPM 安装分支:

  1. 使用和/或删除您以前的http-node-proxy主安装npm uninstall node-proxysudo npm -d uninstall node-proxy

  2. 下载caronte分支 .zip 并将其解压缩。

  3. npm -g install /path/to/node-http-proxy-caronte
  4. 就我而言,安装链接已损坏,因此我不得不运行sudo npm link http-proxy

我已经使用他们的基本代理示例启动并运行它——这是否解决了您的会话丢失问题,只有您自己知道。

于 2013-12-11T17:02:08.090 回答