No1*_*ver 7 redis signalr docker signalr-backplane asp.net-core
使用单个应用程序实例(容器)运行网站时 - SignalR工作正常.
当扩展到更多实例(> 1)时,它会抛出错误而无法正常工作.我在互联网上寻找解释,发现我需要将Signalr配置为在群集中工作.我选择Redis作为我的背板.
我已经完成了许多关于如何正确完成它的教程并且它不适合我.
我在Google云端托管了asp.net core v2.1.使用Kestrel + nginx将应用程序部署为Docker容器.docker容器在负载均衡器后面的Kubernetes集群中运行.
Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration,
IHostingEnvironment hostingEnvironment)
{
Configuration = configuration;
HostingEnvironment = hostingEnvironment;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSignalR().AddRedis(options =>
{
options.Configuration = new ConfigurationOptions
{
EndPoints =
{
{ ConfigurationManager.Configuration["settings:signalR:redis:host"], int.Parse(ConfigurationManager.Configuration["settings:signalR:redis:port"])}
},
KeepAlive = 180,
DefaultVersion = new Version(2, 8, 8),
Password = ConfigurationManager.Configuration["settings:signalR:redis:password"]
};
});
// ...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/errors/general");
app.UseHsts();
}
// nginx forward
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseSignalR(routes =>
{
routes.MapHub<StatisticsHub>("/hubs/myhub");
});
}
}
Run Code Online (Sandbox Code Playgroud)
为了验证与Redis服务器的连接是否成功,我检查了Kastrel的输出窗口:

在服务器上也发现了相同的行为(已连接)(2个副本,而不是在开发环境中).
为了验证Signalr是否"真正"使用Redis(而不仅仅是连接),我曾经redis-cli连接Redis服务器并发现:
由此我可以理解Redis上有一些"谈话".
我删除了网站LoadBalancer(GCP)并将其部署为agian.现在使用Sticky-Session:ClientIP.此负载均衡器将请求路由到不同的容器.
我没有改变的唯一地方是nginx配置.我错了?
SignalR无法在浏览器上运行.这些错误来自浏览器控制台:
scripts.min.js:1 WebSocket connection to 'wss://site.com/hubs/myhub?id=VNxKLazEKr9FKM4GPZRDhA' failed: Error during WebSocket handshake: Unexpected response code: 404
scripts.min.js:1 Error: Failed to start the transport 'WebSockets': undefined
Error: Connection disconnected with error 'Error: Server returned handshake error: Handshake was canceled.'.
scripts.min.js:1 Error: Connection disconnected with error 'Error: Server returned handshake error: Handshake was canceled.'.
files.min.js:1 Uncaught (in promise) Error: Error connecting to signalR. Error= Error
...
Run Code Online (Sandbox Code Playgroud)
我错过了什么?怎么检查?
| 归档时间: |
|
| 查看次数: |
550 次 |
| 最近记录: |