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在一台电脑需要设置不同的端口,如果不在同一台则可以用最简方式:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> 简单设置下jvmRoute ,cluster节点去除即可。[color=blue]注意[/color]:不同tomcat版本配置中的节点属性可能不一样,可以参考tomcat官网的说明文档http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html

<Engine name="Catalina" defaultHost="localhost"[color=blue] jvmRoute="tomcat1"[/color]>

<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->

<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">
<!-- address = 228.0.0.4 不用修改,两个tomcat一样配置-->
<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"
[color=blue]address="127.0.0.1"
port="4000"[/color]
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>


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

四,在项目下分别建立test测试文件,在浏览器输入localhost:8080/test.jsp和localhost:8081/test.jsp 发现session不变,tomcat标志改变说明在两个tomcat中用的是同一个sessionid。还可以添加session属性,访问两个tomcat的test.jsp都能成功。说明部署成功,
<%@ page contentType="text/html; charset=utf8" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
// 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.println("<b>Session 列表</b><br>");
System.out.println("============================");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
名称:<input type=text size=20 name="dataName">
<br>
值:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页