apache+mod_jk+tomcat集群配置要点
相关软件信息
- 操作系统版本:Windows Professional x64 简体中文版
- Apache 版本:2.2.32 (Win32),下载地址:http://www.apachehaus.com/downloads/httpd-2.2.32-x86-r1.zip
- mod_jk 版本:1.2.31,下载地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.31/mod_jk-1.2.31-httpd-2.2.3.so
- Tomcat 版本:8.5.12
- JDK 版本:1.8.0_121 x64
Apache 方面的配置
mod_jk.so
拷贝 mod_jk-1.2.31-httpd-2.2.3.so 到路径 [apacheRoot]/modules/mod_jk.so 下。
workers.properties
存放路径:[apacheRoot]/conf/workers.properties
内容如下:
worker.list = controller
worker.tomcat1.port=8010
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
# Server的加权比重,值越高,分的请求越多。
worker.tomcat1.lbfactor=1
worker.tomcat2.port=8011
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.controller.type=lb
worker.controller.balance_workers=tomcat1,tomcat2
# 粘性 Session (默认打开)
# 当该属性值 = true (或 1)时,代表 Session 是粘性的,即同一 Session 在集群中的同一个节点上处理,
# Session 不跨越节点。在集群环境中,一般该值设置为 false。
worker.controller.sticky_session=false
# 设置用于负载均衡的 Server 的 session 可否共享
worker.controller.sticky_session_force=1
mod_jk.conf
存放路径:[apacheRoot]/conf/mod_jk.conf
内容如下:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkMount /*.jsp controller
httpd.conf
文件末尾加入:
Include conf/mod_jk.conf
Tomcat 配置
拷贝出两份 Tomcat,分别修改 Server.xml 文件。
项目 | 默认配置 | Tomcat1配置 | Tomcat2配置 | 备注 |
---|---|---|---|---|
server port | 8005 | 8006 | 8007 | 不影响集群,只影响启动,可任意不同值 |
http connnector port | 8080 | 8081 | 8082 | 不影响集群,只影响启动,可任意不同值 |
ajp connnector port | 8009 | 8010 | 8011 | 需与workers.properties中的port保持一致 |
Engine jvmRoute | 无 | tomcat1 | tomcat2 | 需与workers.properties中的balance_workers保持一致 |
Cluster 节点 | 无 | 需追加 | 需追加 | 可参考 Tomcat 文档 cluster-howto.html |
下面是其中一份的完整配置,删除了注释:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 修改port -->
<Server port="8006" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<!-- 修改port -->
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- 修改port -->
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
<!-- 追加jvmRoute属性 -->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<!-- 加入测试用的程序 -->
<Context path="/test" reloadable="true" docBase="E:/jspwebapps" workDir="E:/work" />
</Host>
<!-- 加入Cluster节点 -->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<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="4000"
autoBind="100"
selectorTimeout="5000"
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.MessageDispatchInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<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>
</Engine>
</Service>
</Server>
测试用的应用程序
为了保证 Session 能实现同步复制。需要在 web.xml 加入下面的内容:
<distributable/>
更新日志
2017-05-19
- 未做深入研究,只是做了小小的实验,记录以备后用。