参考: tomcat.apache.org  -->Documentation -->TomcatX -->Clustering 


实验环境:centos5.11 64,jdk1.6 tomcat6

IP:192.168.10.10  tomcatA   nginx , tomcat , 

IP:192.168.10.11  tomcatB   tomcat



配置如下

nginx:

      

upstream mytomcat {

         server localhost:8080;

         server 192.168.10.11:8080;

            }

    server {

        listen       80;

        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {

          proxy_pass  http://mytomcat;

        }


tomcatA/B:

测试页面:

#vi $TOMCAT_HOME/webapps/ROOT/test.jsp


<%@ page language="java" %>

<html>

  <head><title>TomcatA/B</title></head>

  <body>

    <h1><font color="blue">TomcatB.dodowolf.com</font></h1>

    <table align="centre" border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("dodowolf.com","dodowolf.com"); %>

        <td><%= session.getId() %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%= session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>



#cat $TOMCAT_HOME/webapps/ROOT/WEB-INF/web.xml

...

...

    <distributable/>

</web-app>


修改server.xml文件:

<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>


注意:1.在各节点的server.xml的engine或host容器添加如上内容

      2.注意修改MemberShip组件中的多播地址address="228.0.0.4"

      3.建议修改Receiver中的address为本机能够传递心跳信息的地址

      4.在各节点为使用组播地址添加组播路由,格式:

        route add -net $MCAST_ADDRESS netmask 255.255.255.255 dev eth0

      5.在相应应用程序的web.xml中添加<distributable\>

      6.以上内容定义在Engine容器中,则表示对所有主机均启动用集群功能, Engine 参数                 jvmRoute="TomcatA" ,另一台要改为“TomcatB”。如果定义在某Host中,则表示仅对此主机         启用集群功能