【问题标题】:Sticky Session on HerokuHeroku 上的粘性会话
【发布时间】:2019-05-25 01:49:18
【问题描述】:

我们有一个使用 SocketIO 运行并在 heroku 上集群的 NodeJS 应用程序。为了让 SocketIO 工作,我们使用这里讨论的 redis-adapter:https://socket.io/docs/using-multiple-nodes/

然后我们实现了粘性会话,如此处的粘性会话文档所示:https://github.com/elad/node-cluster-socket.io

原来当我们部署到 Heroku 时,connection.remoteAddress 在:

// Create the outside facing server listening on our port.
var server = net.createServer({ pauseOnConnect: true }, function(connection) {
    // We received a connection and need to pass it to the appropriate
    // worker. Get the worker for this connection's source IP and pass
    // it the connection.
    var index = worker_index(connection.remoteAddress, num_processes);
    var worker = workers[index];
    worker.send('sticky-session:connection', connection);
}).listen(port);

实际上是一些heroku 路由服务器的IP 地址,而不是客户端IP。我已经看到可以使用请求头“x-forwarded-for”来获取客户端IP,但是当我们以这种方式暂停连接时,我们甚至还没有请求头?

【问题讨论】:

    标签: node.js heroku sticky-session


    【解决方案1】:

    我们到处寻找解决方案,但显然没有好的解决方案。

    以下是一些更好的建议:

    https://github.com/indutny/sticky-session/issues/6

    https://github.com/indutny/sticky-session/pull/45

    它们似乎都没有很好的性能,因此我们最终只将 SocketIO 通信更改为 Websockets。这消除了对粘性会话的需要。

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 2017-01-21
      • 2020-04-03
      • 2021-11-04
      • 2012-05-17
      • 1970-01-01
      • 2011-09-16
      • 2011-02-12
      相关资源
      最近更新 更多