terracotta+tomcat+apache集羣配置

Terracotta3.3.7+ tomcat6.0 + apache2.2.2集羣配置


一.目的

    Tomcat集羣配置,實現集羣服務器中session共享

二.準備材料:

    1. terracotta-3.7.7-2013-08-19_16-03-48-installer.jar

     2. httpd-2.2.25-win32-x86-openssl-0.9.8y.msi

     3. apache-tomcat-6.0.45-windows-x64.zip

     4. tomcat-connectors-1.2.40-windows-i386-httpd-2.2.x.zip

5.jdk環境必須爲1.6,terracotta3.7.7只能支持1.6(後來測試中改爲了1.7 發現也能使用)

 

三.配置兩個tomcat6.0

    新建兩個文件夾tom1,tom2, 將apache-tomcat-6.0.45-windows-x64.zip解壓後,分別放置到兩個文件夾下,如圖所示:  

       

因爲需要同時啓動兩個tomcat,所以必須要修改兩個端口,配置文件如下:

 

Tom1中的server.xml

<?xmlversion='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation(ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional informationregarding copyright ownership.
  The ASF licenses this file to You under theApache License, Version 2.0
  (the "License"); you may not usethis file except in compliance with
  the License. You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreedto in writing, software
  distributed under the License is distributedon an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
  See the License for the specific languagegoverning permissions and
  limitations under the License.
-->
<!--Note:  A "Server" is not itselfa "Container", so you may not
     define subcomponents such as"Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Serverport="8006" shutdown="SHUTDOWN">
  <!--APR library loader. Documentation at/docs/apr.html -->
  <ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on" />
  <!--Initialize Jasper prior to webapps areloaded. Documentation at /docs/jasper-howto.html -->
  <ListenerclassName="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use ofparticular java/javax APIs-->
  <ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  <!--JMX Support for the Tomcat server. Documentation at /docs/non-existent.html-->
  <ListenerclassName="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <GlobalNamingResources>
    <Resource name="UserDatabase"auth="Container"
             type="org.apache.catalina.UserDatabase"
              description="User databasethat can be updated and saved"
             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
             pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="8081"protocol="HTTP/1.1"
              connectionTimeout="20000"
               redirectPort="8445"/>
    <Connector port="8010"protocol="AJP/1.3" redirectPort="8445" />
<Enginename="Catalina" defaultHost="localhost"jvmRoute="tomcat1">
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
   <!-- This Realm uses the UserDatabaseconfigured in the global JNDI
           resources under the key"UserDatabase".  Any edits
           that are performed against thisUserDatabase are immediately
           available for use by the Realm.  -->
      <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
            resourceName="UserDatabase"/>
<Hostname="localhost" appBase="webapps"
            unpackWARs="true"autoDeploy="true"
            xmlValidation="false"xmlNamespaceAware="false">
      </Host>
    </Engine>
  </Service>
</Server>


 

Tom2 中的server.xml

 

<?xml version='1.0' encoding='utf-8'?>
<Server port="8007" shutdown="SHUTDOWN">
  <!--APR library loader.Documentation at /docs/apr.html -->
  <ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on" />
  <!--Initialize Jasperprior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <ListenerclassName="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memoryleaks due to use of particular java/javax APIs-->
  <ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  <!-- JMX Support forthe Tomcat server. Documentation at /docs/non-existent.html -->
  <ListenerclassName="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
 <GlobalNamingResources>
    <Resourcename="UserDatabase" auth="Container"
             type="org.apache.catalina.UserDatabase"
             description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
             pathname="conf/tomcat-users.xml" />
 </GlobalNamingResources> 
  <Servicename="Catalina">
    <Connectorport="8083" protocol="HTTP/1.1"
              connectionTimeout="20000"
              redirectPort="8446" />
    <Connectorport="8011" protocol="AJP/1.3"redirectPort="8446" />
    <Enginename="Catalina" defaultHost="localhost"jvmRoute="tomcat2">
          <ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
   <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
            resourceName="UserDatabase"/>
      <Hostname="localhost" appBase="webapps"
           unpackWARs="true" autoDeploy="true"
           xmlValidation="false" xmlNamespaceAware="false">
      </Host>
    </Engine>
  </Service>
</Server>



同時將兩個tomcat都加入windows服務中,

對於tom1,修改bin目錄下的tomcat6.exe爲tomcat6_1.exe,tomcat6w.exe爲tomcat6w_1.exe,用記事本打開service.bat,將裏面的tomcat6全部改爲tomcat6_1,同時修改set SERVICE_NAME=Tomcat6爲set SERVICE_NAME=Tomcat6_1,

同理對於tom2修改爲tomcat_2,

通過cmd界面,cd 進入tom1的bin目錄,執行service install命令安裝服務



同理,安裝好tom2.

 

 

四.配置apache

 雙擊httpd-2.2.25-win32-x86-openssl-0.9.8y.msi進行安裝,選擇文件夾爲D:\soft\apache (按自己喜歡的位置來),安裝成功後,開始進行配置。

解壓tomcat-connectors-1.2.40-windows-i386-httpd-2.2.x.zip,得到mod_jk.so,將其放入至apache安裝目錄的modules下面,然後打開conf文件夾下httpd.conf配置文件,向其中添加一條語句 Includeconf/mod_jk.conf

接着在conf裏面新建mod_jk.conf,填寫一些內容如下:

 

LoadModulejk_module "D:/soft/apache/modules/mod_jk.so"

JkWorkersFile"conf/workers.properties"

JkMount/*.jsp controller

JkMount/*.do controller

 

以上就配置好了apache的mod_jk模塊(負載均衡),將*.jsp和*.do轉發給tomcat

 

接着在conf裏面新建一個workers.properties,內容如下:

#server 

worker.list =controller 

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

worker.tomcat1.port=8010

worker.tomcat1.host=localhost 

worker.tomcat1.type=ajp13 

worker.tomcat1.lbfactor= 1 

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

worker.tomcat2.port=8011

worker.tomcat2.host=localhost 

worker.tomcat2.type=ajp13 

worker.tomcat2.lbfactor= 1 

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

worker.controller.type=lb 

worker.controller.balanced_workers=tomcat1,tomcat2 

worker.controller.sticky_session=false 

worker.controller.sticky_session_force=1 

#worker.controller.sticky_session=1

 

至此apache配置結束。

 

(其實到此就可以實現session共享了,只不過是tomcat自帶的粘性session,一當session過多後就會導致性能下降,可以寫個小web項目sharesession測試一下,修改web.xml文件,在<web-app></web-app>之間加入</description>,這是配置tomcat進行粘性session的標籤,最後新建index.jsp頁面,內容如下:)

<%@ pagelanguage="java" import="java.util.*"pageEncoding="UTF-8"%> 

<%@ pageimport="java.util.*" %> 

<html><head><title>ClusterApp Test</title></head> 

<body> 

Server Info: 

<% 

out.println(request.getLocalAddr()+ " : " + request.getLocalPort()+"<br>");%> 

<% 

  out.println("<br> ID " +session.getId()+"<br>"); 

  // 如果有新的 Session 屬性設置 

  String dataName =request.getParameter("dataName"); 

  if (dataName != null &&dataName.length() > 0) { 

     String dataValue =request.getParameter("dataValue"); 

     session.setAttribute(dataName, dataValue);

  } 

  out.println("<b>Session 列表</b><br>");

  System.out.println("********************"); 

  Enumeration e =session.getAttributeNames(); 

  while (e.hasMoreElements()) { 

     String name =(String)e.nextElement(); 

     String value =session.getAttribute(name).toString(); 

     out.println( name + " = " +value+"<br>"); 

         System.out.println( name + " =" + value); 

   } 

%> 

  <form action="index.jsp"method="POST"> 

    名稱:<input type=text size=20name="dataName"> 

     <br> 

    值:<input type=text size=20name="dataValue"> 

     <br> 

    <input type=submit> 

   </form> 

</body> 

</html>

 

打開瀏覽器後,輸入地址127.0.0.1:80/sharesession/index.jsp,即可訪問,多打開幾個頁面會發現,session並不會改變,改變的只是不同tomcat對請求進行處理。

 

四.配置terracotta

雙擊terracotta-3.7.7-2013-08-19_16-03-48-installer.jar安裝至D:\terracotta目錄下,在該目錄下添加tc-config.xml文件,內容如下:

 

<?xmlversion="1.0" encoding="UTF-8" ?> 
<tc:tc-configxmlns:tc="http://www.terracotta.org/config" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-6.xsd"> 
  <tc-properties> 
    <propertyname="l2.nha.dirtydb.autoDelete" value="true"/> 
    <propertyname="l1.cachemanager.enabled" value="true"/> 
    <propertyname="logging.maxLogFileSize" value="1024"/> 
  </tc-properties> 
  <system> 
   <configuration-model>development</configuration-model> 
  </system> 
  <servers> 
    <server host="127.0.0.1"name="localhost" bind="0.0.0.0"> 
     <statistics>/opt/terracotta/server-statistics</statistics>  
      <dso-portbind="127.0.0.1">9510</dso-port> 
      <jmx-portbind="127.0.0.1">9520</jmx-port> 
      <l2-group-portbind="127.0.0.1">9530</l2-group-port> 
      <authentication/>
      <dso> 
       <client-reconnect-window>120</client-reconnect-window> 
        <persistence>     
         <mode>temporary-swap-only</mode> 
        </persistence> 
                     <garbage-collection> 
         <enabled>true</enabled> 
          <verbose>false</verbose> 
         <interval>3600</interval> 
        </garbage-collection> 
      </dso> 
    </server> 
    <mirror-groups>  
      <mirror-groupgroup-name="group1"> 
        <members> 
         <member>localhost</member> 
        </members>         
        <ha> 
         <mode>networked-active-passive</mode> 
          <networked-active-passive> 
           <election-time>5</election-time> 
         </networked-active-passive> 
        </ha> 
       </mirror-group> 
    </mirror-groups>      
    <ha> 
     <mode>networked-active-passive</mode> 
      <networked-active-passive> 
       <election-time>5</election-time> 
      </networked-active-passive> 
    </ha> 
    <update-check> 
      <enabled>true</enabled> 
     <period-days>10</period-days> 
    </update-check> 
  </servers> 
  <clients> 
    <logs>logs-%i</logs> 
  </clients> 
</tc:tc-config>


 

將D:\terracotta\sessions目錄下的terracotta-session-XXXXXXX.jar以及

D:\terracotta\common目錄下的terracotta-toolkit-1.6-runtime-XXXXX.jar

複製到tomcat的lib目錄下,然後在剛剛sharesession測試項目的META-INF文件夾下添加context.xml文件,內容如下:

<?xml version="1.0"encoding="UTF-8"?>

<Context>

    <Valve className="org.terracotta.session.TerracottaTomcat60xSessionValve"tcConfigUrl="127.0.0.1:9510" />

</Context>

配置到此結束了!

 

開始啓動terracotta,cmd中進入terracotta安裝目錄,然後執行 start-tc-server.bat批處理文件,如下圖:

 

啓動成功!

 

接着打開兩個tomcat6.0

 

 


均啓動成功!

 

最後打開apache,

 

 

啓動成功!

 

打開瀏覽器輸入

 

 

很明顯 session實現了共享, session值是一樣的,不一樣的只是請求處理的服務器。同時可以使用terracotta的Terracotta Developer Console進行查看

 

 

結果:測試中發現 Terracotta3.3.7 存儲的對象同樣也需要序列化,用了兩個tomcat,通過apache協調調度tomcat,有效的均衡了瀏覽器url請求,並且在後來的使用中將jdk改爲了1.7,並將tomcat6.0改爲了tomcat8.0,運行正常。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章