此类话题已经很多,可以参考的文档也不少,我是参考这个blog的文档做的:
http://blogger.org.cn/blog/blog.asp?name=lhwork
1)环境和版本:
Apache2.0.59+Tomcat5.5.15(两个),一开始用的是Tomcat5.0.28,一直有问题(在后面总结),就升级到5.5去做试验了,冤枉5.0.28兄弟了:)
步骤小结为:
1、安装(忽略);
2、配置Tomcat:
1)第一个Tomcat:
A.启用jvmRoute
<!-- You should set jvmRoute to support load-balancing via AJP ie :-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">
<!-- Define the top level container in our container hierarchy
<Engine name="Catalina" defaultHost="localhost">-->
B.启用原来禁用的Cluster设置
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastBindAddress="127.0.0.1"
mcastAddr="224.1.2.3"
mcastPort="2525"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="15000"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
C.搞定
2)配置第二个Tomcat(要注意端口冲突了):
A.变更端口
<Server port="8005" shutdown="SHUTDOWN">
=》
<Server port="8004" shutdown="SHUTDOWN">
B.变更端口
<Connector port="8009"
enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
=》
<Connector port="9009"
enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
C.启用jvmRoute
<!-- You should set jvmRoute to support load-balancing via AJP ie :-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat2">
<!-- Define the top level container in our container hierarchy
<Engine name="Catalina" defaultHost="localhost">-->
D.启用原来禁用的Cluster设置
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastBindAddress="127.0.0.1"
mcastAddr="224.1.2.3"
mcastPort="2525"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4002"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="15000"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
E.搞定
3、配置apache:
1)修订conf/httpd.conf
#add by zhengxq
LoadModule jk2_module modules/mod_jk2.so
2)新增worders2.properties并放到conf下
[shm]
info=Scoreboard. Requried for reconfiguration and status with multiprocess servers.
file=anon
# Defines a load balancer named lb. Use even if you only have one machine.
[lb:lb]
worker=ajp13:tomcat1
worker=ajp13:tomcat2
# Example socket channel, override port and host.
[channel.socket:localhost:9009]
port=9009
host=127.0.0.1
# define the worker
[ajp13:localhost:9009]
channel=channel.socket:localhost:9009
group=lb
# Example socket channel, override port and host.
[channel.socket:localhost:8009]
port=8009
host=127.0.0.1
# define the worker
[ajp13:localhost:8009]
channel=channel.socket:localhost:8009
group=lb
# Map the Tomcat examples webapp to the Web server uri space
[uri:/clusterapp/*]
group=lb
3)搞定
3.测试程序,请参考上述参考文档,如下:
<%@ page contentType="text/html; charset=GBK" import="java.util.*"%>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info: <%out.print(request.getLocalAddr() + " : " + request.getLocalPort());%>
<%
out.println("<br> ID " + session.getId());
// 如果有新的 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);
}
%>
<form action="index.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>
所需要注意的是:
1、测试应用的web.xml必须加上:<distributable/>
2、我搞了很久,发现三个东西起来后,可以访问,但只能做到负载均衡,不能session复制,这点我重复配置了一次,就是不行,郁闷之下找了tomcat的邮件列表,这样做:
1)测试广播是否正常
A. download this jar
http://cvs.apache.org/~fhanik/tomcat-replication.jar
B. Open two terminals
a) Terminal one run
java -cp tomcat-replication.jar MCaster 239.255.10.10 2525 Terminal1
b) Terminal two run
java -cp tomcat-replication.jar MCaster 239.255.10.10 2525 Terminal2
发现结果如下:
C:\>java -cp tomcat-replication.jar MCaster 224.1.2.3 2525 Terminal1
Usage MCaster [address port message]
BEGIN TO RECEIVE
SENT:Terminal11
SENT:Terminal12
SENT:Terminal13
SENT:Terminal14
SENT:Terminal15
SENT:Terminal16
SENT:Terminal17
SENT:Terminal18
表明广播有问题,气死了,难怪Tomcat总是在启动的时候提示:
信息: Manager [/clusterapp]: skipping state transfer. No members active in cluster group.
后来终于发现猫腻,必须注意在server.xml的<Cluster ..<Membership里面加上mcastBindAddress="127.0.0.1"。
为什么呢?很简单,因为我安装了VPN,这家伙会设置一个虚拟网卡,导致绑定失败(没地方责怪了,只好让它垫背)?!这是通过邮件列表里面所描述的多网卡绑定问题举一反三得到的判断。具体请参考:http://www.servlets.com/archive/servlet/ReadMsg?msgId=475067&listName=tomcat-user