文章目录
  1. 1. add by zhengxq
  2. 2. Defines a load balancer named lb. Use even if you only have one machine.
  3. 3. Example socket channel, override port and host.
  4. 4. define the worker
  5. 5. Example socket channel, override port and host.
  6. 6. define the worker
  7. 7. Map the Tomcat examples webapp to the Web server uri space

关于Apache+Tomcat的Cluster的郁闷问题,终于得到解决

此类话题已经很多,可以参考的文档也不少,我是参考这个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

<pre><code>

<!— 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”>—>

</code></pre>

B.启用原来禁用的Cluster设置

<pre><code>

<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>

</code></pre>

C.搞定

2)配置第二个Tomcat(要注意端口冲突了):

A.变更端口

<pre><code>

<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”>—>

</code></pre>

D.启用原来禁用的Cluster设置

<pre><code>

<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>

</code></pre>

E.搞定

3、配置apache:

1)修订conf/httpd.conf

<pre><code>

add by zhengxq

LoadModule jk2_module modules/mod_jk2.so

</code></pre>

2)新增worders2.properties并放到conf下

<pre><code>

[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

</code></pre>

3)搞定

3.测试程序,请参考上述参考文档,如下:

<pre><code>

<%@ 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>

</code></pre>

所需要注意的是:

1、测试应用的web.xml必须加上:

2、我搞了很久,发现三个东西起来后,可以访问,但只能做到负载均衡,不能session复制,这点我重复配置了一次,就是不行,郁闷之下找了tomcat的邮件列表,这样做:

1)测试广播是否正常

<pre><code>

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

</code></pre>

发现结果如下:

<pre><code>

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

</code></pre>

表明广播有问题,气死了,难怪Tomcat总是在启动的时候提示:

信息: Manager [/clusterapp]: skipping state transfer. No members active in cluster group.

后来终于发现猫腻,必须注意在server.xml的

为什么呢?很简单,因为我安装了VPN,这家伙会设置一个虚拟网卡,导致绑定失败(没地方责怪了,只好让它垫背)?!这是通过邮件列表里面所描述的多网卡绑定问题举一反三得到的判断。

具体请参考:http://www.servlets.com/archive/servlet/ReadMsg?msgId=475067&listName=tomcat-user

三、tomcat集群配置,session 同步配置:

1、tomca1 tomcat 2 在同一台机器

Tomcat 1 (IP: 192.168.19.199)

A、修改Engine节点信息:

B、去掉 <\Cluster> 的注释符

C、修改Cluster 节点信息

<pre><code>

<Membership

className=”org.apache.catalina.cluster.mcast.McastService” mcastBindAddress=”192.168.19.199”

mcastAddr=”224.0.0.1”

mcastPort=”45564”

mcastFrequency=”500”

mcastDropTime=”3000”/>

<Receiver

className=”org.apache.catalina.cluster.tcp.ReplicationListener” tcpListenAddress=”192.168.19.199”

tcpListenPort=”4001”

tcpSelectorTimeout=”100”

tcpThreadCount=”6”/>

</code></pre>

Tomcat 2 (IP: 192.168.19.81)

A、修改Engine节点信息:

B、去掉 <\Cluster> 的注释符

C、修改Cluster 节点信息

<pre><code>

<Membership

className=”org.apache.catalina.cluster.mcast.McastService”

mcastBindAddress=”192.168.19.199”

mcastAddr=”224.0.0.1”

mcastPort=”45564”

mcastFrequency=”500”

mcastDropTime=”3000”/>

<Receiver

className=”org.apache.catalina.cluster.tcp.ReplicationListener”

tcpListenAddress=”192.168.19.199”

tcpListenPort=”4002” 一定要改

tcpSelectorTimeout=”100”

tcpThreadCount=”6”/>

</code></pre>

D、将8080 8009 8082三个节点的端口改为

9080 9009 9082 避免 与tomcat1端口冲突

注:这里的IP也可以不改

2、tomca1 tomcat 2 在不同机器上

Tomcat 1 (IP: 192.168.19.199)

A、修改Engine节点信息:

B、去掉 <\Cluster> 的注释符

C、修改Cluster 节点信息

<pre><code>

<Membership

className=”org.apache.catalina.cluster.mcast.McastService”

mcastBindAddress=”192.168.19.199”

mcastAddr=”224.0.0.1”

mcastPort=”45564”

mcastFrequency=”500”

mcastDropTime=”3000”/>

<Receiver

className=”org.apache.catalina.cluster.tcp.ReplicationListener”

tcpListenAddress=”192.168.19.199”

tcpListenPort=”4001”

tcpSelectorTimeout=”100”

tcpThreadCount=”6”/>

</code></pre>

Tomcat 2 (IP: 192.168.19.81)

A、修改Engine节点信息:

B、去掉 <\Cluster> 的注释符

C、修改Cluster 节点信息

<pre><code>

<Membership

className=”org.apache.catalina.cluster.mcast.McastService”

mcastBindAddress=”192.168.19.81”

mcastAddr=”224.0.0.1”

mcastPort=”45564” mcastFrequency=”500”

mcastDropTime=”3000”/>

<Receiver

className=”org.apache.catalina.cluster.tcp.ReplicationListener” tcpListenAddress=”192.168.19.81”

tcpListenPort=”4001”

tcpSelectorTimeout=”100”

tcpThreadCount=”6”/>

</code></pre>

修改web应用里面WEB-INF目录下的web.xml文件,加入标签

直接加在之前就可以了

做tomcat集群必须需要这一步,否则用户的session就无法正常使用。

源文档 <http://www.opendoc.com.cn/webserver/Apache_Tomcat_Cluster.html&gt;

文章目录
  1. 1. add by zhengxq
  2. 2. Defines a load balancer named lb. Use even if you only have one machine.
  3. 3. Example socket channel, override port and host.
  4. 4. define the worker
  5. 5. Example socket channel, override port and host.
  6. 6. define the worker
  7. 7. Map the Tomcat examples webapp to the Web server uri space