Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持

.net framework 4.5下测试成功,使用RedisSessionStateProvider 2.2.1保持session数据,通过Haproxy保持会话数据。
首先在PM下安装RedisSessionStateProvider
Install-Package Microsoft.Web.RedisSessionStateProvider,修改web站点 web.config,使两个web站点的Redis配置相同。

<configuration>
<appSettings>
<add key="val" value="100"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<sessionState mode="Custom" customProvider="MySessionStateStore">
<providers>
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="localhost" accessKey="" ssl="false" />
</providers>
</sessionState> </system.web> </configuration>

测试站点index.aspx页面源码

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApp.Index" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSet" runat="server" OnClick="btnSet_Click" Text="Set Session" />
</div>
</form>
</body>
</html>

后台代码

  public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(System.Configuration.ConfigurationManager.AppSettings["val"] + "<br/>"); if (Session["a"] != null)
{
Response.Write(Session["a"]);
}
else
{
Response.Write("session empty");
}
Response.Write("<br/>" + Request.Url.ToString());
Response.Write("<br/> sessionid=" + Session.SessionID);
} protected void btnSet_Click(object sender, EventArgs e)
{
Session["a"] = "Wilson.fu";
}
}

将测试站点发布到一*立服务器,iis中分拨设置(testweb1)81、(testweb2)82两个端口模拟两台web服务器。

Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持

CentOS下安装Haproxy,配置如下

 global
log 127.0.0.1 local0
# log 127.0.0.1 local1
maxconn 4000
ulimit-n 8000
uid 0
gid 0
# chroot /tmp
# nbproc 2
# daemon
# debug
# quiet listen proxy1 192.168.207.128:8000
mode http
# source 127.0.0.2:0
# log 127.0.0.1 local0
# log 127.0.0.1 local1
log global
#mode tcp
# cookie SERVERID insert indirect
appsession ASP.NET_SessionId len 30 timeout 5h request-learn
balance roundrobin
#dispatch 127.0.0.1:3130
#dispatch 127.0.0.1:31300
#dispatch 127.0.0.1:80
#dispatch 127.0.0.1:22
option httpchk
# server test 127.0.0.1:80 cookie cookie1 check inter 300
# server web02 192.168.0.104:80 cookie cookie2 check inter 300
#server web01 127.0.0.1:80 cookie cookie1 check inter 2000 fall 3 weight 30 #定义的多个后端
#server web02 192.168.0.104:80 cookie cookie2 check inter 2000 fall 3 weight 30 #定义的多个后端
server web01 192.168.207.130:81 cookie cookie1 check inter 2000 fall 3 weight 30 #定义的多个后端
server web02 192.168.207.130:82 cookie cookie2 check inter 2000 fall 3 weight 30 #定义的多个后端 # server nc 127.0.0.1:8080 cookie cookie1 check inter 300
# server tuxlocal0 10.101.23.9:80 cookie cookie1 check
# server tuxlocal1 127.0.0.1:80 cookie cookie1 check
# server tuxlocal2 127.0.0.1:80 cookie cookie2 check
# server tuxlocal3 127.0.0.1:80 cookie cookie3 check
# server tuxlocal4 127.0.0.1:80 cookie cookie4 check
# server vax 10.101.14.1:80 cookie cookie1 check
#server tuxceleron 10.101.0.1:80 cookie cookie2 check
#server telnet 127.0.0.1:23
#server ssh 127.0.0.1:22
#server local 127.0.0.1:3130 cookie cookie3 check
#server ko 127.0.0.1:0 cookie cookie3 check
#server local 127.0.0.1:8001 cookie cookie3 check
#server local 127.0.0.1:3130
#server celeron 10.101.0.1:80 cookie srv1
#server celeron 10.101.0.1:31300
#server local 10.101.23.9:31300
contimeout 3000
clitimeout 150000
srvtimeout 150000
maxconn 60000
option redispatch
retries 3
grace 3000
#rsprep ^Server.* Server:\ IIS
#rspdel ^Server.*
#rspadd Set-Cookie:\ mycookie=0;\ path=/
#rsprep ^(Date:\ )([^,]*)(,\ )(.*) LaDate\ est:\ \4\ (\2)
# force connection:close
#reqidel ^Connection:
#rspidel ^Connection:
#reqadd Connection:\ close
#rspadd Connection:\ close
# processing options
#option keepalive
option forwardfor
option httplog
option dontlognull
# reqirep ^(Test:\ ) \0_toto_\1_toto
# reqidel ^X-Forwarded-for:
# reqirep ^(GET|POST)\ .* \0
# reqirep ^(Host:|Connection:|User-agent:|Cookie:)\ .* \0
# reqideny ^

Haproxy

启动Haproxy

[root@localhost sbin]# ./haproxy -f /usr/local/haproxy/haproxy2.cfg

输入代理服务器IP,被分流至82站点

Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持

下面在iis中停止端口为82的站点,刷新页面发现自动分流至端口为81的站点,并且session值被保留!

Haproxy马上侦测到82端口的站点被停止(down)

Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持

Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持

上一篇:302重定向,MVC中的Get,Post请求。


下一篇:《所用到的AJAX技术基础》