node.js - Firebase 与 AWS/ALB 负载均衡器和 Nginx 后面的多个 Node.js 连接

标签 node.js sockets amazon-web-services firebase nginx

我需要将 Firebase 连接到 AWS/Elastic Beanstalk 上的 Node 设置。有 1-4 个 Node 服务器,位于 ALB 负载均衡器和 Nginx 代理后面。 Firebase 使用 WSS 协议(protocol)(因此需要 ALB,因为常规 ELB 不支持套接字)。当 Node 实例通过 Firebase 进行身份验证时,它会获得应用程序可以监听的套接字。

我的问题:由于可能有任何 Node 服务器与 Firebase 通信,因此如何使套接字保持粘性,以便无论哪个 Node 服务器打开套接字,它都将是每次通信的正确套接字?

谢谢!

ZE

最佳答案

您可以启用Sticky sessions在 AWS ALB 中,WSS 协议(protocol)可以工作并将流量在一段时间内发送到同一 EC2 实例。

另请注意,您需要在目标组级别配置粘性。

I created a 2nd Target Group call "xxxSocket" with Stickiness enabled, and leaving the 1st Target Group "xxxHTTP" without Stickiness (default). Finally, in my Application Load Balancer, I added a new Rule to have "Path Pattern" = /socket.io then route to Target Group "xxxSocket", leaving the default pattern route to "xxxHTTP".

引用:AWS Forum Anyone gotten the new Application Load Balancer to work with websockets?

此外,WebSocket 连接本质上是粘性的。

WebSockets connections are inherently sticky. If the client requests a connection upgrade to WebSockets, the target that returns an HTTP 101 status code to accept the connection upgrade is the target used in the WebSockets connection. After the WebSockets upgrade is complete, cookie-based stickiness is not used.

引用:Target Groups for Your Application Load Balancers

关于node.js - Firebase 与 AWS/ALB 负载均衡器和 Nginx 后面的多个 Node.js 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45900955/

相关文章:

sockets - 使用 sendto 发送时如何强制 UDP 中的客户端打开端口

node.js - 在 AWS Lambda 中重试

java - 通过 java 套接字传输大文件

ajax - 如何限制对 AWS EC2 上 Node 应用程序的 API 调用

amazon-web-services - 限制 lambda 权限以访问 VPC

javascript - 多客户端 Node.js 应用程序的 mocha-casperjs headless 测试

javascript - 无法将 .pem 文件导入 Express

javascript - 使用 JavaScript 在 if 语句中设置变量抛出错误(已声明)

javascript - OAuth 2.0 token 处理。是否有服务器 token 和客户端 token ?

C# 32feet.Net : Handling two bluetooth connections in seperate threads, 给出 SocketException