tomcat cluster session 复制方式 集群配置

一、概述

实现多个tomcat的集群,tomcat之间自动复制session,实现在一个tomcat失效后另一个tomcat可用,并有正确的session

二、软件

tomcat6.029

三、配置步骤

a.解压tomcat到文件夹,生成2分,tomcat1和tomcat2

b.分别打开tomcat下的server.xml设置或添加cluster节点,设置下面蓝色字体处,jvmRoute用于区分不同的tomcat

Receiver下的address和port用于设置本tomcat的tcpListenAddress和监听端口,如果两个tomcat在一台电脑需要设置不同的端口,如果不在同一台则可以用最简方式:<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat2">简单设置下jvmRoute,cluster节点去除即可。注意:不同tomcat版本配置中的节点属性可能不一样,可以参考tomcat官网的说明文档http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html

<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat1">

<!--Forclustering,pleasetakealookatdocumentationat:

/docs/cluster-howto.html(simplehowto)

/docs/config/cluster.html(referencedocumentation)-->

<Clusterclassname="org.apache.catalina.ha.tcp.SimpleTcpCluster"

channelSendOptions="8">

<Managerclassname="org.apache.catalina.ha.session.DeltaManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"/>

<Channelclassname="org.apache.catalina.tribes.group.GroupChannel">

<!--address=228.0.0.4不用修改,两个tomcat一样配置-->

<Membershipclassname="org.apache.catalina.tribes.membership.McastService"

address="228.0.0.4"

port="45564"

frequency="500"

dropTime="3000"/>

<Receiverclassname="org.apache.catalina.tribes.transport.nio.NioReceiver"

address="127.0.0.1"

port="4000"

autoBind="100"

selectorTimeout="5000"

maxThreads="6"/>

<Senderclassname="org.apache.catalina.tribes.transport.ReplicationTransmitter">

<Transportclassname="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

</Sender>

<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

</Channel>

<Valveclassname="org.apache.catalina.ha.tcp.ReplicationValve"

filter=""/>

<Valveclassname="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

<Deployerclassname="org.apache.catalina.ha.deploy.FarmWarDeployer"

tempDir="/tmp/war-temp/"

deployDir="/tmp/war-deploy/"

watchDir="/tmp/war-listen/"

watchEnabled="false"/>

<ClusterListenerclassname="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>

<ClusterListenerclassname="org.apache.catalina.ha.session.ClusterSessionListener"/>

</Cluster>

c.在两个项目下的web.xml中分别添加

四,在项目下分别建立test测试文件,在浏览器输入localhost:8080/test.jsp和localhost:8081/test.jsp发现session不变,tomcat标志改变说明在两个tomcat中用的是同一个sessionid。还可以添加session属性,访问两个tomcat的test.jsp都能成功。说明部署成功,

<%@pagecontentType="text/html;charset=utf8"%>

<%@pageimport="java.util.*"%>

<html><head><title>ClusterAppTest</title></head>

<body>

ServerInfo:

<%

out.println(request.getLocalAddr()+":"+request.getLocalPort()+"<br>");%>

<%

out.println("<br>ID"+session.getId()+"<br>");

//如果有新的Session属性设置

StringdataName=request.getParameter("dataName");

if(dataName!=null&&dataName.length()>0){

StringdataValue=request.getParameter("dataValue");

session.setAttribute(dataName,dataValue);

}

out.println("<b>Session列表</b><br>");

System.out.println("============================");

Enumeratione=session.getAttributeNames();

while(e.hasMoreElements()){

Stringname=(String)e.nextElement();

Stringvalue=session.getAttribute(name).toString();

out.println(name+"="+value+"<br>");

System.out.println(name+"="+value);

}

%>

<formaction="test.jsp"method="POST">

名称:<inputtype=textsize=20name="dataName">

<br>

值:<inputtype=textsize=20name="dataValue">

<br>

<inputtype=submit>

</form>

</body>

</html>

相关推荐