六.基于mod_jk实现负载均衡
6.1 注释刚才添加的基于proxy_mod的配置,启用已添加的mod_jk配置
- #ProxyRequests Off
- #<Proxy balancer://lbcluster>
- # BalancerMember ajp://172.16.1.21:8009 loadfactor=10route=TomcatA
- # BalancerMember ajp://172.16.1.22:8009 loadfactor=10route=TomcatB
- #</Proxy>
- #ProxyPass / balancer://lbcluster/
- #ProxyPassReverse / balancer://lbcluster/
- Include /etc/httpd/extra/httpd-jk.conf
6.2 修改 /etc/httpd/extra/httpd-jk.conf
- vi /etc/httpd/extra/httpd-jk.conf
- # Load the mod_jk
- LoadModule jk_module modules/mod_jk.so
- JkWorkersFile /etc/httpd/extra/workers.properties
- JkLogFile logs/mod_jk.log
- #JkLogLevel debug
- JkMount /* cluster1
- JkMount /status/ status
6.3 修改/etc/httpd/workers.properties
- worker.list = cluster1,status
- worker.TomcatA.type = ajp13
- worker.TomcatA.host = 172.16.1.21
- worker.TomcatA.port = 8009
- worker.TomcatA.lbfactor = 5
- worker.TomcatB.type = ajp13
- worker.TomcatB.host = 172.16.1.22
- worker.TomcatB.port = 8009
- worker.TomcatB.lbfactor = 5
- worker.cluster1.type = lb
- worker.cluster1.sticky_session = 0 ##如果为1会绑定session
- worker.cluster1.balance_workers = TomcatA, TomcatB
- worker.status.type = status
6.4 测试访问 http://balance.linuxidc.com/
七.构建基于内存session复制的tomcat集群
为了防止由于节点故障导致的session丢失,我们来构建基于内存session复制的集群,在刚才的的负载均衡中你或许已经发现网页显示的session id一直是变动的,这个session id 就是通过jsp获得的此次会话的id,我们发现会话id一直是变动的,TomcatA中一直是TomcatA的session id,下面我们来构建吧。
7.1 修改172.16.1.21的server.xml,在engine下添加如下
- vi /usr/local/tomcat/conf/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="227.50.10.1"bind="172.16.1.21"port="45564"
- frequency="500"dropTime="3000"/>
- <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="172.16.1.21"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=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>
- <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>
7.2 修改balance虚拟主机的部署文件web.xml
我们建立的balance时没有建立web.xml,我们继承的是/usr/local/tomcat/conf/web.xml的,我们现在需要修改部署文件,所以拷贝到/web/balance/WEB-INF中修改,在<web-app> </web-app>中添加 <distributable/>,部份如下
- cp /usr/local/tomcat/conf/web.xml /web/apps/WEB-INF/
- vi /web/apps/WEB-INF/web.xml
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <distributable/>
- </web-app>
7.3 修改172.16.1.22 的server.xml 基本与172.16.1.21的相同,只修改多播ip与接收ip即可,为了不浪费51blog的存储空间,就不完全显示了
- <MembershipclassName="org.apache.catalina.tribes.membership.McastService"
- address="227.50.10.1"bind="172.16.1.22"port="45564"
- frequency="500"dropTime="3000"/>
- <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="172.16.1.22"port="4000"autoBind="100"
- selectorTimeout="5000"maxThreads="6"/>
7.4 对balance虚拟主机部署文件修改与172.16.1.21相同
7.5 测试访问 http://balance.linuxidc.com/ 查看session id变化情况
到此构建结束,过程并不难,但要熟练使用tomcat必须得去了解Java啊,还有好长的路要走,不要以为会构建了tomcat就觉的会tomcat了,其实还不到10%,所以加油,写给你们,也写给自己。
