天天看點

[伺服器]基于mod_proxy+Apache 2.2.16+Tomcat 7的負載均衡與叢集配置(一)

第一章. 背景簡介

對于大多數企業應用,都希望能做到7*24小時不間斷運作。要保持如此高的可用性并非易事,比較常見的做法是将系統部署到多台機器上,每台機器都對外提供同樣的功能,這就是叢集。系統變為叢集時,除了要求系統能夠支援水準伸縮外,還要解決兩個問題:

1, 如何均衡地通路到提供業務功能的機器。

2, 如何保證當機器出現問題時,使用者能自動跳轉到另外的機器,不影響使用。

常用的負載均衡技術有硬體和軟體兩種,本示例常用軟體的技術實作。軟體也有很多實作技術,如基于apache的mod_jk以及mod_proxy等。基于mod_jk的文章有不少,本文示範一下用mod_proxy的方式。

實作叢集的應用最重要的是處理使用者Session的問題,一般有三種政策:

1, Session複制

2, Session Sticky

3, 基于Cache的集中式Session

本文使用的是Tomcat 7.0.2應用伺服器,用的方法是Session複制。

第二章. 配置環境

1, JDK1.6,請自行下載下傳安裝,搞Java的一般都裝有的吧,哈哈。

2, Apache 2.2.16, (released 2010-07-25),現在為止應該是最新的穩定版本,下載下傳位址: http://httpd.apache.org/download.cgi

3, Tomcat 7.0.2,目前也是最新的版本。Minimum Java Version1.6.下載下傳位址:http://tomcat.apache.org/download-70.cgi

4, 安裝過程略

第三章. 部署圖(略)

第四章. Tomcat7叢集配置

一、 就地取材,複制tomcat7/webapps下的examples,重命名為cluster應用,以後就用cluster做測試。

二、 詳細配置參照tomcat7 \webapps\docs\cluster-howto.html 或者http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html

三、 為了在Tomcat7中實作session複制,以下必需完成:

• 所有session屬性必需實作 java.io.Serializable

• Uncomment the Cluster element in server.xml。把Cluster元素的注釋去掉。參照四

• If you have defined custom cluster valves, make sure you have the ReplicationValve defined as well under the Cluster element in server.xml 。參照四

• If your Tomcat instances are running on the same machine, make sure the tcpListenPort attribute is unique for each instance, in most cases Tomcat is smart enough to resolve this on it's own by autodetecting available ports in the range 4000-4100。參照四< Receiver>中的注釋

• Make sure your web.xml has the <distributable/> element or set at your <Context distributable="true" /> 參照四

• If you are using mod_jk, make sure that jvmRoute attribute is set at your Engine <Engine name="Catalina" jvmRoute="node01" > and that the jvmRoute attribute value matches your worker name in workers.properties .用mod_jk的情況,我們可以不管。

• Make sure that all nodes have the same time and sync with NTP service! 當使用多台機器時,要保證不同機器時間的同步。原因為tomcat session複制的一些機制。具體原因看文檔。

• Make sure that your loadbalancer is configured for sticky session mode. 保證負載均衡軟體設定為session sticky模式。

四、 詳細配置:

1. 修改tomcat7_a/conf/server.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"
                      <!—tcpListenPort如果是同一機器部署兩個tomcat7應用,則修改tomcat7_b為4001,以免沖突. 不同機器下,不用更改此項。—>
                      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>
           

2. <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 修改,僅為了調試友善。

3. tomcat7_a\webapps\cluster\WEB-INF\web.xml中加入<distributable/>

4. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_a">,添加jvmRoute屬性,此項為後面apache負載均衡用到。

五、 複制一份tomcat7_a應用,改名為tomcat7_b.隻是為了圖友善,實際應該複制的是前面的cluster工程。注意以下幾點配置就ok.

1. 修改tomcat7_a/conf/server.xml 中的Server port屬性<Server port="8006" shutdown="SHUTDOWN">,因為是同一台機器兩個tomcat應用,是以改一下。

2. 修改<connector port="8082" protocol="HTTP/1.1" onnectionTimeout="20000" edirectPort="8443" />,同理,為了避免同一台機器端口号沖突。部置在不同的機器是不用管的。

3. 修改<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_b">,此項為後面apache負載均衡用到。

4. 修改<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto"port="4001" autoBind="100" selectorTimeout="5000" maxThreads="6"/>

5. tomcat7_b\ebapps\cluster\WEB-INF\web.xml中加入<distributable/>

到此叢集配置完成.