http://lym6520.iteye.com/blog/416280
上篇文章呢将乐如何集成apache服务器和tomcat服务器,见文章http://lym6520.iteye.com/admin/blogs/412046,接下来讲讲如何tomcat集群
首先呢,修改配置文件workers.properties
# the list of workers worker.list=router,worker1,worker2 worker.worker1.type=ajp13 worker.worker1.host=ip worker.worker1.port=port worker.worker1.lbfactor=100 worker.worker2.type=ajp13 worker.worker2.host=ip worker.worker2.port=port worker.worker2.lbfactor=100 worker.router.type = lb worker.router.balance_workers=worker1,worker2 worker.router.sticky_session = true worker.router.sticky_session_force = false
这个配置文件,配置了两个tomcat和一个负载平衡器router(要注意的是,worker1和worker2分别对应各自tomcat服务器server.xml的 Engine的jvmRoute值)
这样tomcat集群基本上配置完成了,但是这样会有个问题,会话不能共享,也就是说我将同一个项目分别部署在这两个tomcat上,通过apache服务器访问这个项目,发现会话不能共享,需要重复登入,为了解决这个问题,可以再两个tomcat服务器的server.xml里的Engine元素增加以下配置信息(该配置信息可以再tomcat官网中找到):
- <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"/>
- <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
- tempDir="/tmp/war-temp/"
- deployDir="/tmp/war-deploy/"
- watchDir="/tmp/war-listen/"
- watchEnabled="false"/>
- <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
- <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
- </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"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
(在Membership 元素中有个bind属性,可以明确的指定当前服务器的ip地址)
这样就能保证会话可以在两个tomcat将共享了,当然在项目的web.xml中加入如下配置:
- <!-- 将会话移交给tomcat server.xml配置的会话管理器管理 -->
- <distributable />
<!-- 将会话移交给tomcat server.xml配置的会话管理器管理 --> <distributable />
ok,大功告成!下次我将介绍如何在linux下配置apache服务器!