建立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 worker worker.list=router # Define a worker using ajp13 worker.worker1.port=8009 worker.worker1.host=192.168.56.3 worker.worker1.type=ajp13 worker.worker1.lbfactor=1 # Define preferred failover node for worker1 worker.worker1.redirect=worker2 worker.worker1.route=tomcat1 # Define another worker using ajp13 worker.worker2.port=8009 worker.worker2.host=192.168.56.4 worker.worker2.type=ajp13 worker.worker2.lbfactor=1 # Disable worker2 for all requests except failover #worker.worker2.activation=disabled worker.worker2.route=tomcat2 # Define the LB worker worker.router.type=lb worker.router.balance_workers=worker1,worker2 #worker.router.sticky_session_force=1 # Add the status worker to the worker list worker.list=jkstatus # Define a 'jkstatus' worker using status worker.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 location LoadModule jk_module modules/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 directory JkShmFile 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 [debug/error/info] JkLogLevel info # Select the timestamp log format JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " # Send everything for context /examples to worker named worker1 (ajp13) JkMount /examples/* router # Add the jkstatus mount point JkMount /jkmanager/* jkstatus # Enable the JK manager access from localhost only <Location /jkmanager/> JkMount jkstatus Order deny,allow Deny from all Allow 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值