Amazon EC2多个服务器共享会话状态

时间:2010-03-12 08:20:46

标签: c# asp.net .net-2.0

我有一堆负载均衡的EC2服务器。 有些服务器不共享会话,用户不断登录和注销。

如何让所有服务器共享一个会话, 甚至可能使用partitionresolver解决方案

    public class PartitionResolver : System.Web.IPartitionResolver
    {
        private String[] partitions;

        public void Initialize()
        {
            // create the partition connection string table
            //                           web1,            web2
            partitions = new String[] { "192.168.1.1" };
        }

        public String ResolvePartition(Object key)
        {
            String oHost = System.Web.HttpContext.Current.Request.Url.Host.ToLower().Trim();

            if (oHost.StartsWith("10.0.0") || oHost.Equals("localhost"))
                return "tcpip=127.0.0.1:42424";

            String sid = (String)key;

            // hash the incoming session ID into
            // one of the available partitions
            Int32 partitionID = Math.Abs(sid.GetHashCode()) % partitions.Length;

            return ("tcpip=" + partitions[partitionID] + ":42424");
        }
    }

-theo

相关问题