具有 redis 背板的横向扩展信号服务器无法正常工作(vue 前端)

scale-out signalr server with redis backplane not working correctly (vue front end)

感谢任何答案!

我在 docker(debian) 上创建了一个基于 .net core 3.1 运行 的 signalr 后端服务器。 当我只在 kubernetes 上创建单个服务器部署时,它运行良好。 但是当我将副本增加到 1 个以上时,它就无法正常工作。 貌似是redis背板坏了,导致多台服务器通信不可达。

按照官方文档,我安装了nuget包:

<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="3.1.13" />

这是后端服务器startup.cs代码:

public void ConfigureServices(IServiceCollection services)
{
// ...
  services
    .AddSignalR(option =>
    {
        option.EnableDetailedErrors = true;
    })
    .AddNewtonsoftJsonProtocol()
    .AddStackExchangeRedis(option =>
    {
    option.ConnectionFactory = async writer =>
    {
        var config = new ConfigurationOptions
        {
            AbortOnConnectFail = false,
            ChannelPrefix = "devopshub",
            ServiceName = "devopshub",
            ClientName = "devopshub"
        };
        config.DefaultDatabase = 13;
        var connection = await ConnectionMultiplexer.ConnectAsync("redis host:6379,allowAdmin=true,defaultdatabase=13", writer);
        connection.ConnectionFailed += (_, e) =>
        {
            Console.WriteLine("SignalR Redis Server Connection Failed.");
        };
        if (connection.IsConnected)
            Console.WriteLine("SignalR Redis Server Connected.");
        else
            Console.WriteLine("SignalR Redis Server Not Connected.");
        return connection;
    };
// ... 
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
    app.UseRouting();
    app.UseCors(builder => builder
    .SetIsOriginAllowed(_ => true)
    .AllowAnyMethod()
    .AllowAnyHeader()
    .AllowCredentials());
    app.UseEndpoints(builder =>
    {
        builder.MapHub<LogHub>("/api/loghub").RequireCors(t => t.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials());
        builder.MapHub<ProcessDetailStatusSyncHub>("/api/processdetailstatussynchub").RequireCors(t => t.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials());
        builder.MapControllers();
        builder.MapHealthChecks("/health");
    });
// ...
}

所有代码似乎都正常,并且在部署服务器时它确实可以正常工作single.once我添加了副本,websocket 连接将立即或几秒钟后重置或断开连接。 之后,前端(vue)项目会抛出无法处理的错误,然后崩溃。

像这样:

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! frontendProjectName@0.0.1 dev: `vue-cli-service serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the frontendProjectName@0.0.1 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\ASUS\AppData\Roaming\npm-cache\_logs21-03-18T03_13_05_435Z-debug.log
终端进程“C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command npm run dev”已终止,退出代码: 1。

最后一行中文意思是“进程已停止,退出代码为1”

这是日志文件内容:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'D:\nodejs\node.exe',
1 verbose cli   'D:\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'dev'
1 verbose cli ]
2 info using npm@6.13.4
3 info using node@v12.16.1
4 verbose run-script [ 'predev', 'dev', 'postdev' ]
5 info lifecycle frontendProjectName@0.0.1~predev: frontendProjectName@0.0.1
6 info lifecycle frontendProjectName@0.0.1~dev: frontendProjectName@0.0.1
7 verbose lifecycle frontendProjectName@0.0.1~dev: unsafe-perm in lifecycle true
8 verbose lifecycle frontendProjectName@0.0.1~dev: PATH: D:\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;E:\workspace\CICD\hualv-devops-frontend\node_modules\.bin;C:\Users\ASUS\AppData\Roaming\npm;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\Docker\Docker\Resources\bin;C:\Python27\;C:\Python27\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\dotnet\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;D:\Program Files\platform-tools;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\ManagementStudio\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;D:\nodejs\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC0\Tools\Binn\;E:\Nuget;C:\Users\ASUS\AppData\Roaming\npm;C:\Users\ASUS\AppData\Local\Microsoft\WindowsApps;D:\Program Files\Fiddler;D:\Docker Toolbox;C:\Users\ASUS\.dotnet\tools;D:\JetBrains Rider\bin;;D:\Microsoft VS Code\bin;E:\Nuget;C:\Users\ASUS\AppData\Local\Programs\Fiddler;C:\Users\ASUS\AppData\Local\Microsoft\WindowsApps;D:\JetBrains Rider 2020.3.2\bin;
9 verbose lifecycle frontendProjectName@0.0.1~dev: CWD: E:\workspace\CICD\hualv-devops-frontend
10 silly lifecycle frontendProjectName@0.0.1~dev: Args: [ '/d /s /c', 'vue-cli-service serve' ]
11 silly lifecycle frontendProjectName@0.0.1~dev: Returned: code: 1  signal: null
12 info lifecycle frontendProjectName@0.0.1~dev: Failed to exec dev script
13 verbose stack Error: frontendProjectName@0.0.1 dev: `vue-cli-service serve`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (D:\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:311:20)
13 verbose stack     at ChildProcess.<anonymous> (D:\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:311:20)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid frontendProjectName@0.0.1
15 verbose cwd E:\workspace\CICD\hualv-devops-frontend
16 verbose Windows_NT 10.0.19041
17 verbose argv "D:\nodejs\node.exe" "D:\nodejs\node_modules\npm\bin\npm-cli.js" "run" "dev"
18 verbose node v12.16.1
19 verbose npm  v6.13.4
20 error code ELIFECYCLE
21 error errno 1
22 error frontendProjectName@0.0.1 dev: `vue-cli-service serve`
22 error Exit status 1
23 error Failed at the frontendProjectName@0.0.1 dev script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

服务器控制台日志如下:

Socket connection closed prematurely.
System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
 ---> System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
   at System.Net.WebSockets.ManagedWebSocket.ThrowIfEOFUnexpected(Boolean throwOnPrematureClosure)
   at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure)
   at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
   at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
   at Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsServerTransport.StartReceiving(WebSocket socket)

好吧,都是我的fault.After仔细看了一遍官方文档,找到原因了。最后一点我忽略了:

Configure your server farm load balancing software for sticky sessions.

针对我的情况的解决方案是配置 nginx ingress 以启用 Sticky Session,如下所示:

    nginx.ingress.kubernetes.io/affinity: cookie
    nginx.ingress.kubernetes.io/affinity-mode: balanced
    nginx.ingress.kubernetes.io/session-cookie-expires: "86400"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "86400"

将上面的配置添加到入口 yaml 中的 metadata:annotations 节点