第六章. Session集群工作步骤
参照tomcat7 doc:
To make it easy to understand how clustering works, We are gonna take you through a series of scenarios. In the scenario we only plan to use two tomcat instances TomcatA and TomcatB. We will cover the following sequence of events:
1. TomcatA starts up
2. TomcatB starts up (Wait that TomcatA start is complete)
3. TomcatA receives a request, a session S1 is created.
4. TomcatA crashes
5. TomcatB receives a request for session S1
6. TomcatA starts up
7. TomcatA receives a request, invalidate is called on the session (S1)
8. TomcatB receives a request, for a new session (S2)
9. TomcatA The session S2 expires due to inactivity.
第七章. 负载均衡配置
Tomcat有两种负载均衡的方式:
1. 使用 JK1.2.x native connector
2. 使用Apache HTTP Server 2.x with mod_proxy
我们使用的是mod_proxy,在Apache Http Server2.2以上版本已经自动带有:
Mod_proxy supports either HTTP or AJP load balancing. 我们通过ajp方式。
详细查看:tomcat7\webapps\docs\balancer-howto.html
1. 首先,监听8000端口, 在Apache安装目录下找到conf/httpd.conf文件
稍前面加上
2. conf/httpd.conf,去掉以下文本前的注释符(#)以便让Apache在启动时自动加载代理(proxy)模块?
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_connect_module modules/mod_proxy_connect.so LoadModule proxy_ftp_module modules/mod_proxy_ftp.so LoadModule proxy_http_module modules/mod_proxy_http.so3. conf/httpd.conf文件最后加上:
#虚拟机配置,负载均衡配置 <VirtualHost *:8000> ServerAdmin weigbo@163.com ServerName localhost ServerAlias localhost #小心,有些地方要有空格,要不然会出错哈哈。 ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On ProxyPassReverse / balancer://cluster/ #ErrorLog "logs/error.log" #CustomLog "logs/access.log" common </VirtualHost> #The ProxyRequests directive should usually be set off when using ProxyPass. ProxyRequests Off <proxy balancer://cluster> BalancerMember ajp://localhost:8009 loadfactor=1 route=tomcat7_a smax=5 max=20 ttl=120 retry=300 timeout=15 BalancerMember ajp://localhost:9009 loadfactor=1 route=tomcat7_b smax=5 max=20 ttl=120 retry=300 timeout=15 # status=+H为配置热备,当所有机器都over时,才会请求该机器 #BalancerMember http://192.168.1.218:8009 status=+H ProxySet lbmethod=bytraffic </proxy>4. Tomcat7配置(server.xml):
因为是同一机器,两个应用,所以配不同的端口,不同机器则不用配,要和前面的ajp对应上。
port="8009" protocol="AJP/1.3" redirectPort="8443" />
port="9009" protocol="AJP/1.3" redirectPort="8443" />
第八章. 负载均衡测试
Tomcat7_a的cluster工程的测试文件:testCluster.jsp加上html代码:
负载均衡测试:此为:Tomcat7_a上的文件,aaaaaaaaaaaaaaaaaa
Tomcat7_b的cluster工程的测试文件:testCluster.jsp加上html代码
负载均衡测试:此为:Tomcat7_b上的文件,bbbbbbbbbbbbbbbbbb
打开浏览器,进入地址::8000/cluster/testCluster.jsp
多刷新几次,会看到页面值会在aaaaa和bbbbb间切换,说明负载均衡配置成功。如图: