没有共享内存存储的负载平衡 socket.io 服务器的解决方法

发布于 2025-01-14 02:53:37 字数 647 浏览 4 评论 0原文

我有一个 node.js / socket.io 应用程序需要在负载平衡服务器上运行。服务器没有启用粘性会话,因此我无法使用 Redis 之类的东西为 socket.io 创建共享内存存储。

socket.io 网站建议按照所附插图进行操作(取自 此处)

可能的服务器设置的图示

我的问题是你如何实际实现这个?

从逻辑上讲,这是有道理的 - 在所有服务器之间建立连接,以便它们可以在彼此之间传递任何消息。是否已经有一个节点模块可以做到这一点,或者我需要提出自己的逻辑?这是否需要在每个服务器之间创建额外的套接字连接?因此知道每个服务器的内部 IP 以便从每个其他服务器连接到它们?

我知道这不是推荐的工作流程,而且绝对不理想。我觉得这个解决方案可能相当复杂。但如果有人提出实现这一目标的最佳方法的建议,那将会有所帮助。谢谢!

I have a node.js / socket.io app that needs to run on a load balanced server. The server does not have sticky sessions enabled, so I cannot use something such as Redis to create a shared memory store for socket.io.

The socket.io website suggests doing something as per the attached illustration (taken from here)

illustration of a possible server setup

My question is how could you go about actually implementing this?

Logically it makes sense - set up a connection between all the servers so they can pass any messages between themselves. Is there a node module already that can do this, or do I need to come up with my own logic? Would this require creating an additional socket connection between every server? And therefore knowing the internal IP of each server in order to connect to them all from every other server?

I know this isn't a recommended workflow and definitely isn't ideal. I feel like the solution may prove fairly overcomplicated. But if anyone has a suggestion of the best way to achieve this, that would be helpful. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕色琉璃 2025-01-21 02:53:37

这不是您要问的,但如果您禁用 HTTP 长轮询传输,则不需要粘性会话,根据您的链接:

如果您禁用 HTTP 长轮询传输(这在 2021 年是一个完全有效的选择),您将不需要粘性会话:

这就是 Socket.IO 对于长轮询的说法,因此请记住这一点:

默认情况下,客户端与 HTTP 长轮询传输建立连接。虽然 WebSocket 显然是建立双向通信的最佳方式,但经验表明,由于企业代理、个人防火墙、防病毒软件的原因,并不总是能够建立 WebSocket 连接......

那么您将能够使用 Redis适配器在节点之间传递事件。

This is not what you are asking but if you disable HTTP long-polling transport you don't need sticky sessions, per your link:

if you disable the HTTP long-polling transport (which is a perfectly valid choice in 2021), you won't need sticky sessions:

This is what Socket.IO has to say about long polling, so keep this in mind:

By default, the client establishes the connection with the HTTP long-polling transport. While WebSocket is clearly the best way to establish a bidirectional communication, experience has shown that it is not always possible to establish a WebSocket connection, due to corporate proxies, personal firewall, antivirus software...

Then you will be able to use the redis adapter to pass events between nodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文