1. Summary
All Web programs will use Session to save data. Using an independent Session server can solve the Session sharing problem in load balancing scenarios. This article introduces the establishment of a .NET platform Several methods of Session server, and introduces various experiences and techniques when using Session.
2. About Session, SessionID and Cookies
Session The data is stored on the server side, but each client needs to save a SessionID. The SessionID is stored in Cookies and expires when the browser is closed.
The SessionID will be included in the HTTP request sent to the server. The server side will SessionID gets the session information of this user.
Many junior developers don’t know the relationship between SessionID and Cookies, so they often think that there is no connection between the two. This is incorrect. It is precisely because SessionID is stored in Cookies, so in our When saving cookies, be sure not to create SessionID objects due to the size and number of cookies. In our program, there are special treatments for SessionID cookies:
方式名称 |
存储方式 | 性能 |
Off |
设置为不使用Session功能 |
无 |
InProc |
设置为将Session存储在进程内,就是ASP中的存储方式,这是默认值。 |
性能最高 |
StateServer |
设置为将Session存储在独立的状态服务中。通常是aspnet_state.exe进程. |
性能损失10-15% |
SQLServer |
设置将Session存储在SQL Server中。 |
性能损失10-20% |
Customer |
自定制的存储方案 |
由实现方式确定 |
我们可以在Web.Config中配置程序使用的Session存储方式.默认情况下是InProc, 即保存在IIS进程中. 关于Off, InProc和Customer本文不做讲解. 相关文章大家都可以在网上搜索到.
下面主要讲解 StateServer 和 SQLServer 的应用.
四.使用 StateServer 模式搭建Session服务器
(1)服务器端配置
1.启动 Asp.net State service服务.(这个服务默认的状态为手动.修改为自动并启动.)
2.修改注册表: [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\aspnet_state\Parameters]
设置 AllowRemoteConnection = 1 , 设置 Port = 42424 (十进制,默认即为42424)
Port是服务的端口号
AllowRemoteConnection 表示是否允许其他机器连接,0为仅能本机使用,1为可以供其他机器使用.
(2)客户端设置
在Web应用程序的Web.Config中, 我们需要修改
没有则添加(默认使用的是InProc方式)
选项 |
说明 |
t |
将会话数据存储到 SQL Server tempdb 数据库中。这是默认设置。如果将会话数据存储到 tempdb 数据库中,则在重新启动 SQL Server 时将丢失会话数据。 |
p |
将会话数据存储到 ASPState 数据库中,而不是存储到 tempdb 数据库中。 |
c |
将会话数据存储到自定义数据库中。如果指定 c 选项,则还必须使用 -d 选项包括自定义数据库的名称。 |
(2)Session client settings
This room also requires the web application to modify the
7. Summary
I have used the SqlServer mode to implement session sharing for multiple servers in the company. Restarting the server will not cause the user reservation process to restart (booking The Session required for the process will not be lost). I hope this article will be helpful to those who build specific Session servers.