八.构建基于内存session复制的tomcat集群为了防止由于节点故障导致的session丢失,我们来构建基于内存session复制的集群,在刚才的的负载均衡中你或许已经发现网页显示的session id一直是变动的,这个session id 就是通过jsp获得的此次会话的id,我们发现会话id一直是变动的,TomcatA中一直是TomcatA的session id,下面我们来构建吧。
8.1 修改172.16.1.21的server.xml,在engine下添加如下
vi /usr/local/tomcat/conf/server.xml
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<ManagerclassName="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
<MembershipclassName="org.apache.catalina.tribes.membership.McastService"
address="227.50.10.1"bind="172.16.1.21"port="45564"
frequency="500"dropTime="3000"/>
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="172.16.1.21"port="4000"autoBind="100"
selectorTimeout="5000"maxThreads="6"/>
<SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>
<ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"watchEnabled="false"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
8.2 修改balance虚拟主机的部署文件web.xml
我们建立的balance时没有建立web.xml,我们继承的是/usr/local/tomcat/conf/web.xml的,我们现在需要修改部署文件,所以拷贝到/web/balance/WEB-INF中修改,在<web-app> </web-app>中添加 <distributable/>,部份如下
cp /usr/local/tomcat/conf/web.xml /web/apps/WEB-INF/
vi /web/apps/WEB-INF/web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<distributable/>
</web-app>
8.3 修改172.16.1.22 的server.xml 基本与172.16.1.21的相同,只修改多播ip与接收ip即可,为了不浪费51blog的存储空间,就不完全显示了
<MembershipclassName="org.apache.catalina.tribes.membership.McastService"
address="227.50.10.1"bind="172.16.1.22"port="45564"
frequency="500"dropTime="3000"/>
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="172.16.1.22"port="4000"autoBind="100"
selectorTimeout="5000"maxThreads="6"/>
8.4 对balance虚拟主机部署文件修改与172.16.1.21相同
8.5 测试访问 http://balance.linuxidc.com/ 查看session id变化情况
到此构建结束,过程并不难,但要熟练使用tomcat必须得去了解java啊,还有好长的路要走,不要以为会构建了tomcat就觉的会tomcat了,其实还不到10%,所以加油。