简体   繁体   English

ASP.Net - 在负载平衡环境中处理会话数据?

[英]ASP.Net - Handling session data in a load balanced environment?

How does ASP.Net deal with session data in a load balanced environment? ASP.Net如何在负载平衡环境中处理会话数据? Let's say a user makes multiple requests while navigating through a couple of pages? 假设用户在浏览几页时会发出多个请求? Do all requests go to the same server in the load balancer? 是否所有请求都转到负载均衡器中的同一台服务器?

What can I do if they don't all point to the same server? 如果他们并非都指向同一台服务器,我该怎么办? how do I deal with my session data? 我该如何处理会话数据?

You need to ensure that your NLB has the ability to enable "Sticky" sessions. 您需要确保您的NLB能够启用“粘性”会话。

If this isn't an option, then you need to look at using the ASP.Net Session Modes to configure either a Session Server, or some backing store like SQL Server. 如果这不是一个选项,那么您需要查看使用ASP.Net会话模式来配置会话服务器或某些后备存储,如SQL Server。

In general most load balancers try to have persistent connections but for your purposes you can't guarantee it. 通常,大多数负载均衡器尝试持久连接,但出于您的目的,您无法保证它。 You're best off either using something like an SQL backend to maintain state or create a separate class to manage your session data. 您最好使用类似SQL后端的东西来维护状态或创建一个单独的类来管理会话数据。 In either case you can then rebuild the session data if it happens to be null when you expect it to be otherwise. 在任何一种情况下,您都可以重建会话数据,如果它恰好是您想要的其他时间。 I tend to use the getters to check for null and rebuild when necessary. 我倾向于使用getter来检查null并在必要时重建。 In practice I haven't done any metrics to see how often it is rebuilt but I'd guess not very often. 在实践中,我没有做任何指标,看看它重建的频率,但我猜不常。 Better to be safe than sorry, as they say. 正如他们所说,最好是安全而不是抱歉。

Load balancers usually can be configured with something the infrastructure guys call sticky session or sticky bit . 负载均衡器通常可以配置基础设施人员称为sticky sessionsticky bit On this type of environment, once a request has been assigned to a server; 在这种类型的环境中,一旦将请求分配给服务器; it stays with that server throughout the session. 它在整个会话期间与该服务器保持一致。

When Load Balancers are not configured in such a way, ASP.NET offers several session state providers. 如果未以这种方式配置Load Balancers,ASP.NET将提供多个会话状态提供程序。 One very popular is the SQL Server State Provider , which allows session information to be stored in a database. 一个非常流行的是SQL Server State Provider ,它允许将会话信息存储在数据库中。 In order for this to work, all objects placed in the Session object must be Serializable so that they can be stored in a table. 为了使其工作,放置在Session对象中的所有对象必须是Serializable,以便它们可以存储在表中。

As I said, there are several more alternatives; 正如我所说,还有其他几种选择; for example, some people prefer to use a different approach such as memcached or similar products. 例如,有些人喜欢使用不同的方法,例如memcached或类似的产品。

You can use NLB from Windows or other products such as BIG-IP F5,HA Proxy, there tons of options. 您可以使用Windows中的NLB或其他产品,如BIG-IP F5,HA代理,有很多选项。

Use Out-of-Proc (State Server) and make sure your machinekey settings are the same on all your servers. 使用Out-of-Proc(状态服务器)并确保所有服务器上的machinekey设置相同。 Also make sure you are doing IO on a shared disk drive such as a NAS. 还要确保在共享磁盘驱动器(如NAS)上执行IO。

Here is a link with some more tips: Load Balancing and ASP.NET 这是一个链接,其中包含一些提示: 负载平衡和ASP.NET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM