Tomcat 6 集群配置

tomcat 6.0.16
apache-2.2.8
mod_jk 1.2.26
一、tomcat 配置


修改两台tomcat的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> address及port可修改为不同的值,但集群内的配置必须相同。
在Engine配置项中加入jvmRoute如下:


配置Session复制

所有需要复制的session属性必须实现java.io.Serializable。
需要复制Session的应用web.xml加入<distributable/>元素。
如果使用mod_jk,需要在server.xml中配置Engine的jvmRoute,例如:<Engine name="Catalina" defaultHost="localhost" jvmRoute="node01" >,jvmRoute的名称要和workers.properties中匹配,且集群中的每一个节点都有自己的jvmRoute,不能重复。
集群中的节点时间应使用ntp服务同步。
apache中配置sticky session
需要jdk 1.5


配置apache

将mod_jk.so复制到modules目录中
修改httpd.conf,加入
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /*.jsp controller
在conf目录下新建文本文件workers.properties,内容:
worker.list = controller,node01,node02
worker.node01.port=8009
worker.node01.host=localhost
worker.node01.type=ajp13
worker.node01.lbfactor = 1

worker.node02.port=8009
worker.node02.host=10.100.1.156
worker.node02.type=ajp13
worker.node02.lbfactor = 1

worker.controller.type=lb
worker.controller.balance_workers=node01,node02
worker.controller.sticky_session=1

node01,node02是节点机器的jvmRoute名称,port及host根据具体配置修改

测试
创建一个web应用,在web.xml中添加<distributable/>,应用根目录下编辑test.jsp:
<%@ page contentType="text/html; charset=GBK" %>
<%@ 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.print("<b>Session 列表</b>");
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>

浏览器中打开http://localhost/ClusterSession/test.jsp,添加Session属性,停掉第一个tomcat节点,刷新原有页面,发现Session已经被复制到第二台节点,apache也将浏览器导向了第二节点,集群已经生效。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值