建立tomcat集群
以一个apache2.2前端,2个tomcat6后端为例,tomcat服务器在内网,apache服务器具有内网和外网接口。本文使用JK模块实现负载均衡。一、建立tomcat负载均衡
1.修改tomcat的conf目录下的server.xml文件,
去掉对jvmRoute这一行的注释,并且把jvmRoute的值改为例如tomcat1这样容易记忆的字符。
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
把2个tomcat服务器上的上述文件都作修改。
2.在apache的conf目录下建立workers.properties文件,内容如下
# The advanced router LB workerworker.list=router# Define a worker using ajp13worker.worker1.port=8009worker.worker1.host=192.168.56.3worker.worker1.type=ajp13worker.worker1.lbfactor=1# Define preferred failover node for worker1worker.worker1.redirect=worker2worker.worker1.route=tomcat1# Define another worker using ajp13worker.worker2.port=8009worker.worker2.host=192.168.56.4worker.worker2.type=ajp13worker.worker2.lbfactor=1# Disable worker2 for all requests except failover#worker.worker2.activation=disabledworker.worker2.route=tomcat2# Define the LB workerworker.router.type=lbworker.router.balance_workers=worker1,worker2#worker.router.sticky_session_force=1# Add the status worker to the worker listworker.list=jkstatus# Define a 'jkstatus' worker using statusworker.jkstatus.type=status
其中的
worker.worker1.route=tomcat1
中的tomcat1对应第1个tomcat服务器中上述server.xml中的jvmRoute的内容。
同样的,
worker.worker2.route=tomcat2
中的tomcat2对应第2个tomcat服务器中上述server.xml中的jvmRoute的内容。
3.在apache的conf目录下的httpd.conf文件末尾添加如下内容
# Load mod_jk module# Update this path to match your modules locationLoadModule jk_modulemodules/mod_jk.so# Declare the module for <IfModule directive> (remove this line on Apache 2.x)#AddModule mod_jk.c# Where to find workers.properties# Update this path to match your conf directory location (put workers.properties next to httpd.conf)JkWorkersFile conf/workers.properties# Where to put jk shared memory
4.为了能够在运行时查看jk的状态,在上述内容末尾再添加如下内容
# Update this path to match your local state directory or logs directoryJkShmFile logs/mod_jk.shm# Where to put jk logs# Update this path to match your logs directory location (put mod_jk.log next to access_log)JkLogFile logs/mod_jk.log# Set the jk log level JkLogLevel info# Select the timestamp log formatJkLogStampFormat "[%a %b %d %H:%M:%S %Y] "# Send everything for context /examples to worker named worker1 (ajp13)JkMount/examples/* router# Add the jkstatus mount pointJkMount /jkmanager/* jkstatus# Enable the JK manager access from localhost only<Location /jkmanager/>JkMount jkstatusOrder deny,allowDeny from allAllow from 127.0.0.1</Location>
5.需要注意的是,JK模块的默认配置是支持session黏性的,即用户的session在其生命周期内始终保存在一个tomcat服务器上。
关于如何设置为非sessin黏性等,请参考tomcat官方文档:LoadBalancer HowTo http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html
二、配置session复制
1.修改tomcat的conf目录下的server.xml文件,
去掉对这一行的注释,即可使tomcat服务支持session复制。
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
2.修改希望复制session的应用的WEB-INF目录下的web.xml文件,
在末尾增加一行
<distributable/>
以使该应用支持session复制。
把2个tomcat服务器上的希望实现session复制的应用中的上述文件都作修改。
3.分别先后启动2个tomcat服务,你会发现启动信息中增加了一些session复制的信息。当第1个tomcat启动完毕,在启动第2个tomcat服务时,log里面会记录寻找可作session复制的第1个tomcat的信息。即寻找簇内服务器的过程。
当停止任何一个tomcat服务后,也会在正在运行的tomcat服务的log中看到有那个tomcat服务停止了的消息。
4.如果想实现更复杂的sessioin复制功能,可以参考tomcat官方文档:
Clustering/Session Replication HOW-TO http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html
页:
[1]