之前有研究过apache+resin+tomcat域名转发整合,后来做了apache+resin负载均衡,最近又学习了下apache+tomcat的负载均衡配置方法.
一:准备材料.
1.apache2.2.11
2.tomcat2.0.18
3.mod_jk(我用的是:mod_jk-1.2.28-httpd-2.2.3.so,版本要匹配,比如:apache2.2.x的要用mod_jk1.2.28)
-----------------------------------------------------------------------------------------------------
二:workers.properties创建
workers.properties是用来连接apache和tomcat的连接文件,具体配置如下:
ps=/
worker.list = controller,tomcat1,tomcat2 #server 列表
#========tomcat1========
worker.tomcat1.port=8009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat2.port=8010 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2 #指定分担请求的tomcat
worker.controller.sticky_session=1
/**
*.这里面用了两个tomcat(tomcat1,tomcat2)作为复杂均衡的容器
*.worker.tomcat1.port:为tomcat中用来监听apache转发请求的端口
*.worker.tomcat1.host=localhost:localhost为tomcat所在的ip(如果与apache在同一台服务器则用localhost即可)
*.worker.tomcat1.type=ajp13 指定了tomcat与apache通信的方式
*.worker.tomcat1.lbfactor = 1 制定了此tomcat的加权比重,值越高,分得的请求越多
-------------------------------------
*.controller是一个虚拟的服务,用来对两个tomcat负载均衡
*.worker.controller.type=lb是一种apache转发给tomcat的方式,lb为负载均衡模式(另外还有ajp13,ajp12等等)
*.worker.controller.balanced_workers=tomcat1,tomcat2制定了负载均衡要用到的服务器
*/
-------------------------------------------------------------------------------------------------------
三:tomcat配置
tomcat的conf文件中找到:server.xml并打开
1.多个tomcat在同一服务器上启动的时候要注意端口冲突,需要注意修改以下三个端口
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1"...
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />(此端口即为tomcat与apache的通信端口)
2.在server.xml中
将
<Engine name="Catalina" defaultHost="localhost">
修改为
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">(与workers.properties名称对应)
3.在server.xml中
将
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
替换为
<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="4011"
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>
---------------------------------------------------------------------------------------------------------------
四:apache配置
在apache的conf文件中找到httpd.conf文件并打开
在文件的最末尾加入:
#设置Apache与Tomcat之间的连接,让Apache遇到jsp文件时,在后台将其交由Tomcat去处理
LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.2.3.so
#指定workers.properties文件
JkWorkersFile "D:/temptest/apache2.2/conf/workers.properties"
#指定apache将哪些信息交给tomcat处理
JkMount /* controller
---------------------------------------------------------------------------------------------------------------
五:不要忽略这一步
在你的web工程里面的web.xml中添加:
<distributable/>
这样你的工程才能支持session同步
至此apache与tomcat集群就算完成了
此外还有个问题需要注意一下那就是数据对象的二级缓存的问题,如果数据库方使用的第三方的二级缓存就要注意处理缓存方面的集群问题了
写的不对的地方欢迎大家批评指导,也欢迎大家一起交流!