Web场中的SignalR连接管理 [英] SignalR connection management in a web farm

查看:191
本文介绍了Web场中的SignalR连接管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在大型Web应用程序中使用SignalR.我们已经部署了多个处理SignalR连接的Web服务器.我们实现了自己的Redis背板,我有以下问题: SignalR对服务器进行三个调用:1)协商,2)连接和3)启动.如果我们在负载均衡器后面有一个Web场,并且这三个调用最终转到三个不同的服务器,那么这三个服务器上的连接状态是否会损坏?在这种情况下会发生什么?我不是在谈论消息传递.

更具体地说:

  1. 第二个网络服务器是否能够理解connectionId 谈判电话产生的?

  2. 建立物理连接(websockets)后会发生什么 在不同的服务器上,然后开始调用转到另一个 服务器?

我知道SignalR不会在服务器之间传递连接信息.我想知道这三台服务器上内存中的连接状态. 我阅读了与此相关的另一个问题,但它仅涉及消息传递.对于我们来说,消息传递不是问题.如果这三个调用最终转到不同的服务器,我想了解连接的最终状态.

我已经回答过的问题: 在网络农场中的SignalR连接亲和力

由于我们的负载均衡器保持粘性,所以我无法确定粘性消失后会发生什么.我想为此做好准备.

解决方案

这个老问题,但是我花时间回答,因为我有一个类似的设置,尽管使用了不同的技术.它可能无法完全解决您的问题,但我认为可以得出很多相似之处.

我们的方案实时通信(即聊天消息).当其他用户登录,断开连接等操作时,服务器将随时通知客户端.

下面的技术栈:

客户端:AngularJS应用

CDN:提供静态资源(HTML,CSS,图像)

负载平衡器:在Azure中设置

多个WebApp:运行ASP.NET Core

多个SQL Server数据库:镜像

这里的关键是应用程序始终保持无状态.在任何时候,客户端都可以断开/重新连接并检索与断开连接之前相同的信息集.

  • 使用令牌机制(在本例中为OAuth)对用户进行身份验证.所有用户信息(用户ID等)都已在令牌中加密.

  • 已建立连接,并将其路由到任何正在运行的Web应用程序.

  • Webapp将连接信息存储在数据库表中.

  • Webapp检索在数据库中具有连接的用户的所有朋友".

  • Webapp将列表发送回客户端.

  • 每当用户断开连接时,webapp就会收到断开连接事件并删除记录表单数据库.

  • 用户重新连接后,webapp会重新添加连接,并向用户发送当前的在线连接/客户端列表.

由客户端来维护它处于哪个状态以及如何对客户端列表中的更新做出反应.

这种方法类似于您在问题中发布的答案中提到的方法.

我相信这里的基本概念是背板.它可以保持状态,而不仅仅是在服务器之间进行通信.

此处的相关博客文章可能会有所帮助: https://docs.microsoft.com/zh-cn/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections

(看看多个服务器"场景)

We are using SignalR in a large scale web application. We have deployed multiple web servers handling signalR connections. We have our own Redis backplane implemented, I have the following question: SignalR makes three calls to the server: 1) negotiate, 2) connect and 3) start. If we have a web farm behind a Load Balancer and these three calls end up going to three different servers, would the state of connections be corrupted on all three servers? What would happen in this scenario? I am not talking about message delivery.

More specifically:

  1. Would a second webserver be able to understand the connectionId produced by the negotiate call?

  2. What happens when the physical connection(websockets) is established on a different server and then the start call goes to another server?

I know for a fact that SignalR does not communicate the connection info between the servers. I am wondering about the state of connections in memory on these three servers. I read the another question related to this, but it talks only about message delivery. For us, message delivery is not a problem. I want to understand the final state of the connections if these three calls end up going to different servers.

The question that I already went through to get an answer: SignalR connection affinity in web-farm scenario

Since our load balancer maintains stickiness, I am unable to confirm what will happen when the stickiness goes away. I want to be prepared for that eventuality.

解决方案

Old question, but I'm taking the time to answer because I have a similar setup, albeit with different technologies. It might not completely solve your problems, but I think a lot of similarity can be drawn.

Our scenario real-time communication (i.e. chat messages). The server will notify the client at anytime when another user logs in, disconnects, etc.

Tech stack below:

Client: AngularJS App

CDN: serving static resources (HTML, CSS, images)

Load balancer: setup in Azure

Multiple WebApps: running ASP.NET Core

Multiple SQL Server databases: mirrored

The key point here is the application remains stateless throughout. At any point, a client may disconnect/reconnect and retrieve the same set of information it had before disconnecting.

  • Users are authenticated using a token mechanism (OAuth in our case). All user info (user id, etc.) are encrypted int he token.

  • Connection is established, and routed to any one of the web apps running.

  • Webapp stores connection info in a database table.

  • Webapp retrieves all "friends" of users who has a connection in the database.

  • Webapp sends list back to client.

  • Whenever a user disconnects, webapp receives disconnect event and removes record form database.

  • Whevenver a user reconnects, webapp adds the connection back, and sends the user the current list of online connections/clients.

It is up to the client to maintain which state it is in and how to react to updates in client lists.

This approach is similar to the one mentioned in the answer you posted in your question.

I believe the essential concept here is the backplane. It persists the state, instead of just communicating it around the servers.

A related blog post here may help: https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections

(Take a look at the "More than one server" scenario)

这篇关于Web场中的SignalR连接管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆