一直以来,我误解认为启动了n个tomcat,则Session需要同步复制到n个Tomcat中存在,因此在启动了6个以上的Tomcat,性能会大大下降。
而实际情况下,采取Apache 加Tomcat进行负载均衡集群的时候,是可以不用将Session复制到所有的节点里,
比如有六个Tomcat实例
Tomcat1,Tomcat2,Tomcat3,Tomcat4,Tomcat5,Tomcat6
是可以配置成 三组互相复制Session的群组,
比如Tomcat1和Tomcat2互相同步Session,
比如Tomcat3和Tomcat4互相同步Session,
比如Tomcat5和Tomcat6互相同步Session.再在Apache的mod_jk模块里,对每个worker配置上redirect和activation属性。
将Tomcat1,Tomcat3,Tomcat5做为对外正常提供服务器的Tomcat,
Tomcat2,Tomcat4,Tomcat6作为分别对应的灾难恢复的备份Tomcat.
即比如Tomcat1当机之后,Apache会自动将发给Tomcat1的请求转发到Tomcat2上,而Tomcat2因为同步了Tomcat1的Session信息,因此从用户的角度,是感觉不出任何差异的。
单纯的Tomcat测试如下:
Tomcat配置:
Tomcat1. port=”15080″, jvmRoute=”tomcat1″,
<MembershipclassName=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.5″
port=”45564″
frequency=”500″
dropTime=”3000″/>Tomcat2. port=”16080″, jvmRoute=”tomcat2″,
<MembershipclassName=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.5″
port=”45564″
frequency=”500″
dropTime=”3000″/>Tomcat3. port=”17080″, jvmRoute=”tomcat3″,
<MembershipclassName=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.6″
port=”45564″
frequency=”500″
dropTime=”3000″/>Tomcat4. port=”18080″, jvmRoute=”tomcat4″,
<MembershipclassName=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.6″
port=”45564″
frequency=”500″
dropTime=”3000″/>Tomcat5. port=”19080″, jvmRoute=”tomcat5″,
<MembershipclassName=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.7″
port=”45564″
frequency=”500″
dropTime=”3000″/>Tomcat6. port=”20080″, jvmRoute=”tomcat6″,
<MembershipclassName=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.7″
port=”45564″
frequency=”500″
dropTime=”3000″/>所有的Tomcat webapp的web.xml加上<distributable />
(例如D:\Java\Tomcat2\webapps\examples\WEB-INF\web.xml ,)
测试页面为:
第一组:
http://localhost:15080/examples/servlets/servlet/SessionExample
http://localhost:16080/examples/servlets/servlet/SessionExample
第二组:
http://localhost:17080/examples/servlets/servlet/SessionExample
http://localhost:18080/examples/servlets/servlet/SessionExample
第三组:
http://localhost:19080/examples/servlets/servlet/SessionExample
http://localhost:20080/examples/servlets/servlet/SessionExample
在每一组中,打开其中任何一个链接,然后设置属性值,
都可以在访问另外一个链接的时候得到。对应的sessionid的后缀会变成对应Tomcat的jvmRoute.
(以上测试的tomcat均在同一台服务器上)接下来再配置Apache的mod_jk.
workers.properties的内容节选如下:
(重点是redirect和activation两个属性)
worker.tomcat1.port=15009
worker.tomcat1.host=127.0.0.1
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat1.redirect=tomcat2worker.tomcat2.port=16009
worker.tomcat2.host=10.108.20.126
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.tomcat2.redirect=tomcat1
worker.tomcat2.activation=disabled详细参考:
http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html
备注:Tomcat6已经没有使用JGroups来实现集群复制,
而是使用Apache Tribes 来实现该效果的。Apache Tribes的介绍在:
http://tomcat.apache.org/tomcat-6.0-doc/tribes/introduction.html
为了简便配置,因此只使用一份Tomcat,使用不同的cofig来启动,配置好的文件在:
http://618119.com/docs/Tomcat/TomcatCluster.zip
下载解压到Tomcat(例如D:\Java\apache-tomcat-6.0.14)目录里,运行对应的bat文件即可:
start15080.bat
start16080.bat
start17080.bat
start18080.bat
start19080.bat
start20080.bat