1. 程式人生 > 實用技巧 >Tomcat負載均衡及session共享

Tomcat負載均衡及session共享


使用mod_proxy負載均衡:

[[email protected]conf]#vimhttpd.conf
//引入配置檔案
include/usr/local/apache/conf/http-proxy.conf
[[email protected]conf]#vimhttp-proxy.conf
//新增以下內容
ProxyRequestsOff
<proxybalancer://lbcluster1>
BalancerMemberajp://192.168.1.115:8009loadfactor=10route=TomcatA
BalancerMemberajp://192.168.1.116:8009loadfactor=10route=TomcatB
ProxySetlbmethod=bytraffic
</proxy>
<VirtualHost*:80>
ServerAdmin
[email protected]
ServerNamewww.w4ya.com ProxyViaOn ProxyPass/balancer://lbcluster1/stickysession=jsessionid ProxyPa***everse/balancer://lbcluster1/ </VirtualHost> <proxy*> Requireallgranted <proxy> <Location/> Requireallgranted </Location> <VirtualHost*:80> ServerNamewww.w4ya.com <Location/manager> SetHandlerbalancer-manager Requireallgranted </Location> </VirtualHost>


使用mod_jk實現負載均衡:

//引入配置檔案
[[email protected]conf]#vimhttpd.conf
include/usr/local/apache/conf/jk.conf
//新建並編輯vimjk.conf為如下內容:
[[email protected]conf]#vimjk.conf
LoadModulejk_modulemodules/mod_jk.so
JkWorkersFile/usr/local/apache/conf/workers.properties
JkLogFilelogs/mod_jk.log
JkLogLeveldebug
JkShmFilelogs/jk.shm
JkMount/*dragon
JkMount/status/stat
新建並編輯workers.properties,新增如下內容:
[
[email protected]
conf]#vimworkers.properties worker.list=dragon,stat1 worker.TomcatA.type=ajp13 worker.TomcatA.host=192.168.1.115 worker.TomcatA.port=8009 worker.TomcatA.lbfactor=5 worker.TomcatB.type=ajp13 worker.TomcatB.host=192.168.1.116 worker.TomcatB.port=8009 worker.TomcatB.lbfactor=5 worker.dragon.type=lb worker.dragon.sticky_session=1 worker.dragon.balance_workers=TomcatA,TomcatB worker.stat1.type=status [[email protected]conf]#/usr/local/apache/bin/apachectl-t SyntaxOK [[email protected]conf]#/usr/local/apache/bin/apachectlstart



修改tomcat的server.xml,新增叢集配置:

<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<ManagerclassName="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
<MembershipclassName="org.apache.catalina.tribes.membership.McastService"
address="222.222.0.1"//公共iP
port="45564"
frequency="500"
dropTime="3000"/>
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.1.1115"//tomcat本地主機,單網絡卡時也可以auto
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>


在tomcat的網站目錄新建目錄WEB-INFO,並且編輯web.xml,在複製進去。

[[email protected]conf]#mkdir/var/www/html/WEB-INFO
[[email protected]conf]#cp/usr/local/tomcat/conf/web.xml/var/www/html/WEB-INF/
[[email protected]conf]#vim/var/www/html/WEB-INF/web.xml
//在<web-app>之間新增
<distributable/>


檢視到tomcatA和tomcatB交換心跳資訊:

資訊:Serverstartupin3227ms
2014-12-919:33:24org.apache.catalina.tribes.io.BufferPoolgetBufferPool
資訊:Createdabufferpoolwithmaxsize:104857600bytesoftype:org.apache.catalina.tribes.io.BufferPool15Impl
2014-12-919:33:25org.apache.catalina.ha.tcp.SimpleTcpClustermemberAdded
資訊:Replicationmemberadded:org.apache.catalina.tribes.membership.MemberImpl[tcp://{192,168,1,115}:4000,{192,168,1,115},4000,alive=1214,securePort=-1,UDPPort=-1,id={49-69-77157566870-9969794-87-19-99-24},payload={},command={},domain={},]
2014-12-919:33:26org.apache.catalina.ha.session.ClusterSessionListenermessageReceived
資訊:Startingclusteringmanageratwww.w4ya.com#
2014-12-919:32:04org.apache.catalina.ha.session.DeltaManagergetAllClusterSessions
資訊:Manager[www.w4ya.com#],requestingsessionstatefromorg.apache.catalina.tribes.membership.MemberImpl[tcp://{192,168,1,116}:4000,{192,168,1,116},4000,alive=16797,securePort=-1,UDPPort=-1,id={8-18626122-9866-25-68-39107-7-1251087119},payload={},command={},domain={},].Thisoperationwilltimeoutifnosessionstatehasbeenreceivedwithin60seconds.
2014-12-919:32:04org.apache.catalina.tribes.group.interceptors.ThroughputInterceptorreport



在apache檢視負載均衡資訊:

wKioL1SIEDngbrhMAAU-E83Rod4948.jpg


轉載於:https://blog.51cto.com/dragon123/1588398