1,copy mod_jk.so 到 apache 的 modules 目录
2,在 apache 的 httpd.conf 文件中添加如下内容
#加载mod_jk Module
LoadModule jk_module modules/mod_jk.so
## 配置 mod_jk
#加载集群中的workers
JkWorkersFile conf/workers.properties
#加载workers的请求处理分配文件
JkMountFile conf/uriworkermap.properties
#指定jk的日志输出文件
JkLogFile logs/mod_jk.log
3,在apache的conf目录里面创建 workers.properties 和 uriworkermap.properties
4,在 workers.properties 中添加
worker.list=loadbalancer, status
#work1
worker.work1.port=8009 #tomcat中ajp端口
worker.work1.host=127.0.0.1
worker.work1.type=ajp13
worker.work1.lbfactor=1 #负载权重
#work2
worker.work2.port=9009
worker.work2.host=127.0.0.1
worker.work2.type=ajp13
worker.work2.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=work1,work2
worker.loadbalancer.sticky_session=true
worker.loadbalancer.sticky_session_force=false
worker.status.type=status
5,在uriworkermap.properties添加
/status=status
/*=loadbalancer
# Exclude the subdirectory static:
!/*/assets|/*=loadbalancer
其中assets为静态文件,此时apache需要配置一个alias别名到tomcat的项目目录,这样可以做到动静分离
6,分别修改tomcat的server.xml文件
<Engine name="Catalina" defaultHost="localhost" jvmRoute="work1">
这里jvmRoute的值和workers.properties文件中对应 worker.<jvmRoute>.<...>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
ajp端口对应workers.properties文件中worker.<jvmRoute>.port
7,分别在tomcat的server.xml文件中Engine节点下添加
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
-->
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="5000"
selectorTimeout="100"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
7,在应用的web.xml中末尾添加
<distributable/>
如果使用terracotta实现session复制,则第6,7两部可以跳过,实际上terracotta来进行session复制是更好的方案,下次附上tomcat用terracotta保存session的配置方法
8,分别启动tomcat和apache,通过访问http://localhost/status可以查看集群状态,访问http://localhost/<tomcat下的应用名称> ,都能成功即配置成功