通过Socket.io连接到Application Load Balancer后面的AWS Fargate服务器

时间:2018-12-25 04:54:19

标签: amazon-web-services socket.io

我已经在AWS Fargate上设置了服务器,并通过Socket.io连接到Application Load Balancer。

我已启用了Application Load Balancer的访问日志,日志如下:

http 2018-12-25T04:00:15.787118Z app/EC2Co "GET http://ec2co.us-east-2.elb.amazonaws.com:28656/socket.io/?EIO=3&transport=polling&t=1545710415657-0 HTTP/1.1" "python-requests/2.20.0" - - arn:aws:elasticloadbalancing:us-east-2:735263141676:targetgroup/EC2Co-Defau-8X "Root=1-5c21ab4f-f26e11527cb2fa030ade3020" "-" "-" 0 2018-12-25T04:00:15.777000Z "forward" "-" "-"
ws 2018-12-25T04:00:14.548103Z app/EC2Co "GET http://ec2co.us-east-2.elb.amazonaws.com:28656/socket.io/?EIO=3&transport=websocket&sid=ils820o21XTc_rlnAAAA HTTP/1.1" "python-requests/2.20.0" - - arn:aws:elasticloadbalancing:us-east-2:735263141676:targetgroup/EC2Co-Defau-8X "Root=1-5c21ab27-560b3f40746e96c087a97ec0" "-" "-" 0 2018-12-25T03:59:35.646000Z "forward" "-" "-"
ws 2018-12-25T04:14:38.103181Z app/EC2Co "GET http://ec2co.us-east-2.elb.amazonaws.com:28656/socket.io/?EIO=3&transport=websocket&sid=lO2XYpIjTe_9p4s7AAAA HTTP/1.1" "python-requests/2.20.0" - - arn:aws:elasticloadbalancing:us-east-2:735263141676:targetgroup/EC2Co-Defau-8X "Root=1-5c21ab50-f024082cf6db65724f6384c0" "-" "-" 0 2018-12-25T04:00:16.360000Z "forward" "-" "-"
http 2018-12-25T04:14:39.359703Z app/EC2Co "GET http://ec2co.us-east-2.elb.amazonaws.com:28656/socket.io/?EIO=3&transport=polling&t=1545711279222-0 HTTP/1.1" "python-requests/2.20.0" - - arn:aws:elasticloadbalancing:us-east-2:735263141676:targetgroup/EC2Co-Defau-8X "Root=1-5c21aeaf-8a69112ff7361e029ddedf81" "-" "-" 0 2018-12-25T04:14:39.344000Z "forward" "-" "-"

如您所见,该连接已正确升级到websocket,但降级到polling

我想知道这是否正常吗?

即使我每分钟向负载均衡器发送ping,客户端与应用程序负载均衡器之间的连接也会大约每2分钟重置一次,有时少于2分钟。

此外,负载均衡器的运行状况检查仍然失败,并且没有目标变得健康。

enter image description here

我尚未在客户端代码中明确定义消息200 ok以便负载均衡器接收。我是否必须明确定义200 ok消息?如果是,那么负载均衡器在这些运行状况检查期间发送的事件是什么?

持续断开连接会导致一些从客户端到负载均衡器的输入,进而使AWS Fargate服务器丢失。

我正在服务器上使用express。以下是服务器上app.js的相关代码:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var AWS = require ('aws-sdk');

请注意,服务器代码位于Nodejs中,而客户端代码位于Python中。会有所不同并导致断开连接吗?

那么,这是正常行为吗(请参阅上面的日志)?还是我想念的东西?

此外,当进行健康检查以使目标变得健康时,如何定义要发送到负载平衡器的200 ok消息?

如果我想念任何信息,而您需要回答此问题,请在下面评论,我将编辑问题以提供所需的信息。

谢谢。

1 个答案:

答案 0 :(得分:1)

HealthCheck由LoadBalancer执行(在随附的TargetGroup中配置,默认设置为200 Ok,请参见AWS Elastic Loadbalancing Targetgroup HealthChecks

您已经找到/检查过这个吗?

does-an-application-load-balancer-support-websockets

抱歉,德国现在已经晚了。