ASP.NET中实现Session的负载均衡

据我目前所知有2种方法,如下:

1、利用微软提供的解决方案

参考网址:http://blog.maartenballiauw.be/post/2008/01/23/ASPNET-Session-State-Partitioning.aspx

2、利用第三方组件,比如:Memcached

我们排除第二种方法,只说第一种方法。


 在ASP.NET中Session的存储方式有3种:

1、IIS进程

2、ASP.NET State Service服务

3、SqlServer数据库

好,我们来分析一下,将Session放入IIS进程肯定是不行的,将Session放入SqlServer数据库中性能方面可能大打折扣,最好是放在ASP.NET State Service中。


下面说一下步骤:

1、在命令行键入services.msc打开服务窗口,开启ASP.NET State Service,仅仅这样还不行,还需要在注册表修改HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesaspnet_stateParametersAllowRemoteConnection的值,改为1即可。

2、新建解决方案MySession,里面包含一个Web项目:MySession和一个类库:App

3、在类库下面新建一类文件:PartitionResolver.cs,内容为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace App
{
    public class PartitionResolver : System.Web.IPartitionResolver
    {
        #region Private members

        private String[] partitions;

        #endregion


        #region IPartitionResolver Members

        public void Initialize()
        {
            // Create an array containing
            // all partition connection strings
            //
            // Note that this could also be an array
            // of SQL server connection strings!
            partitions = new String[] {
                "tcpip=192.168.1.17:42424",
                "tcpip=192.168.1.18:42424"
            };
        }

        public string ResolvePartition(object key)
        {
            // Accept incoming session identifier
            // which looks similar like "2ywbtzez3eqxut45ukyzq3qp"
            string sessionId = key as string;

            // Create your own manner to divide session id's
            // across available partitions or simply use this one!
            int partitionID = Math.Abs(sessionId.GetHashCode()) % partitions.Length;
            return partitions[partitionID];
        }

        #endregion
    }
}
View Code

  注意:可能需要引用System.Web,而且服务器IP列表自行斟酌修改,端口保持不变

4、在MySession Web项目中引用类库App

5、在MySession Web项目中的Web.config的<system.web>节点里面添加<sessionState mode="StateServer" partitionResolverType="App.PartitionResolver"/>

6、新建Default.aspx页面,cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MySession
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["test"] == null)
            {
                Session["test"] = "Test";
            }
            Response.Write(Session["test"]);
        }
    }
}
View Code

7、新建ShowSession.aspx页面,cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MySession
{
    public partial class ShowSession : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(Session["test"]);
        }
    }
}
View Code

按照我说的来,应该是没有问题的。简单吧?

而且还支持同一个顶级域名的跨域访问,不信自己试试。

如果有多台Web服务器,到时候可能还要注意machineKey

原文地址:https://www.cnblogs.com/subendong/p/3156721.html