安装Apache后进行如下配置:
1. 修改 httpd.conf文件,增加如下一行
include "%你的apache安装路径%/conf/mod_jk.conf"
2. 将mod_jk插件拷贝到apache的modules文件夹中
3. 添加如下文件到Apache路径下的conf文件夹中
1) mod_jk.conf 内容如下:
#加载mod_jk Module
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
JkMountFile conf/urlworkers.properties
#指定JK日志输出文件
JkLogFile logs/mod_jk.log
#指定日志级别
JkLogLevel warn
#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器
JkMount /* controller
2) workers.properties 内容如下:
worker.list = controller #server 列表
worker.maintain=60 #worker线程统一时间
#========tomcat1========
worker.tomcat1.port=8009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13 #监听协议为AJP 1.3
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
worker.tomcat1.socket_keepalive=true #底层socket连接使用长连接
worker.tomcat1.socket_timeout=20 #socket连接超时为20秒,代表每个tcp读写操作的timeout
worker.tomcat1.connect_timeout=10000 #连接超时
worker.tomcat1.prepost_timeout=10000
worker.tomcat1.connection_pool_size=250 #连接池大小
worker.tomcat1.connection_pool_minsize=25 #最小连接数
worker.tomcat1.connection_pool_timeout=600 #连接池超时
#========tomcat2========
worker.tomcat2.port=8009
worker.tomcat2.host=10.0.103.23
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
worker.tomcat2.socket_keepalive=true
worker.tomcat2.socket_timeout=20
worker.tomcat2.connect_timeout=10000
worker.tomcat2.prepost_timeout=10000
worker.tomcat2.connection_pool_size=250
worker.tomcat2.connection_pool_minsize=25
worker.tomcat2.connection_pool_timeout=600
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2 #指定分担请求的tomcat
worker.controller.sticky_session=1 # 等同于worker.controller.sticky_session=true.此处指定集群是否需要会话复制,如果设为true,则表明为会话粘性,不进行会话复制,当某用户的请求第一次分发到哪台Tomcat后,后继的请求会一直分发到此Tomcat服务器上处理;如果设为false,则表明需求会话复制。
3) urlworkers.properties 内容如下:
/*=controller
/jkstatus=status
/*.gif=controller
/*.jpg=controller
/*.png=controller
/*.css=controller
/*.js=controller
/*.htm=controller
/*.html=controller
/*.jsp=controller
/*.do=controller
4. 配置tomcat的server.xml文件
-- 配置ajp端口,与workers.properties里配置的相对应
<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
-- 配置jvmRoute,与workers.properties里的相对应
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
-- 释放cluster标签
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="15000"
waitForAck="true"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>