gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:09:50 29 4
gpt4 key购买 nike

感谢任何答案!
我创建了一个基于 .net core 3.1 在 docker(debian) 上运行的信号后端服务器。
当我只在 kubernetes 上创建单个服务器部署时,它运行良好。
但是当我将副本增加到 1 以上时,它工作不正常。
看起来redis背板不工作,导致多个服务器之间无法通信。
按照官方文档,我安装了 nuget 包:

<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="3.1.13" />
这是后端服务器启动.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");
});
// ...
}

所有代码似乎都没有问题,并且当服务器单独部署时它确实可以正常工作。一旦我添加了副本,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\_logs\2021-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 Server\140\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;D:\Program Files\platform-tools;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\Microsoft SQL Server\130\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\ODBC\170\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)

最佳答案

好吧,都是我的错。再次仔细阅读官方文档后,我找到了原因。我忽略了最后一点:
Configure your server farm load balancing software for sticky sessions.
我的情况的解决方法是配置nginx ingress开启粘性 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 中的节点

关于c# - 带有 redis 背板的横向扩展信号服务器无法正常工作(vue 前端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66684521/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com