websocket 使用aws eks时在SignalR(.net core)中启用跳过协商的优势或劣势

yftpprvb  于 2023-03-08  发布在  .NET
关注(0)|答案(1)|浏览(204)

我们正在使用AWS kubernetes集群(EKS),带有ALB/入口和redis-cache用于信号连接。如果我们的副本集为3,则连接到服务时会随机抛出404错误。

SkipNegotiation = true
Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets

一切似乎都正常。我找不到将SkipNegotation设置为true的任何缺点。但找不到这是否有任何副作用(默认设置为false肯定有原因)。
SkipNegotation设置为true是否会导致其他问题?
正在运行的示例代码:

Console.WriteLine("Start");
HubConnection connection = new HubConnectionBuilder()
    .WithUrl("https://<url>/api/v1/taskboard", o =>
    {
        o.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
        o.SkipNegotiation = true;
    })
    .WithAutomaticReconnect()
    .Build();

await connection.StartAsync();

connection.On<string>("ReceiveMessage", message =>
{
    Console.WriteLine(message);
});

for (int i = 0; i < 20; i++)
{
    await connection.InvokeAsync("SendMessage", $"Hello {i}");
    await Task.Delay(55);
}

await connection.StopAsync();
await connection.DisposeAsync();
    • 编辑**

这些设置已添加到入口:

alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:......
alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}, {"HTTPS":443}]'
alb.ingress.kubernetes.io/ssl-redirect: '443'
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/subnets: subnet-..., subnet-...
alb.ingress.kubernetes.io/load-balancer-attributes: idle_timeout.timeout_seconds=600
alb.ingress.kubernetes.io/target-group-attributes: stickiness.enabled=true,stickiness.lb_cookie.duration_seconds=600
alb.ingress.kubernetes.io/healthcheck-path: /HealthCheck
alb.ingress.kubernetes.io/healthcheck-port: '80'
vcirk6k6

vcirk6k61#

使用SkipNegotiation的主要原因是为了避免使用粘性会话,当您想与SignalR服务器建立连接时,客户端会向服务器发送一个post请求,直到客户端收到服务器的响应后,连接才会建立:

客户请求:

{
   "connectionId":"807809a5-31bf-470d-9e23-afaee35d8a0d",
   "availableTransports":[
   {
      "transport": "WebSockets",
      "transferFormats": [ "Text", "Binary" ]
    },
    {
      "transport": "ServerSentEvents",
      "transferFormats": [ "Text" ]
    },
    {
      "transport": "LongPolling",
      "transferFormats": [ "Text", "Binary" ]
    }
    ]
}

服务器响应:

{
   "url": "https://myapp.com/chat",
   "accessToken": "accessToken"
}

为了避免使用粘性会话,客户端需要跳过协商,但被限制为仅使用websocket。
来源:https://github.com/aspnet/SignalR/blob/release/2.2/specs/TransportProtocols.md

相关问题