天天看點

Apache HttpServer與Tomcat搭建叢集全記錄

1.軟體準備:    

    Tomcat版本:apache-tomcat-6.0.35.tar.gz

    Apache Http-Server 版本:httpd-2.2.22.tar.gz

    Tomcat-Connectors:tomcat-connectors-1.2.32-src.tar.gz

    JDK版本:jdk1.6.0_29

    前三個可以到apache 官網上下載下傳,JDK到oracle 官網上下載下傳。

2.伺服器環境

   Red Hat Enterprise Linux Server release 5.4

3.JDK的安裝

   JDK安裝(略),請詳見文章  Linux下的JDK安裝與解除安裝

4.Tomcat的安裝

    解壓  tar -zxvf apache-tomcat-6.0.35.tar.gz

5.http server 安裝

     1)解壓 tar -zxvf httpd-2.2.22.tar.gz

    2)cd httpd-2.2.22

    3)進行編譯參數配置

          ]# ./configure --prefix=/usr/local/apache2 --enable-mods-shared=all

         關于configure參數的更多内容可參考:

         http://httpd.apache.org/docs/2.2/en/programs/configure.html#installationdirectories

    4)編譯安裝

         ]#make

         ]#make install

    請注意:在進行第4步安裝時,報Cannot use an external APR with the bundled APR-util錯誤的解決辦法

    4.1)進入httpd-2.2.22目錄下的srclib 進行apr的安裝

            ]#cd srclib/apr

            ]#./configure --prefix=/usr/local/apr

            ]#make

            ]#make install

    4.2)進入httpd-2.2.22目錄下的srclib 進行apr-util的安裝

            ]#cd srclib/apr-util

            ]#./configure --prefix=/usr/local/apr-util  --with-apr=/usr/local/apr

            ]#make

            ]#make install

     4.3)傳回到httpd-2.2.22目錄下 進行httpserver的安裝

            ]#cd ..

            ]#./configure --prefix=/usr/local/apache2 --enable-mods-shared=all \

                 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/bin

     5) 啟動和關閉apache

            完成上述步驟後,apache的安裝就結束了,通過下面的指令啟動和關閉apache:

            啟動:/usr/local/apache2/bin/apachectl start

            關閉:/usr/local/apache2/bin/apachectl stop

6.Apache Tomcat Connector(生産mod_jk)的安裝

      1)解壓tomcat-connectors-1.2.32-src.tar.gz

           ]#tar -zxvf tomcat-connectors-1.2.32-src.tar.gz

      2)進入解壓出的檔案夾下的native子目錄

           ]#cd tomcat-connectors-1.2.32-src/native

      3)進行編譯參數的配置

           ]#./buildconf.sh

           ]#./configure --with-apxs=/usr/local/apache2/bin/apxs \

                --with-java-home=$JAVA_HOME --with-java-platform=2 --enable-jni

           ]#make

           ]#make install

       如果成功結束,你可以在/usr/local/apache2/modules/下找到mod_jk.so檔案。

7.修改Tomcat配置

       7.1)修改端口

               對于tomcat配置主要集中在conf/server.xml檔案上。首先,鑒于是在同一機器上運作兩個tomcat執行個體,

               是以,要對server.xml中配置的所有端口進行修改,避免端口被占用,一種簡單而穩妥的修改方法是将該

               檔案中出現的所有端口号按一種簡單的規則統一進行改動,比如在原端口号基礎上統一加1000或減1000。

               基于這個原則,apache-tomcat-6.0.33-1的連接配接端口修改為:6080,apache-tomcat-6.0.33-2的

               連接配接端口修改為:7080,其他端口配置同樣依照該原則。

       7.2)2.開啟tomcat叢集支援,同樣是在conf/server.xml檔案裡,進行兩處改動

              改動1:為<Engine>設定jvmRoute

              說明:實際上,在server.xml裡就有一段現成的帶jvmRoute的<Engine>配置,我們可以參考這段配置

              為<Engine/>加個jvmRoute就可以了,對于jvmRoute的取值要特别注意:其值必須于後面要提到的

              mod_jk的workers.properties檔案中結點名相一緻!由于那個檔案中結點名為tomcat1和tomcat2,

              是以,此處我們必須設定jvmRoute的值為這兩個值之一。關于這一點在tomcat官方關于Apache

              Tomcat Connector的文檔 :

              http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html 也有明确提及。

              jvmRoute值會出現在由該結點建立的session id中,例如:在非叢集環境下,一個sessionid可能是

              “xxxxxxxxx” 的格式,而在叢集環境下,如果目前結點的jvmRtomat1oute被配置為tomcat1,那由

              該結點生成的sessionid将變成“xxxxxxxxx.tomat1”格式,而mod_jk正是依賴于這個節點字尾實作

              sticky session的,也就是把所有字尾是tomat1的請求都發送給tomat1結點進行處理。

Apache HttpServer與Tomcat搭建叢集全記錄

               改動2:在<Engine/>中添加關于叢集的配置

               在tomcat官方關于tomcat叢集配置的文檔:

                    http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html 中

               有一份預設配置:

                <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="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>

8.配置apache

   打開apache安裝目錄下的conf/httpd.conf檔案,在檔案最後追加以下内容:

    # Load mod_jk module

    LoadModule jk_module modules/mod_jk.so

    #Specify jk log file.

    JkLogFile /var/log/mod_jk.log

    # Specify jk log level [debug/error/info]

    JkLogLevel info

    # Specify workers.properties, this file tell jk:

    # how many nodes and where they are.

    JkWorkersFile conf/workers.properties

    # Specify which requests should handled by which node.

    JkMount /* controller 

    # controller--workers.properties 裡指定的負載均衡器

    上述配置中:JkWorkersFile conf/workers.properties 指明由一個workers.properties檔案來描述

    叢集結點的情況,是以,我們需要建立這個workers.properties檔案,并放置于conf檔案夾下,這個文

    件的内容如下:

    #所有節點清單,其中controller是一個邏輯結點,負責負載均衡控制,

    #如果JkMount中的URL指定給了controller就表示這個請求會被自動散列到某個實體節點上。

    #注意:真正負責處理請求的tomcat的名稱(這裡就是tomcat1,tomcat2)必須于它們在conf/server.xml

    #檔案中配置的jvmRout的屬性值是一緻的!

    worker.list = controller,tomcat1,tomcat2

    #========tomcat1========

    worker.tomcat1.port=6009       #ajp13 端口号,在tomcat下server.xml配置,預設8009

    worker.tomcat1.host=localhost  #tomcat的主機位址,如不為本機,請填寫ip位址

    worker.tomcat1.type=ajp13

    worker.tomcat1.lbfactor = 1    #server的權重比重,值越高,分得的請求越多

   #========tomcat2========

   worker.tomcat2.port=7009       #ajp13 端口号,在tomcat下server.xml配置,預設8009

   worker.tomcat2.host=localhost  #tomcat的主機位址,如不為本機,請填寫ip位址

   worker.tomcat2.type=ajp13

   worker.tomcat2.lbfactor = 1    #server的權重比重,值越高,分得的請求越多

   #========controller,負載均衡控制器========

   worker.controller.type=lb

   worker.controller.balance_workers=tomcat1,tomcat2 

             #指定分擔請求的tomcat,舊版本中的   balanced_workers,已不再推薦使用!

   worker.controller.sticky_session=1 #sticky_session為1表示,

   #當某一 client的session建立之後,後續由該用戶端發起的請求,也就是這個session的所有

     請求都始終由第一次處理該請求的結點

   #負責處理(除非該結點挂掉)

     到此,所有配置均已完成,啟動兩個tomcat和apache後,将某一應用同時部署到兩個tomcat中,

    通過apache通路這個應用,觀察tomcat背景打出的日志會發現,請求被随機配置設定給了兩個tomcat交替執行

繼續閱讀