建立tomcat集群

转  http://blog.youkuaiyun.com/jackyyen/article/details/5322745


以一个apache2.2前端,2个tomcat6后端为例,tomcat服务器在内网,apache服务器具有内网和外网接口。本文使用JK模块实现负载均衡。

一、建立tomcat负载均衡

1.修改tomcat的conf目录下的server.xml文件,

去掉对jvmRoute这一行的注释,并且把jvmRoute的值改为例如tomcat1这样容易记忆的字符。

[xhtml]  view plain copy
  1. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">  

把2个tomcat服务器上的上述文件都作修改。

2.在apache的conf目录下建立workers.properties文件,内容如下

[cpp]  view plain copy
  1. # The advanced router LB worker  
  2. worker.list=router  
  3. # Define a worker using ajp13  
  4. worker.worker1.port=8009  
  5. worker.worker1.host=192.168.56.3  
  6. worker.worker1.type=ajp13  
  7. worker.worker1.lbfactor=1  
  8. # Define preferred failover node for worker1  
  9. worker.worker1.redirect=worker2  
  10. worker.worker1.route=tomcat1  
  11. # Define another worker using ajp13  
  12. worker.worker2.port=8009  
  13. worker.worker2.host=192.168.56.4  
  14. worker.worker2.type=ajp13  
  15. worker.worker2.lbfactor=1  
  16. # Disable worker2 for all requests except failover  
  17. #worker.worker2.activation=disabled  
  18. worker.worker2.route=tomcat2  
  19.    
  20. # Define the LB worker  
  21. worker.router.type=lb  
  22. worker.router.balance_workers=worker1,worker2  
  23. #worker.router.sticky_session_force=1  
  24. # Add the status worker to the worker list  
  25. worker.list=jkstatus  
  26. # Define a 'jkstatus' worker using status  
  27. worker.jkstatus.type=status  

其中的

[c-sharp]  view plain copy
  1. worker.worker1.route=tomcat1  
 

中的tomcat1对应第1个tomcat服务器中上述server.xml中的jvmRoute的内容。

同样的,

[c-sharp]  view plain copy
  1. worker.worker2.route=tomcat2  
 

中的tomcat2对应第2个tomcat服务器中上述server.xml中的jvmRoute的内容。

 

3.在apache的conf目录下的httpd.conf文件末尾添加如下内容

[c-sharp]  view plain copy
  1. # Load mod_jk module  
  2. # Update this path to match your modules location  
  3. LoadModule    jk_module  modules/mod_jk.so  
  4. # Declare the module for <IfModule directive> (remove this line on Apache 2.x)  
  5. #AddModule     mod_jk.c  
  6. # Where to find workers.properties  
  7. # Update this path to match your conf directory location (put workers.properties next to httpd.conf)  
  8. JkWorkersFile conf/workers.properties  
  9. # Where to put jk shared memory  

 

4.为了能够在运行时查看jk的状态,在上述内容末尾再添加如下内容

[c-sharp]  view plain copy
  1.  # Update this path to match your local state directory or logs directory  
  2.  JkShmFile     logs/mod_jk.shm  
  3.  # Where to put jk logs  
  4.  # Update this path to match your logs directory location (put mod_jk.log next to access_log)  
  5.  JkLogFile     logs/mod_jk.log  
  6.  # Set the jk log level [debug/error/info]  
  7.  JkLogLevel    info  
  8.  # Select the timestamp log format  
  9.  JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "  
  10.  # Send everything for context /examples to worker named worker1 (ajp13)  
  11.  JkMount  /examples/* router  
  12.  # Add the jkstatus mount point  
  13.  JkMount /jkmanager/* jkstatus  
  14.    # Enable the JK manager access from localhost only  
  15. <Location /jkmanager/>  
  16.    JkMount jkstatus  
  17.    Order deny,allow  
  18.    Deny from all  
  19.    Allow from 127.0.0.1  
  20. </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复制。

[xhtml]  view plain copy
  1. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>  

2.修改希望复制session的应用的WEB-INF目录下的web.xml文件,

在末尾增加一行

 

[xhtml]  view plain copy
  1. <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、付费专栏及课程。

余额充值