Apache集成Tomcat做负载均衡和动静分离
背景描述:
1.使用apache集成Tomcat进行web服务部署
2.多台tomcat部署web服务
3.结合apache做负载均衡和动静分离
准备阶段:
1.两个相同配置的tomcat服务,都是ApacheTomcat 7.0.54版本
2.一个tomcat,端口2080的图片资源服务器
3.一个集成了Proxy(用于域名代理、负载均衡)的apache,运用的是xmapp集成软件
说明:apache提供了两种负载均衡的实现方式,一种是用jkMod,一种是proxy,但是第一种已经不更新了,本文采用的是proxy做的负载均衡和动静分离。
具体实现:
第一步、Tomcat配置
1.修改tomcat的server.xml
在conf/server.xml中,设置对应的配置
tomcat1:假设shutdown端口为:11005,tomcat连接端口为11080;修改的AJP/1.3端口为11009,修改<Engine>元素的jvmRoute="jvm1"属性,添加<Cluster>用于两个tomcat的session同步复制,其中相同tomcat服务的<Receiver>的端口为4005要一致
tomcat2:假设shutdown端口为:12005,tomcat连接端口为12080;修改的AJP/1.3端口为12009,修改<Engine>元素的jvmRoute="jvm2"属性,添加<Cluster>用于两个tomcat的session同步复制,其中相同tomcat服务的<Receiver>的端口为4005要一致
注:这里只黏贴<Engine>、<Cluster>两个元素,详细参看附件
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <!-- DeltaManager--> <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="4005" 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> <!-- .*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt; --> <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> |
2.修改tomcat的context.xml
在conf/context.xml文件中<Context>元素中加入<Loaderdelegate="true" />
3.修改web.xml
在conf/web.xml文件中,在<web-app>元素中加入:<distributable/>
第二步、Apache配置
1.修改httpd.conf
在xmapp/apache/conf/httpd.conf文件中,修改proxy的相关注释,includehttp-proxy.conf文件
注:这里只黏贴部分截图,详细参看附件
|
2.修改 httpd-vhost.conf
负载均衡相关配置,及多个服务的负载均衡 |
<VirtualHost *:80> ServerName wxcs.xmsa.info ErrorLog "logs/wxcs.xmsa.info-error.log" CustomLog "logs/wxcs.xmsa.info-access.log" common
ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On ProxyPassReverse / balancer://cluster/
</VirtualHost>
<VirtualHost *:80> ServerName ydymcs.xmsa.info ErrorLog "logs/ydymcs.xmsa.info-error.log" CustomLog "logs/ydymcs.xmsa.info-access.log" common
ProxyPass / balancer://ydymcluster/ stickysession=JSESSIONID|jsessionid nofailover=On ProxyPassReverse / balancer://ydymcluster/
</VirtualHost> |
负载均衡相关配置,及多个服务的负载均衡,动静分离: 将服务的静态代码复制一份到xmapp/htdoc文件下,并部署一个图片服务器(127.0.0.1:2080)用于实现图片、文件资源的统一管理,值得注意的是,资源服务器要和apache在同一台服务器上部署。 |
<VirtualHost *:80> ServerAdmin wxcs.xmsa.info DocumentRoot "D:/xampp/htdocs/shian-web/" ServerName wxcs.xmsa.info ServerAlias wxcs.xmsa.info ErrorLog "logs/wxcs.xmsa.info-error.log" CustomLog "logs/wxcs.xmsa.info-access.log" common
ProxyRequests Off ProxyPreserveHost On ProxyVia On #过滤gif ProxyPassMatch /*.gif$ ! #过滤jpg ProxyPassMatch /*.jpg$ ! #过滤png ProxyPassMatch /*.png$ ! #过滤css ProxyPassMatch /*.css$ ! #过滤js ProxyPassMatch /*.js$ ! #过滤htm ProxyPassMatch /*.htm$ ! #过滤html ProxyPassMatch /*.html$ ! ProxyPassMatch /*.woff$ ! ProxyPassMatch /*.woff2$ ! ProxyPassMatch /*.ttf$ ! ProxyPassMatch /*.txt$ ! ProxyPassMatch /*.json$ ! ProxyPassMatch /*.svg$ ! ProxyPassMatch /*.apk$ !
#过滤server-stauts监控页面 ProxyPass /server-status ! #过滤balancer-manager监控页面 ProxyPass /balancer-manager !
ProxyPass /*.jsp http://127.0.0.1:2080/ ProxyPassMatch ^(/.*.upload) http://127.0.0.1:2080/ ProxyPass /ueditor/*.* http://127.0.0.1:2080/ ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On ProxyPassReverse / balancer://cluster/
</VirtualHost>
<VirtualHost *:80> ServerAdmin ydymcs.xmsa.info DocumentRoot "D:/xampp/htdocs/ydym-web/" ServerName ydymcs.xmsa.info ServerAlias ydymcs.xmsa.info ErrorLog "logs/ydymcs.xmsa.info-error.log" CustomLog "logs/ydymcs.xmsa.info-access.log" common ProxyRequests Off ProxyPreserveHost On ProxyVia On #过滤gif ProxyPassMatch /*.gif$ ! #过滤jpg ProxyPassMatch /*.jpg$ ! #过滤png ProxyPassMatch /*.png$ ! #过滤css ProxyPassMatch /*.css$ ! #过滤js ProxyPassMatch /*.js$ ! #过滤htm ProxyPassMatch /*.htm$ ! #过滤html ProxyPassMatch /*.html$ ! ProxyPassMatch /*.woff$ ! ProxyPassMatch /*.woff2$ ! ProxyPassMatch /*.ttf$ ! ProxyPassMatch /*.txt$ ! ProxyPassMatch /*.json$ ! ProxyPassMatch /*.svg$ ! ProxyPassMatch /*.apk$ !
#过滤server-stauts监控页面 ProxyPass /server-status ! #过滤balancer-manager监控页面 ProxyPass /balancer-manager !
ProxyPass /*.jsp http://127.0.0.1: 2080/ #ProxyPass /*.upload http://127.0.0.1:2080/ ProxyPassMatch ^(/.*.upload) http://127.0.0.1:2080/ ProxyPass /ueditor/*.* http://127.0.0.1:2080/
ProxyPass / balancer://ydymcluster/ stickysession=JSESSIONID|jsessionid nofailover=On ProxyPassReverse / balancer://ydymcluster/
</VirtualHost> |
3.修改httpd-proxy.conf
#创建集群cluster #ProxyPass / balancer://cluster/ <Proxy balancer://cluster/> BalancerMember ajp://127.0.0.1:11009/ loadfactor=1 route=jvm1 BalancerMember ajp://192.168.0.107:12009/ loadfactor=1 route=jvm2 </Proxy>
#ProxyPass / balancer://ydymcluster/ <Proxy balancer://ydymcluster/> BalancerMember ajp://127.0.0.1:18009/ loadfactor=1 route=jvm4 </Proxy> #设置server-stauts监控页面 <Location /server-status> SetHandler server-status Order Deny,Allow Deny from all Allow from all </Location> #设置balancer-manager监控页面 <Location /balancer-manager> SetHandler balancer-manager Order Deny,Allow Deny from all Allow from all </Location> |
第三步、重启服务
到此就实现了负载均衡、动静分离的配置,重启Tomcat、Apache服务