1.方案特点
对于对SESSION强依赖的系统,并且对系统处理能力要求不是太高,只考虑2台TOMCAT作为后端服务的主备方式的情况,这是一种简答的配置。不适合太多的TOMCAT节点,节点越多,复制SESSION的代价会几何方式增加。如:适合小型网站,管理系统,小型业务系统。
3.基础环境安装设置
操作系统环境,APACHE安装,TOMCAT集群安装,TOMCAT测试工程请参见:
http://zp820705.iteye.com/blog/1347417
4.TOMCAT配置
Tomcat集群配置后端Tomcat Server为支持AJP的独立服务,以适应前端Apache配置为粘性会话(sticky-session),Tomcat节点配置Cluster,支持以多播方式进行内存级Session复制。
Tomcat1配置
配置Cluster,使用多播方式同步复制实现节点间session复制。详细配置参数请参见:
http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html
修改server.xml,在Engine节点内,host节点后,加入Cluster配置,如下:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
修改Engine节点配置,以支持AJP方式连接
<Engine name="Catalina" defaultHost="localhost">
为
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
完整配置文件请参见:
配置完成保存后,通过SCP命令拷贝到TOMCAT2节点
scp server.xml [email protected]://opt/apache-tomcat-6.0.33/conf/
Tomcat2配置
Tomcat2的配置基本与TOMCAT1配置相同,唯一不同的只是AJP的jvmRoute参数不同。
修改server.xml中
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
为:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
5.Apache配置
apache的配置方式还是采用vhost方式配置,主配置文件(conf/httpd.conf)引用(include)vhost子配置文件(conf/extra/httpd-vhosts.conf)方式.
Apache的编译请参考:http://zp820705.iteye.com/blog/1347749
apache编译为支持Proxy后的module情况:
LoadModule proxy_module modules/mod_proxy.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.so LoadModule proxy_scgi_module modules/mod_proxy_scgi.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule ssl_module modules/mod_ssl.so LoadModule speling_module modules/mod_speling.so LoadModule rewrite_module modules/mod_rewrite.so
删除主配置文件conf/httpd.conf文件中引用vhost的配置行的注释。
# Virtual hosts Include conf/extra/httpd-vhosts.conf
Apache虚拟主机配置(httpd-vhost.conf)
apache端可以配置sticky-session或no-sticky-session,sticky-session实现的是会话级别的负载均衡,no-sticky-session实现的是请求级别的负载均衡。可以根据实际情况选择配置。
<VirtualHost *:80> ErrorLog "logs/acooly.org-error_log" LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon CustomLog logs/access_log vcommon ProxyRequests Off ProxyPreserveHost on # apache+tomcat cluster ProxyPass / balancer://tomcat-cluster/ stickysession=JSESSIONID|jsessionid nofailover=Off ProxyPassReverse / balancer://tomcat-cluster/ <Proxy balancer://tomcat-cluster/> BalancerMember ajp://10.10.10.11:8009 loadfactor=1 route=tomcat1 BalancerMember ajp://10.10.10.12:8009 loadfactor=1 route=tomcat2 ProxySet lbmethod=bybusyness </Proxy> </VirtualHost>
<VirtualHost *:80> ErrorLog "logs/acooly.org-error_log" LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon CustomLog logs/access_log vcommon ProxyRequests Off ProxyPreserveHost on # apache+tomcat cluster ProxyPass / balancer://tomcat-cluster/ ProxyPassReverse / balancer://tomcat-cluster/ <Proxy balancer://tomcat-cluster/> BalancerMember ajp://10.10.10.11:8009 loadfactor=1 route=tomcat1 BalancerMember ajp://10.10.10.12:8009 loadfactor=1 route=tomcat2 ProxySet lbmethod=bybusyness </Proxy> </VirtualHost>
6.测试
1.浏览器访问:http://10.10.10.11/cluster/index.jsp
上图显示后端服务为10.10.10.11
2.通过表单设置session参数key1,key2
3.停止10.10.10.11节点上的tomcat服务,再次刷新页面,查看session变量是否存储,如果存储则表示:故障转移和会话复制成功了。