网络负载均衡(NLB)及Session 同步相关

随著互联网的迅速发展,应用伺服器工作量的日益增加,负载均衡技术的应用越加的广泛,而在眾多的负载平衡技术中,网路负载平衡技术Network Load Balancing NLB)由於其优势,成为了目前使用最为广泛的技术。

  网路负载平衡技术就是能将大量的客户端请求负载比较平均地分佈到同一网路中的多臺伺服器或多块网卡来进行处理的一种技术。使用Windows Network Load Balancing Services(网路负载平衡服务)可以极大的提高系统的可靠性及性能,并且增强伺服器应用程式的可伸缩性和可用性。目前可以利用网路负载平衡的应用套装程式括诸如HTTP和文件传输协议(FTP)(使用Internet资讯服务(IIS))、防火墙与代理(使用ISA 2000)、虚拟专用网、Windows Media Services、移动资讯伺服器和终端服务等这样的Web服务。同时,网路负载平衡还有助於改善伺服器的可伸缩性,以满足不断增长的Internet型客户端的需求。

  NLB群集最早出现在Windows 2000 ServerAdvanced Server系统中,在Windows Server 2003的某些版本中均提供了此项功能。在NLB群集中,每台伺服器都会有一个属於自己的静态IP地址,但NLB群集中的所有伺服器还有一个共同的IP地址—NLB群集地址。客户端可以通过这个IP地址连接到NLB群集,就像连接到其他IP地址一样。当有客户端请求连接到这个共用的IP地址时,NLB群集会将每个访问请求指派一个具体的群集成员。当部署一个使用网路负载平衡的群集时,每个伺服器都需要对所有的用户可用,为了更加方便的实现这一点,我们需要存储每个用户的资讯,系统的资讯,以及公共的资讯在一个都可以访问的地方,例如后端的文件伺服器。 必要的终端服务组件

  对於建立网路负载平衡,终端服务中有两个组件至关重要。

  首先就是Terminal Services Session Directory(终端服务会话目录),这个服务是一个用来跟踪群集上终端服务会话的数据库,它可以为用户连接到现在正在进行的会话提供资讯。

  Terminal Services Session Directory系统服务允许负载平衡的终端伺服器群集将用户的连接请求路由到该用户已经具有运行会话的伺服器上。用户将被路由到第一个可用的终端伺服器,而不管这些用户在群集中的其他伺服器上是否有运行的会话。负载均衡通过TCP/IP网路协议将几个伺服器上正在处理的资源集中起来。对终端伺服器的群集使用此服务,可以通过在多个伺服器之间分佈会话来平衡单个终端伺服器的性能。会话目录跟踪群集中的断开的会话,确保用户可以重新连接到那些会话。

  当会话目录服务开啟后,它将创建一个“Session Directory Computers”的本地组,默认这个组是不被使用的,用户必须要手动的将希望加入会话目录服务的电脑或者组加到里面去。

  为了确保会话目录服务是开啟状态的,用户可以在Services里双击Terminal Services Session Directory Properties然后点选Start

  另一个组件就是Terminal Services Connection Management ,这个工具与网路负载平衡服务一起工作,用来确保用户可以重新连接到原来的会话上。整个过程分成下面两步:

  1.当用户登录到终端服务群集时,终端伺服器将会收到一个初始用户登录请求,并且向会话目录伺服器发送一个查询。

  2.会话目录服务通过自己的数据库检查用户名,然后发送检查结果到请求伺服器:如果用户没有断开的会话,登录将继续驻留在初始连接的伺服器;而如果用户在其他的伺服器上有断开的会话,那麼客户端会话将会传递到那臺伺服器并继续登录。

 

--------------------------------------------------------------------------------------------------------------------

 

ASP.NET   有三种   Session   方式:
1.in-process
Session   存在同一台   server   里面,不能跨机器。
2.out-of   process
Session   可以跨机器。
(1).Session   Service
:可以指定某一台机器提供   Session   Service,其它   Web   Server   就可以把   Session   存在   Service   里。
(2).SQL   Server
.NET   有提供一个   script   可以在   SQL   Server   建立存放   Session     Table     Job,其它   Web   Server   就可以把   Session   存在   SQL   Server   里。
Session  
  State   可以在   Web.config   里面设定。

-----------------------------------------------------------------------------------------------------------------------

创建和配置ASP.NET Session状态数据库

 

在基于NLB(网络负载平衡)环境下的ASP.NET Web应用程序开发,我们需要将Session存储在数据库中供多个Web应用程序调用,以下为配置方法及注意事项。

1.
创建用于存储ASP.NET Session的数据库(远程、本地皆可,使用数据库用户身份认证)
Windows\Microsoft.NET\Framework/V2.0.50727目录下使用如下命令:
aspnet_regsql.exe -S
SQL Server IP> -U User Name> -P Password> -E -ssadd -sstype c -d Database Name>
命令执行后就会成功建立起用于存储ASP.NET Session变量的数据库了。

2.Web.Config
文件配置项
我们需要在ASP.NET Web应用程序中的Web.Config文件修改sessionState配置项以使Session状态数据库生效。
配置节点如下:
sessionState mode="SQLServer"
            sqlConnectionString="server=
Server IP>;database=Database Name>;uid=User Name>;pwd=Password>;"
 allowCustomSqlDatabase="True"
            cookieless="false"
            timeout="20" />

3.
注意在进行系统测试(主要是负载测试)的时候,因为数据库访问负载的增加,需要调整SQL Server相应超时的配置项以适应负载。(默认值为10,请适度进行调整。)

ASP.NET Session状态数据库数据模型
1.ASPStateTempSessions表定义
3.
使用的存储过程

Stored Procedure

Description

CreateTempTables

Creates the ASPStateTempSessions and ASPStateTempApplications tables; called during setup, but not called by SqlSessionStateStore.

DeleteExpiredSessions

Used by SQL Server Agent to remove expired sessions.

GetHashCode

Hashes an application name and returns the hash; called by TempGetAppID.

GetMajorVersion

Returns SQL Server's major version number.

TempGetAppID

Converts an application name into an application ID; queries the ASPStateTempApplications table and inserts a new record if necessary.

TempGetStateItem

Retrieves read-only session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7).

TempGetStateItem2

Retrieves read-only session state from the database (ASP.NET 1.1).

TempGetStateItem3

Retrieves read-only session state from the database (ASP.NET 2.0).

TempGetStateItemExclusive

Retrieves read/write session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7).

TempGetStateItemExclusive2

Retrieves read/write session state from the database (ASP.NET 1.1).

TempGetStateItemExclusive3

Retrieves read/write session state from the database (ASP.NET 2.0).

TempGetVersion

Marker whose presence indicates to ASP.NET 2.0 that the session state database is ASP.NET 2.0-compatible.

TempInsertStateItemLong

Adds a new session, whose size is > 7,000 bytes, to the database.

TempInsertStateItemShort

Adds a new session, whose size is = 7,000 bytes, to the database.

TempInsertUninitializedItem

Adds a new uninitialized session to the database in support of cookieless sessions.

TempReleaseStateItemExclusive

Releases a lock on a session; called when ASP.NET determines that a request has timed out and calls the provider's ReleaseItemExclusive method.

TempRemoveStateItem

Removes a session from the database when the session is abandoned.

TempResetTimeout

Resets a session's timeout by writing the current date and time to the corresponding record's Expires field.

TempUpdateStateItemLong

Updates a session whose size is > 7,000 bytes.

TempUpdateStateItemLongNullShort

Updates a session whose old size is = 7,000 bytes, but whose new size is > 7,000 bytes.

TempUpdateStateItemShort

Updates a session whose size is = 7,000 bytes.

TempUpdateStateItemShortNullLong

Updates a session whose old size is > 7,000 bytes, but whose new size is = 7,000 bytes.


ASP.NET 状态数据库FAQ

1.如果把SESSION值存放到数据库中去,用户关闭了程序那怎么样清空数据库里的SESSION值呢?
   
实际ASP.NET在创建状态数据库的时候会在SQL Server代理(SQL Server Agent)的作业中添加一个作业,名称为<状态数据库名>_Job_DeleteExpiredSessions。如果打开SQL Server代理服务数据库可以通过添加的状态记录的超时时间字段(Exprires)定期对超时的状态数据进行删除。

2.ASPStateTempSessions
表中的SessionId字段如何使用?
数据库中此表的SessionID字段的值,由SessionIDAppID共同组成,最后8位为AppID所以,后8位之前一定是SessionID。例如,存储在数据库中的值为"ekr30c3mwvnc3145yrswew3a037e5e5a",后8位的"037e5e5a"AppID,而前面的"ekr30c3mwvnc3145yrswew3a"为应用程序中你可以使用Session.SessionID获得的字符串。

3.
如何判断Session何时被更新的?
Session
记录被更新时会同时更新ExpiresLockDateLocalExpires字段为UTC时间,如果想通过本地之间进行比较判断还是需要使用LockDateLocal

4.
获得Web.config配置文件节点信息的程序?

列名

类型

描述

SessionId

nvarchar(88)

Session ID + application ID

Created

datetime

Date and time session was created (UTC)

Expires

datetime

Date and time session expires (UTC)

LockDate

datetime

UTC date and time session was locked

LockDateLocal

datetime

Local date and time session was locked

LockCookie

int

Lock ID

Timeout

int

Session timeout in minutes

Locked

bit

1=Session locked, 0=Session not locked

SessionItemShort

varbinary(7000)

Serialized session state (if <= 7,000 bytes)

SessionItemLong

image

Serialized session state (if > 7,000 bytes)

Flags

int

Session state flags (1=Uninitialized session)


2.ASPStateTempApplications
表定义

列名

类型

描述

AppId

int

Application ID

AppName

char(280)

Application name

 


''
获得Web.config文件配置实例
Dim configuration As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/web.config")

''获得状态配置节点实例
Dim mSessionStateSection As System.Web.Configuration.SessionStateSection = CType(configuration.GetSection("system.web/sessionState"),System.Web.Configuration.SessionStateSection)

''获得状态模式
Response.Write(mSessionStateSection.Mode)
''获得状态超时时间
Response.Write(mSessionStateSection.Timeout)

 

posted on 2011-03-06 21:18  xiaohu  阅读(2264)  评论(0编辑  收藏  举报