服务器发送事件或 Websockets 的 Aws 负载均衡器

Aws load balancer for Server Sent Events or Websockets

我正在尝试对 nodejs 服务器发送的事件后端进行负载平衡,我需要知道是否有办法将新连接分配给连接最少的客户端的实例。我遇到的问题是在向上扩展时,路由继续向已经饱和的实例发送新连接,并且由于连接存在很长时间,所以这根本行不通。

水平扩展长期连接有哪些选项?

既然您使用的是 AWS,我会向您推荐 Elastic Beanstalk for your Node.js application deployment. The official documentation provides good examples, like this one. Note that Beanstalk will automatically create an Elastic Load Balancer,这正是您要找的。

By default, Elastic Beanstalk creates an Application Load Balancer for your environment when you enable load balancing with the Elastic Beanstalk console or the EB CLI. It configures the load balancer to listen for HTTP traffic on port 80 and forward this traffic to instances on the same port.

[...]

Note: Your environment must be in a VPC with subnets in at least two Availability Zones to create an Application Load Balancer. All new AWS accounts include default VPCs that meet this requirement. If your environment is in a VPC with subnets in only one Availability Zone, it defaults to a Classic Load Balancer. If you don't have any subnets, you can't enable load balancing.

请注意,configuration of a proper health check path 是正确平衡请求的关键,正如您在问题中提到的那样。

In a load balanced environment, Elastic Load Balancing sends a request to each instance in an environment every 10 seconds to confirm that instances are healthy. By default, the load balancer is configured to open a TCP connection on port 80. If the instance acknowledges the connection, it is considered healthy.

You can choose to override this setting by specifying an existing resource in your application. If you specify a path, such as /health, the health check URL is set to HTTP:80/health. The health check URL should be set to a path that is always served by your application. If it is set to a static page that is served or cached by the web server in front of your application, health checks will not reveal issues with the application server or web container.

编辑:如果您正在寻找粘性会话,如我在评论中所述,请按照 this guide 中提供的步骤操作:

To enable sticky sessions using the console

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under LOAD BALANCING, choose Target Groups.

  3. Select the target group.

  4. On the Description tab, choose Edit attributes.

  5. On the Edit attributes page, do the following:

a. Select Enable load balancer generated cookie stickiness.

b. For Stickiness duration, specify a value between 1 second and 7 days.

c. Choose Save.

您似乎想要一个既能提供 "sticky sessions" 又能使用 "least connection" 而不是 "round-robin" 策略的负载均衡器。很遗憾,NGINX 无法提供此功能。

HAProxy(高可用性代理)允许这样做:

backend bk_myapp
 cookie MyAPP insert indirect nocache
 balance leastconn
 server srv1 10.0.0.1:80 check cookie srv1
 server srv2 10.0.0.2:80 check cookie srv2

如果您需要 ELB 功能并希望手动滚动,请查看此 guide

您可能还想确保经典 AWS ELB "sticky session" configuration or the newer ALB "sticky session" 选项不满足您的需求。 ELB 通常以最少 "load" 将连接发送到上游服务器,当与粘性会话结合使用时可能就足够了。