tomcat集群配置

测试环境:2台电脑xp系统

所需软件:

httpd-2.2.22-win32-x86-openssl-0.9.8t.msi (http://httpd.apache.org/)

apache-tomcat-6.0.35.zip (http://tomcat.apache.org/)

花生壳(http://www.oray.com/) (可选,外网测试项目使用)

 设计如图:

 
tomcat集群配置_第1张图片

将 apache-tomcat-6.0.35.zip解压2次,分别得到tomcat_01,tomcat_02.

配置tomcat_01/conf/server.xml:

修改<Engine name="Catalina" defaultHost="localhost"> 为<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

在该元素标签下面添加如下元素:

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<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="5001"
selectorTimeout="100"
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"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

配置tomcat_02/conf/server.xml,因为tomcat_02和tomcat_01在同一台电脑上,所以端口不能重复,如果是在不同电脑上的话,就可以不用修改端口:

修改<Server port="8005" shutdown="SHUTDOWN">

为<Server port="8015" shutdown="SHUTDOWN">

修改 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8" />

为<Connector port="8888" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8" />

修改<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

为<Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />

修改<Engine name="Catalina" defaultHost="localhost">

为<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

在Engine元素标签下面也添加如下代码,此时Receiver的端口不能重复:

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<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="5002"
selectorTimeout="100"
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"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

 配置tomcat_03/conf/server.xml,和tomcat_01配置一样,只是在另一台电脑上。

启动3个tomcat,访问3个地址:

http://192.168.0.100:8080/

http://192.168.0.100:8888/

http://192.168.0.102:8080/

出现了tomcat首页,说明启动正常。

安装好 httpd-2.2.22-win32-x86-openssl-0.9.8t.msi ,接下来配置http信息。 conf/httpd.conf:

默认端口是80,可以进行修改Listen 80 

1.将下面代码前面的#去掉:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so

2.找到如下信息,修改为 allow from all:

<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
#Deny from all
allow from all
</Directory>

 不然访问的时候就会出现 You don't have permission to access / on this server

3.找到如下代码Include conf/extra/httpd-vhosts.conf,去掉前面注释。

4.在文件最后面添加信息如下:

ProxyRequests Off
<proxy balancer://cluster>
BalancerMember ajp://192.168.0.100:8009 loadfactor=1 route=tomcat1
BalancerMember ajp://192.168.0.100:8019 loadfactor=2 route=tomcat2
BalancerMember ajp://192.168.0.102:8009 loadfactor=3 route=tomcat3
</proxy>

这里的端口就是tomcat里面 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />的端口。

route=tomcat1就是tomcat里面 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">的名字。

 loadfactor值越大表示分配到的可能性越大。

5.修改conf/extra/httpd-vhosts.conf,在文件最后添加如下代码:

<VirtualHost *:80>
ServerAdmin [email protected]
#ServerName localhost
#ServerAlias localhost
#ServerName 192.168.0.100
#ServerAlias 192.168.0.100
ServerName cjjava.vicp.cc
ServerAlias cjjava.vicp.cc
ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On
ProxyPassReverse / balancer://cluster/
</VirtualHost>

如果 ServerName设置为localhost,则只能在本机中测试访问服务 ,即:http://localhost

如果 ServerName设置为局域网ip,则局域网能够通过ip测试访问服务 ,即:http://192.168.0.100

如果 ServerName设置为域名,则可以通过域名测试访问服务。 ,即:http://cjjava.vicp.cc 

这里我通过花生壳进行了外网映射。

启动apache server,访问下面地址:

http://cjjava.vicp.cc/

如果看到tomcat首页,则表示访问成功。

外网访问,这里我是连接的路由器,所以对其端口进行了虚拟服务器列表配置。


 

下面创建一个web项目对其进行session的复制测试,

将项目打包后分别放入到3个tomcat/wabapps/web-0.0.1.war中。

注意web项目中web.xml中要添加如下信息:<distributable/> 

访问下面地址:http://cjjava.vicp.cc/web-0.0.1/

进入index.jsp中。 多刷新几次页面,可以看到tomcat_03输出结果:

 
tomcat集群配置_第2张图片
 tomcat_02输出结果:


tomcat集群配置_第3张图片

 

这个频率和刚才配置的以下信息有关系, loadfactor值越大,被分配的可能性越大。

BalancerMember ajp://192.168.0.100:8019 loadfactor=2 route=tomcat2
BalancerMember ajp://192.168.0.102:8009 loadfactor=3 route=tomcat3

 

 以上测试完成:)

 

 

 

你可能感兴趣的:(tomcat集群)