Tomcat7 Cluster 集群(一)
确保所有节点都有相同的时间。确保loadbalancer被设置为粘性session模式(sticky session)注意:浏览器会在某些情况下创建新的Session。<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"> <!-- <Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/> --> <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="5000" selectorTimeout="100" 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"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/> <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.ClusterSessionListener"/></Cluster><Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">?Cluster元素可以是<Engine>或者<Host>的子元素,如果使用farm war deploy功能,则必须是<Host>的子元素。SimpleTcpCluster是官方提供的唯一一个Cluster。channaelSendOptions是设置SimpleTcpCluster.send方法发送消息的方式,默认值是8。参考:http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/tribes/Channel.html更多信息参考:http://tomcat.apache.org/tomcat-7.0-doc/config/cluster.html<!--<Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/>--><Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/>?默认的manager。在Tomcat5.x以后每个webapp都可以配自己的manager,但是每个节点上相同的webapp要配置相同的manager。如果<Context>中没有manager的配置则使用上面的配置。更多信息参考:http://tomcat.apache.org/tomcat-7.0-doc/config/cluster-manager.html<Channel className="org.apache.catalina.tribes.group.GroupChannel">?Channel元素是Tribes,该元素封装所有沟通和成员逻辑。更多信息参考:http://tomcat.apache.org/tomcat-7.0-doc/config/cluster-channel.html<Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/>?通过组播方式建立成员,通过address和port设置一个组播,在相同组播下为同一个集群。Tribes也支持静态成员,通过使用StaticMembershipInterceptor。更多信息参考:http://tomcat.apache.org/tomcat-7.0-doc/config/cluster-membership.html<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="5000" selectorTimeout="100" maxThreads="6"/>?在Tribes中,发送(sender)和接收(receiver)是分开的两个组件。每个节点的address和port是不同的(如果节点有不同的IP地址,可以使用"auto",如果节点在同一个IP上,则port必须不同)。这个组件中有一个线程池,可以配置相关信息。更多信息参考:http://tomcat.apache.org/tomcat-7.0-doc/config/cluster-receiver.html<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/></Sender>?发送(sender)组件,这个就这么配,没什么贴别特别说明的。更多信息参考:http://tomcat.apache.org/tomcat-7.0-doc/config/cluster-sender.html <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/></Channel>??Tribes用堆栈发送信息,在堆栈中的每个元素都要通过interceptor。<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>?过滤一些不会修改session的request。更多信息参考:http://tomcat.apache.org/tomcat-7.0-doc/config/cluster-valve.html<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>?默认的tomcat集群支持Farm部署,通过一个节点,可以部署和卸载应用到其他节点。集群中只设置一个主节点,将watchEnabled设为true,主节点将监听watchDir的变化,先更新本节点的deployDir,之后更新其他节点的deployDir。集群中其他节点的watchEnabled设为false。如果要执行此功能,Cluster必须是Host的子元素。 <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/></Cluster> ...... <description> Servlet and JSP Examples. </description> <display-name>Servlet and JSP Examples</display-name> <distributable/> <!-- Define example filters --> <filter> <filter-name>Timing filter</filter-name> <filter-class>filters.ExampleFilter</filter-class> <init-param> <param-name>attribute</param-name> <param-value>filters.ExampleFilter</param-value> </init-param> </filter> ......?在项目中添加一个广泛使用的测试jsp:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ page import="java.util.*" %> <html><head><title>Cluster Test</title></head> <body> <% //HttpSession session = request.getSession(true); System.out.println(session.getId()); out.println("<br> SESSION ID:" + session.getId()+"<br>"); // 如果有新的请求,则添加session属性 String name = request.getParameter("name"); if (name != null && name.length() > 0) { String value = request.getParameter("value"); session.setAttribute(name, value); } out.print("<b>Session List:</b>"); Enumeration<String> names = session.getAttributeNames(); while (names.hasMoreElements()) { String sname = names.nextElement(); String value = session.getAttribute(sname).toString(); out.println( sname + " = " + value+"<br>"); System.out.println( sname + " = " + value); } %> <form action="testCluster.jsp" method="post"> 名称:<input type=text size=20 name="name"> <br> 值:<input type=text size=20 name="value"> <br> <input type=submit value="提交"> </form> </body> </html>?启动两个Tomcat,用浏览器访问一个Tomcat,设置一些属性到Session中,用相同浏览器代开另一个窗口,访问另一个Tomcat,看到页面中的Session含有之前设置的属性就说明Session复制成功。参考文档:http://peterwei.iteye.com/blog/757125