Apache2 + Tomcat6配置负载均衡

 

1. 安装Apache和Tomcat,这里假定Apache2.2.3,Tomcat6.x;且Apache安装在apachehost,tomcat分别安装在tomcathost1和tomcathost2上;
2. 修改/etc/httpd/conf/httpd.conf文件,确定以下行没有被注释掉
<textarea cols="50" rows="15" name="code" class="python">LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so</textarea> 
3. 修改/etc/httpd/conf/httpd.conf文件,添加以下行:
<textarea cols="50" rows="15" name="code" class="python"> ProxyRequests off &lt;Proxy balancer://tccluster&gt; BalancerMember http://tomcathost1:8080 loadfactor=2 route=tomcat1 max=150 smax=145 BalancerMember http://tomcathost2:8080 loadfactor=1 route=tomcat2 max=150 smax=145 Order Deny,Allow Allow from all #ProxySet lbmethod=byrequests|bytraffic|bybusyness &lt;/Proxy&gt; &lt;Location /balancer-manager&gt; SetHandler balancer-manager Order Deny,Allow Allow from all &lt;/Location&gt; &lt;Location /examples&gt; ProxyPass balancer://tccluster/examples stickysession=JSESSIONID ProxyPassReverse balancer://tccluster/examples Order Deny,Allow Allow from all &lt;/Location&gt;</textarea>  
* 其中lbmethod配置说明:
lbmethod=byrequests 按请求次数均衡(默认)
lbmethod=bytraffic 按流量均衡
lbmethod=bybusyness 按繁忙程度均衡(总是分配给活跃请求数最少的服务器)
* 其中BalancerMember配置中route=tomcat<ID>是下面在tomcat的server.xml配置中的jvmRoute值,这样配置如果两个tomcat都是可用的情况下,同一个浏览器过来的请求会被转发到同一个tomcat上。
4. 确定apachehost和两台tomcathost可以互访,并且两个tomcat机器在一个网段内;
5. 确定两台tomcat host多播已经打开,在Linux机器上可以使用cat /proc/net/dev_mcast检查,如果文件存在基本上就是打开了;另外确定两台tomcat机器的/etc/hosts文件中包含“xxx.xxx.xxx.xxx    hostname”比如“172.17.1.101    tomcathost1”或“172.17.1.102    tomcathost2”;
6. 修改两个tomcat的conf/server.xml文件,对Engine节点分别添加jvmRoute="tomcat1"和jvmRoute="tomcat2",并添加以下内容:
<textarea cols="50" rows="15" name="code" class="xhtml">&lt;Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"&gt; &lt;Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/&gt; &lt;!-- &lt;Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/&gt; --&gt; &lt;Channel className="org.apache.catalina.tribes.group.GroupChannel"&gt; &lt;Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.5" port="45564" frequency="500" dropTime="3000"/&gt; &lt;Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" selectorTimeout="100" maxThreads="6"/&gt; &lt;Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"&gt; &lt;Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/&gt; &lt;/Sender&gt; &lt;Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/&gt; &lt;Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/&gt; &lt;/Channel&gt; &lt;Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.txt;"/&gt; &lt;ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/&gt; &lt;ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/&gt; &lt;/Cluster&gt;</textarea>  
7. 创建一个web app,这里为了简单使用tomcat自带的examples web app(webapps/examples),修改其中的WEB-INF/web.xml文件,在其中<display-name>Servlet and JSP Examples</display-name>节点后添加<distributable/>表明此应用与集群服务器复制Session;
8. 在两个Tomcat的webapps/examples目录下各创建一个test.jsp文件,用来测试Cluster中两个Tomcat的Session复制,代码如下:
<textarea cols="50" rows="15" name="code" class="xhtml">&lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;% String mydata = request.getParameter("mydata"); if (mydata != null &amp;&amp; mydata.length() != 0) { session.setAttribute("mydata", mydata); } out.println("request.getLocalAddr(): " + request.getLocalAddr()); out.println("&lt;br/&gt;"); out.println("request.getLocalPort(): " + request.getLocalPort()); out.println("&lt;br/&gt;"); out.println("Session ID: " + session.getId()); out.println("&lt;br/&gt;"); out.println("mydata: " + session.getAttribute("mydata")); %&gt; &lt;form&gt; &lt;input type=text size=20 name="mydata"&gt; &lt;br&gt; &lt;input type=submit&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</textarea>  
9. 启动Tomcat和Apache,访问http://apachehost/examples/,可以修改其中Session中的值,并且可以看到两个tomcat的Session中的值是一样的;
10. 访问http://apachehost/balancer-manager,可以使用此负载均衡管理页面来动态enable/disable其中的某个tomcat。

 

参考资料:

http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

 

你可能感兴趣的:(apache,tomcat,session,负载均衡,Module,Interceptor)