使用mod_proxy负载均衡:

[root@localhost conf]# vim httpd.conf
//引入配置文件
include /usr/local/apache/conf/http-proxy.conf 
[root@localhost conf]# vim http-proxy.conf 
//添加以下内容
ProxyRequests Off
<proxy balancer://lbcluster1>
BalancerMember ajp://192.168.1.115:8009 loadfactor=10 route=TomcatA
BalancerMember ajp://192.168.1.116:8009 loadfactor=10 route=TomcatB
ProxySet lbmethod=bytraffic
</proxy>
<VirtualHost *:80>
ServerAdmin admin@w4ya.com
ServerName www.w4ya.com
ProxyVia On
ProxyPass / balancer://lbcluster1/ stickysession=jsessionid
ProxyPa***everse / balancer://lbcluster1/
</VirtualHost>
<proxy *>
Require all granted
<proxy>
<Location />
Require all granted
</Location>
<VirtualHost *:80>
 ServerName www.w4ya.com
 <Location /manager>
  SetHandler balancer-manager
  Require all granted
  </Location>
</VirtualHost>


  

  

使用mod_jk实现负载均衡:

//引入配置文件
[root@localhost conf]# vim httpd.conf
include /usr/local/apache/conf/jk.conf
//新建并编辑 vim jk.conf 为如下内容:
[root@localhost conf]# vim jk.conf 
LoadModule  jk_module  modules/mod_jk.so
JkWorkersFile  /usr/local/apache/conf/workers.properties
JkLogFile  logs/mod_jk.log
JkLogLevel  debug
JkShmFile logs/jk.shm
JkMount  /*  dragon
JkMount  /status/  stat
新建并编辑workers.properties,添加如下内容:
[root@localhost conf]# vim workers.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
[root@localhost conf]# /usr/local/apache/bin/apachectl -t
Syntax OK
[root@localhost conf]# /usr/local/apache/bin/apachectl start



修改tomcat的server.xml,添加集群配置:

<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="222.222.0.1"  //公共iP
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.1.1115"  //tomcat本地主机,单网卡时也可以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.MessageDispatch15Interceptor"/>
</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.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>


在tomcat的网站目录新建目录WEB-INFO,并且编辑web.xml,在复制进去。

[root@localhost conf]#mkdir /var/www/html/WEB-INFO 
[root@localhost conf]# cp /usr/local/tomcat/conf/web.xml /var/www/html/WEB-INF/
[root@localhost conf]# vim  /var/www/html/WEB-INF/web.xml
//在<web-app>之间添加
<distributable/>


查看到tomcatA和tomcatB交换心跳信息:

信息: Server startup in 3227 ms
2014-12-9 19:33:24 org.apache.catalina.tribes.io.BufferPool getBufferPool
信息: Created a buffer pool with max size:104857600 bytes of type:org.apache.catalina.tribes.io.BufferPool15Impl
2014-12-9 19:33:25 org.apache.catalina.ha.tcp.SimpleTcpCluster memberAdded
信息: Replication member added:org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 1, 115}:4000,{192, 168, 1, 115},4000, alive=1214, securePort=-1, UDP Port=-1, id={49 -69 -77 15 75 6 68 70 -99 69 79 4 -87 -19 -99 -24 }, payload={}, command={}, domain={}, ]
2014-12-9 19:33:26 org.apache.catalina.ha.session.ClusterSessionListener messageReceived
信息: Starting clustering manager at www.w4ya.com#
2014-12-9 19:32:04 org.apache.catalina.ha.session.DeltaManager getAllClusterSessions
信息: Manager [www.w4ya.com#], requesting session state from org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 1, 116}:4000,{192, 168, 1, 116},4000, alive=16797, securePort=-1, UDP Port=-1, id={8 -18 6 26 122 -98 66 -25 -68 -39 107 -7 -125 108 7 119 }, payload={}, command={}, domain={}, ]. This operation will timeout if no session state has been received within 60 seconds.
2014-12-9 19:32:04 org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor report



在apache查看负载均衡信息:

wKioL1SIEDngbrhMAAU-E83Rod4948.jpg