瞭解:Apache+Tomcat實現負載均衡

反向代理負載均衡 (Apache2+Tomcat7/8) 
使用代理服務器能夠將請求轉發給內部的Web服務器,讓代理服務器將請求均勻地轉發給多臺內部Web服務器之一上,從而達到負載均衡的目的。這種代理方式與普通的代理方式有所不一樣,標準代理方式是客戶使用代理訪問多個外部Web服務器,而這種代理方式是多個客戶使用它訪問內部Web服務器,所以也被稱爲反向代理模式。 html

這次使用的代理爲mod_proxy的方式來實現的,由於在Apache2以上的版本中已經集成了,所以不須要再另行安裝和配置了,只須要把註釋去掉便可,此去掉的配置,我的感受與Apache虛擬機的實現相相似,去掉如下模塊的註釋:java

LoadModule proxy_module modules/mod_proxy.so   #提供代理服務器功能
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so  #提供負載均衡功能
LoadModule proxy_http_module modules/mod_proxy_http.so  #讓代理服務器能支持HTTP協議
apache

而後把:tomcat

#Include conf/extra/httpd-vhosts.conf的註釋也去掉,配置的負載均衡服務器

具體配置以下:網絡

#虛擬機配置,負載均衡配置  注意空格
<VirtualHost *:9999>
    ServerAdmin binbin@locahost
    ServerName localhost
    ServerAlias localhost
    ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On
    ProxyPassReverse / balancer://cluster/
    #ErrorLog "logs/error.log"
    #CustomLog "logs/access.log" common
    </VirtualHost> #The ProxyRequests directive should usually be set off when using ProxyPass.
    
    ProxyRequests Off
    <proxy balancer://cluster>
    BalancerMember ajp://localhost:8009 loadfactor=1 route=tomcat8_local  smax=5 max=20 ttl=120 retry=300 timeout=15
    BalancerMember ajp://192.168.1.250:8009 loadfactor=1 route=tomcat8_250  smax=5 max=20 ttl=120 retry=300 timeout=15
    ProxySet lbmethod=byrequests
    </proxy>

其中:  session

localhost:8009 和  192.168.1.250:8009爲兩個負載均衡服務器負載均衡

loadfactor=1 route=tomcat8_local  smax=5 max=20 ttl=120 retry=300 timeout=15 這個爲配置的參數,最大連接,超時,等等jvm

route=tomcat8_local  能夠不寫jsp

ProxySet lbmethod=byrequests 爲實現負載均衡的方式,共有三種類型

#lbmethod=byrequests 按照請求次數均衡(默認) 
#lbmethod=bytraffic 按照流量均衡 
#lbmethod=bybusyness 按照繁忙程度均衡(老是分配給活躍請求數最少的服務器)

route=tomcat8_local  根據這個route的值,分別在兩個Tomat中的Service.xml的   Engine 節點配置上jvmRoute的內容,

以下: 

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat8_local">和以及jvmRoute="tomcat8_250" ,不過我在測試是,感受若是不配置,也不會影響程序的執行。

啓動以上程序就能夠進行測試了,測試頁面來源於網絡:

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster Test</title></head>
<body>
<%  //HttpSession session = request.getSession(true);
  System.out.println(session.getId());  out.println("<br> SESSION ID:" + session.getId()+"<br>");  
  // 若是有新的請求,則添加session屬性
  String name = request.getParameter("name");  if (name != null && name.length() > 0) {
     String value = request.getParameter("value");
     session.setAttribute(name, value);
  }  
    out.print("<b>Session List:</b>");  
    Enumeration<String> names = session.getAttributeNames();    while (names.hasMoreElements()) {
        String sname = names.nextElement(); 
        String value = session.getAttribute(sname).toString();        out.println( sname + " = " + value+"<br>");
        System.out.println( sname + " = " + value);
   }%>
  <form action="testCluster.jsp" method="post">
    名稱:<input type=text size=20 name="name">
     <br>
    值:<input type=text size=20 name="value">
     <br>
    <input type=submit value="提交">
   </form>

   <b>負載均衡測試:此爲:Tomcat7_a上的文件,<font color=red>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</font><b>
</body>
</html>


此時訪問就會再個不一樣的頁面了,一個爲aaaaaaaaaaaaaaaaaaaaaaaaaaaa 一個爲bbbbbbbbbbbbbbbbbbbbbbbbbbb;

以下圖所示:

看到的兩個Session的值不一樣,由於這裏尚未session的共享配置

注意:

以上僅僅實現了負載均衡,可是對於兩個負載同時宕機的話,就須要另外的一臺服務器來代替了,這第n+1臺服務器就叫熱備服務器,配置方式與以上相同,僅須要寫上status=+H 標識便可。

BalancerMember http://192.168.1.218:8009 status=+H

以上負載均衡就算所有完成了,若是要實現Session共享,最簡單的方式就是在Tomcat中進行配置,配置以下:

在 service.xml文件中的 Engine節點,添加以下代碼:

<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="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      port="4001"
                      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"/>
  </Cluster>
相關文章
相關標籤/搜索