---
title: "Apache Tomcat 集群"
author: "shellfj"
date: "2012-01-07T01:43:40Z"
tags: []
categories: ["企业架构"]
slug: "apache-tomcat"
draft: false
lastmod: "2012-01-07T01:49:19Z"
id: 1337068
url: "https://shellfj.iteye.com/blog/1337068"
---

经过2天的查资料和实验，在2台机器上完成了集群的配置。
Apache2.2.21,Mod_JK 1.2.32,Tomcat 7.0.23.
用Mod JK，apache 配置load balance。
配置如下：
Apache httpd:
# Load mod_jk module. Specify the filename
# of the mod_jk lib you’ve downloaded and
# installed in the previous section
LoadModule jk_module modules/mod_jk.so

```java

#begin JK config
# Where to find workers.properties
JkWorkersFile conf/workers.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]"
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Add the jkstatus mount point
<Location /jkmanager/>
    JkMount jkstatus
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Location>
JkMount /* loadbalancer
#end JK config

```

workers.properties:

```java

# Define list of workers that will be used  
# for mapping requests  
worker.list=loadbalancer,jkstatus  
  
# Define Node1  
# modify the host as your host IP or DNS name.  
worker.node1.port=8009 
worker.node1.host=127.0.0.1
worker.node1.type=ajp13  
worker.node1.lbfactor=1
  
# Define Node2  
# modify the host as your host IP or DNS name.  
worker.node2.port=8109  
worker.node2.host=127.0.0.1  
worker.node2.type=ajp13  
worker.node2.lbfactor=1  

worker.node3.port=8009  
worker.node3.host=192.168.0.3 
worker.node3.type=ajp13  
worker.node3.lbfactor=1

# Load-balancing behaviour  
worker.loadbalancer.type=lb  
worker.loadbalancer.balance_workers=node1,node2,node3
worker.loadbalancer.sticky_session=1  
#worker.list=loadbalancer  
  
# Status worker for managing load balancer  
worker.jkstatus.type=status

```

关于反向代理，Tomcat AJP已经支持.<http://tomcat.apache.org/connectors-doc/generic_howto/proxy.html>
AJP配置<http://tomcat.apache.org/connectors-doc/index.html>
下面关于Http Session的复制：
Tomcat Catalina Tribes提供了复制功能，这个也能在官方文档上找到。当然，可以考虑使用Jgroups等实现。配置详细说明<http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html>.
完全按照官方文档可以实现。具体其他详细的配置，在需要的时候，去查看Tribes的配置信息。
集群测试用列见附件.
注意在webapp中web.xml需要加上<distributable/>.
在本地已经测试成功！
另外，Tribes使用IP广播在集群节点之间传输信息。在本地配置集群时， Receiver:className="org.apache.catalina.tribes.transport.nio.NioReceiver"的port不能与另外一个相同!
并且要关闭本地的firewall。
使用tomcat-replication.jar<http://cvs.apache.org/~fhanik/tomcat-replication.jar>测试IP广播。

```java
java -cp tomcat-replication.jar MCaster 228.0.0.4 45564 Terminal1
```

在Tribes中，对于使用静态的成员配置，见<https://issues.apache.org/bugzilla/show_bug.cgi?id=52263>.