用tomcat作負載集羣時, 常常會用到session複製(Session Replication), 不少例子會告訴咱們要配置apache或者其餘的Web Server. 而事實上, 單純從session複製的角度講, 是不須要Web Server的.javascript
tomcat的session複製分爲兩種, 一種是全局試的(all-to-all), 這意味着一個node(tomcat實例)的session發生變化以後, 它會將這些變動複製到其餘全部集羣組的成員;另外一種是局部試的, 它會用到BackupManager, BackupManager能實現只複製給一個Buckup Node, 而且這個Node會部署相同的Web應用, 可是這種方式並沒用通過不少的測試(來自官方說明..).html
tomcat的session複製是基於IP組播(multicast)來完成的, 詳細的IP組播介紹能夠參考這裏.java
簡單的說就是須要進行集羣的tomcat經過配置統一的組播IP和端口來肯定一個集羣組, 當一個node的session發生變動的時候, 它會向IP組播發送變動的數據, IP組播會將數據分發給全部組裏的其餘成員(node).node
配置以下(這裏全部的tomcat都在不一樣的物理主機, 若是在同一臺主機上須要改tomcat的tcpListenPort
)web
Xml代碼apache
- <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>
而後新建一個web應用, 咱們這裏叫TomcatClusterDemo, web context與名稱一致.tomcat
新建一個jsp, 這個jsp複製向session裏建立/更新屬性, 並將session裏的全部屬性顯示在網頁上. session
固然, 爲了驗證咱們的session是同步的, 咱們還將session ID顯示了出來, 代碼以下:app
Jsp代碼webapp
- <%@ page contentType="text/html; charset=UTF-8" %>
- <%@ page import="java.util.*" %>
- <html><head><title>Tomcat Cluster Demo</title></head>
- <body>
- Server Info:
- <%
- out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
- <%
- out.println("<br> ID " + session.getId()+"<br>");
-
- String dataName = request.getParameter("dataName");
- if (dataName != null && dataName.length() > 0) {
- String dataValue = request.getParameter("dataValue");
- session.setAttribute(dataName, dataValue);
- System.out.println("application:" + application.getAttribute(dataName));
- application.setAttribute(dataName, dataValue);
- }
- out.print("<b>Session List</b>");
- Enumeration<String> e = session.getAttributeNames();
- while (e.hasMoreElements()) {
- String name = e.nextElement();
- String value = session.getAttribute(name).toString();
- out.println( name + " = " + value+"<br>");
- System.out.println( name + " = " + value);
- }
- %>
- <form action="test.jsp" method="POST">
- Name:<input type=text size=20 name="dataName">
- <br>
- Value:<input type=text size=20 name="dataValue">
- <br>
- <input type=submit>
- </form>
- </body>
- </html>
同時, 在web.xml裏增長<distributable/>描述
Xml代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name>TomcatClusterDemo</display-name>
- <distributable/>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>test.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
如今將TomcatClusterDemo部署到兩個tomcat上(直接將war包或者部署文件拷貝到webapps下, 或者經過Tomcat Web Application Manager部署), 依次啓動兩個tomcat.
先訪問第一臺tomcat(下面的9.119.84.68)的test.jsp, 並添加幾個session.
而後訪問第二臺tomcat(下面的9.119.84.88)的test.jsp, 爲了告訴tomcat咱們要用那個session訪問, 咱們須要在URL後面加上 ;jsessionid=SESSION_ID
SESSION_ID能夠從第一個test.jsp頁面上得到.
看看效果, 兩個在不一樣server上的tomcat實現了session複製!