---
title: "Tomcat集群概要"
author: "cherami"
date: "2007-09-05T03:59:50Z"
tags: []
slug: "tomcat"
draft: false
lastmod: "2007-09-05T03:59:50Z"
id: 120831
url: "https://cherami.iteye.com/blog/120831"
---

其实已经有很多文档了，不过还是老话，给自己备忘，总结些要注意的要点，给一些范例，以后肯定用得着。

以JK作为Apache和Tomcat的连接器，首先配置的是Apache的httpd.conf，主要是加载JK和配置转发规则：  
加载和配置JK：  
[# Load mod\_jk module  
\# Specify the filename of the mod\_jk lib  
LoadModule jk\_module modules/mod\_jk.so]()

[# Where to find workers.properties  
JkWorkersFile workers2.properties  
\
\# Where to put jk logs  
JkLogFile logs/mod\_jk.log  
\
\# Set the jk log level \[debug/error/info\]  
JkLogLevel info  
\
\# Select the log format  
JkLogStampFormat  "\[%a %b %d %H:%M:%S %Y\]"  
\
\# JkOptions indicates to send SSK KEY SIZE  
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories  
\
\# JkRequestLogFormat  
JkRequestLogFormat "%w %V %T"  
\
配置转发规则：  
JKMount /*  loadbalancer  
这个上面的配置差异比较大，简单起见全部转发，可以根据自己的需要修改  
\
然后是定义Workers：  
\
info=Scoreboard. Requried for reconfiguration and status with multiprocess servers.  
#file=anon  
file=logs/shm.file  
size=1048576  
\
\# Defines a load balancer named lb. Use even if you only have one machine.  
\[lb:lb\]  
\
\# Example socket channel, override port and host.  
\[channel.socket:192.168.1.16:8009\]  
port=8009  
host=192.168.1.16  
\
\[channel.socket:192.168.1.18:8009\]  
port=8009  
host=192.168.1.18  
\
\# Map the Tomcat examples webapp to the Web server uri space  
worker.list=loadbalancer  
\
worker.worker1.port=8009  
worker.worker1.host=192.168.1.16  
worker.worker1.type=ajp13  
worker.worker1.lbfactor=1  
worker.worker1.cachesize=60  
worker.worker1.cache\_timeout=300  
worker.worker1.socket\_keepalive=1  
worker.worker1.socket\_timeout=180  
worker.worker1.connect\_timeout=0  
worker.worker1.reply\_timeout=0  
worker.worker1.recovery\_options=1  
worker.worker1.retries=1  
\
worker.worker2.port=8009  
worker.worker2.host=192.168.1.18  
worker.worker2.type=ajp13  
worker.worker2.lbfactor=1  
worker.worker2.cachesize=60  
worker.worker2.cache\_timeout=300  
worker.worker2.socket\_keepalive=1  
worker.worker2.socket\_timeout=180  
worker.worker2.connect\_timeout=0  
worker.worker2.reply\_timeout=0  
worker.worker2.recovery\_options=1  
worker.worker2.retries=1  
\
worker.loadbalancer.type=lb  
worker.loadbalancer.balance\_workers=worker1,worker2  
worker.loadbalancer.sticky\_session=1  
\
\[uri:/\*\]  
group=lb  
\
\[status:status\]  
info=Status worker, displays runtime informations  
\
\# Status worker for managing load balancer  
\# worker.status.type=status  
\
\[uri:/jkstatus/\*\]  
info=Display status information and checks the config file for changes.  
group=status:status]()

[上面的jkstatus的配置是可选的，在开发环境中打开，但是在产品环境中最好关闭。  
使用了粘性Session（worker.loadbalancer.sticky\_session=1），所以Tomcat的Server.xml中的Engine元素需要添加jvmRoute属性，值就是对应的worker的名字，另外Server.xml中的cluster配置不要打开，否则会进行session的复制。  
相关的文章：]()  
[http://tomcat.apache.org/connectors-doc/generic\_howto/loadbalancers.html](http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html)  
[http://tomcat.apache.org/tomcat-5.0-doc/cluster-howto.html](http://tomcat.apache.org/tomcat-5.0-doc/cluster-howto.html)  
[http://blog.csdn.net/ladofwind/archive/2006/08/29/1138484.aspx](http://blog.csdn.net/ladofwind/archive/2006/08/29/1138484.aspx)  
[http://tomcat.jaxmao.org/cluster-howto.html](http://tomcat.jaxmao.org/cluster-howto.html)  
[http://www.matrix.org.cn/thread.shtml?topicId=39848&forumId=27](http://www.matrix.org.cn/thread.shtml?topicId=39848&amp%3BforumId=27)  
[](http://blog.csdn.net/ladofwind/archive/2006/08/29/1138484.aspx)

作者: [解惑/Cherami](http://www.jiehoo.com)  
原载: [Tomcat集群概要](http://www.jiehoo.com/tomcat%e9%9b%86%e7%be%a4%e6%a6%82%e8%a6%81.htm)  
**版权所有。转载时必须以链接形式注明作者和原始出处及本声明。**  
分类: [Java](http://www.jiehoo.com/category/java/ "显示Java的所有文章")