1、万一这个tomcat挂掉了,整个应用就访问不了
2、当需要重启tomcat时,重启期间应用访问不了
3、当访问量瞬时增大时,单个web server会负载不了
集群可以解决这三个问题。集群、负载均衡、session共享、分布式都是不同的概念。
这些概念在《Tomcat权威指南(第二版)》的集群那一章节有详细介绍。这里不叙述。
简单的集群只需要三步,非常简单:
1、配置tomcat集群
2、安装apache httpd
3、配置httpd
不过有一点需要注意,httpd可以集群,但处理静态页面很慢,我试过直接访问tomcat1很快,但通过httpd访问非常慢。下一篇文章会讨论这个问题的优化。
环境版本:
windows xp+tomcat7.0.57+tomcat7.0.57+httpd2.4.4 测试通过
(aix6.1+tomcat7.0.57+tomcat7.0.57)+(aix6.1+httpd2.4.2) 测试通过
apache http server查看版本方法:
[drecv1@test1:/usr/local/apache/bin]$httpd -V
Server version: Apache/2.4.2 (Unix)
Server built: Jul 18 2012 11:10:49
Server's Module Magic Number: 20120211:3
Server loaded: APR 1.4.6, APR-UTIL 1.4.1
Compiled using: APR 1.4.6, APR-UTIL 1.4.1
配置tomcat集群
1、改端口
见 tomcat使用指南(二)-修改端口
2、增加jvmRoute="jvm1"
假设是两台tomcat,修改server.xml,分别增加为1和2:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">
3、修改server.xml,在host节点后,增加cluster节点:
注意,port="5000"要每台不一样。
<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="5000"
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|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>
<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.ClusterSessionListener"/>
</Cluster>
安装httpd
注意:linux需要安装apr和apr-util,windows是不需要,直接解压zip即可。
http://apr.apache.org/download.cgi
下载apr-1.5.1.tar.gz和apr-util-1.5.4.tar.gzhttp://httpd.apache.org/download.cgi#apache24
下载httpd-2.4.12.tar.gz,解压
把两个apr移动到httpd-2.4.12/srclib下,解压
[builder@crmint-tp02 ~/tomcat/httpd-2.4.12/srclib]$gzip -d apr-1.5.1.tar.gz
[builder@crmint-tp02 ~/tomcat/httpd-2.4.12/srclib]$gzip -d apr-util-1.5.4.tar.gz
[builder@crmint-tp02 ~/tomcat/httpd-2.4.12/srclib]$tar xvf apr-1.5.1.tar
[builder@crmint-tp02 ~/tomcat/httpd-2.4.12/srclib]$tar xvf apr-util-1.5.4.tar
根据官方文档:
http://httpd.apache.org/docs/2.4/install.html
需要把版本去掉:
[builder@crmint-tp02 ~/tomcat/httpd-2.4.12/srclib]$mv apr-1.5.1 apr
[builder@crmint-tp02 ~/tomcat/httpd-2.4.12/srclib]$mv apr-util-1.5.4 apr-util
apr安装
# cd srclib/apr
# ./configure --prefix=/usr/local/apr
# make
# make install
apr、apr-util、httpd安装
./configure --prefix=/usr/local/apr
./configure usr/local/apr-util -enable-lib64 --with-apr=/usr/local/apr --with-expat=builtin
./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite
安装后
[drecv1@bss_test1:/usr/local]$ls
apache apr apr-util bin pcre tar_bak
启动
cd /usr/local/apache2/bin/
./apachectl start
停止
./apachectl stop
apache重新启动命令:
/usr/local/apache2/bin/apachectl restart
附:
没有安装apr的报错:
[builder@crmint-tp02 ~/tomcat/httpd-2.4.12]$./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... no
configure: error: APR not found. Please read the documentation.
[builder@crmint-tp02 ~/tomcat/httpd-2.4.12]$
配置httpd
cd /usr/local/apache/confvi httpd.conf
#把以下8个放开注释
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
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 slotmem_shm_module modules/mod_slotmem_shm.so
在最后(也就是#</IfModule>下面)增加:
ProxyPass / balancer://cluster/ stickysession=JSESSIONID
ProxyPassReverse / balancer://cluster/
<proxy balancer://cluster>
BalancerMember ajp://130.51.9.105:8009 loadfactor=1 route=jvm1
BalancerMember ajp://130.51.9.105:8010 loadfactor=1 route=jvm2
</proxy>
附录:
部分so不放开注释的报错提示与其他报错的解决方法:
[Tue Feb 03 19:58:24.046875 2015] [proxy_balancer:emerg] [pid 4644:tid 1724] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
需放开mod_slotmem_shm注释
[Tue Feb 03 19:59:57.437500 2015] [proxy:crit] [pid 6896:tid 1716] AH02432: Cannot find LB Method: byrequests
[Tue Feb 03 19:59:57.437500 2015] [proxy_balancer:emerg] [pid 6896:tid 1716] (22)Invalid argument: AH01183: Cannot share balancer
需放开lbmethod_byrequests_module
[Tue Feb 03 19:55:35.640625 2015] [core:warn] [pid 5980:tid 1736] AH00098: pid file F:/Apache24/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
删掉pid文件可解决
F:\Apache24\bin>httpd.exe
AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using 130.51.23.43. Set the 'ServerName' directive globally to suppress this message
#ServerName www.example.com:80改名ServerName 30.51.23.43
F:\Apache24\bin>httpd.exe
httpd.exe: Syntax error on line 37 of F:/Apache24/conf/httpd.conf: ServerRoot must be a valid directory
解决:如果你不是解压在c:/Apache24这个默认目录,则需要修改ServerRoot "f:/Apache24"等4处地方
本文出处:
http://blog.csdn.net/ouyida3
2015.2.6