windows下apache服务器和tomcat集群

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官网中找到):

Xml代码 复制代码
  1. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  
  2.                 channelSendOptions="8">  
  3.   
  4.          <Manager className="org.apache.catalina.ha.session.DeltaManager"  
  5.                   expireSessionsOnShutdown="false"  
  6.                   notifyListenersOnReplication="true"/>  
  7.   
  8.          <Channel className="org.apache.catalina.tribes.group.GroupChannel">  
  9.            <Membership className="org.apache.catalina.tribes.membership.McastService"  
  10.                        address="228.0.0.4"  
  11.                        port="45564"  
  12.                        frequency="500"  
  13.                        dropTime="3000"/>  
  14.            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
  15.                      address="auto"  
  16.                      port="4000"  
  17.                      autoBind="100"  
  18.                      selectorTimeout="5000"  
  19.                      maxThreads="6"/>  
  20.   
  21.            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  
  22.              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  
  23.            </Sender>  
  24.            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  
  25.            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  
  26.          </Channel>  
  27.   
  28.          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  
  29.                 filter=""/>  
  30.          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>  
  31.   
  32.          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
  33.                    tempDir="/tmp/war-temp/"  
  34.                    deployDir="/tmp/war-deploy/"  
  35.                    watchDir="/tmp/war-listen/"  
  36.                    watchEnabled="false"/>  
  37.   
  38.          <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>  
  39.          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  
  40.        </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中加入如下配置:

Xml代码 复制代码
  1. <!--  将会话移交给tomcat server.xml配置的会话管理器管理 -->    
  2.   <distributable />   
<!--  将会话移交给tomcat server.xml配置的会话管理器管理 --> 
  <distributable /> 


ok,大功告成!下次我将介绍如何在linux下配置apache服务器!

原文地址:https://www.cnblogs.com/chulia20002001/p/2456600.html