歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> tomcat 單機多實例

tomcat 單機多實例

日期:2017/3/1 11:45:15   编辑:關於Linux

環境

tomcat:/home/tomcat-6.0.45

oms-client
站點程序:/home/oms-client/oms-book-client

站點tomcat實例:/home/oms-client/tomcat6-sever1

端口:38080

bms-client

站點程序:/home/bms-client/bms-book-client

站點tomcat實例:/home/bms-client/tomcat6-sever2

端口:48080

1. tomcat安裝

安裝很簡單,配置好jdk與解壓tomcat即可

我們首先看到的是tomcat的目錄結構,其每個文件夾有以下用途:

  • bin – 包含所有運行tomcat的二進制和腳本文件
  • lib – 包含tomcat使用的所有共享庫
  • conf - 包含配置信息,如tomcat綁定的端口等
  • logs – 包含所有的日志文件
  • temp – 此目錄是tomcat存放的臨時文件
  • webapps – 此目錄非常重要,這裡存放所有的應用程序war包
  • work – 如果應用程序包含jsp/" target="_blank">jsp文件,那麼每個jsp文件會被編譯轉化為servlet,存放於此

    當我們運行Tomcat時,會用到5個環境變量。他們是:

    • CATALINA_HOME
    • CATALINA_BASE
    • CATALINA_TMPDIR
    • JRE_HOME/JAVA_HOME
    • CLASSPATH

      在以上列表中,CATALINA_HOME和JAVA_HOME是必要的環境變量。其它的都可以通過CATALINA_HOME來轉換,是可選的。

      CATALINA_HOME – 此環境變量是tomcat安裝/提取的根目錄。所以通過CATALINA_HOME,可以得到bin和lib目錄。

      CATALINA_BASE – 如果不指定則是CATALINA_HOME的值。該變量指向的目錄裡面包括每個運行實例需要使用自己的conf、logs、temp、webapps、work目錄。

      一般運行Tomcat的方法是,只設置CATALINA_HOME變量,執行startup.sh腳本,startup.sh會自動轉換其它未設置的變量。

      2. 配置多實例目錄

      在tomcat安裝目錄下創建oms-client、bms-client,在oms-client創建tomcat實例1 tomcat6-server1 並且將conf、logs、temp、webapp、work目錄拷貝到這兩個目錄,然後tomcat安裝目錄可以全部留下。配置後的目錄結構如下:

      \

      備注:截圖中有bin目錄,實際上bin目錄只會放重新寫的啟動和刪除.sh,原文件都已刪除

      3. 配置站點server.xml

      3.1 配置tomcat6-server1

      需要修改的端口是:Shutdown port,Connector port,ajp port和Redirect port。

      Shutdown port – 此端口用於關閉Tomcat。當執行shutdown.sh腳本時,它會給此端口發出一個信號,Tomcat的進程會監聽此端口,如果接收到這樣的信號,進程會清理退出。

      Connector port - 此端口是應用對外公開發布的端口。

      ajp port – Web服務器(例如Apache的httpd Server)通過此端口和Tomcat進行通信,也可以使用它設置一個負載均衡服務器。

      Redirect port – 如果此Connector支持非SSL請求和接收SSL請求,Catalina會自動將請求指向到此端口。

      修改Shutdown port

      \

      修改Connectors port 和 redirectPort

      \

      修改Connectors port 和 redirectPort\

      修改Host 加入

      其中docBase 為項目地址

      \

      3.2 配置tomcat6-server2

      同上,幾個端口號都要修改一樣

      4. 多實例啟動腳本

      在/home/bms-client/tomcat6-sever1/bin、/home/bms-client/tomcat6-sever2/bin目錄下分別創建啟動腳本tomcat.sh, 兩個tomcat.sh的區別主要就是CATALINA_BASE不同
      4.1 腳本內容tomcat.sh

      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #!/bin/sh # description: 啟動tomcat多實例. ./etc/init.d/functions RETVAL=$? # tomcat實例目錄 export CATALINA_BASE="/home/oms-client/tomcat6-server1" # tomcat安裝目錄 export CATALINA_HOME="/home/tomcat-6.0.45" # 可選 export JVM_OPTIONS="-Xms128m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m" case"$1"in start) if[-f$CATALINA_HOME/bin/startup.sh];then echo$"Start Tomcat" $CATALINA_HOME/bin/startup.sh fi ;; stop) if[-f$CATALINA_HOME/bin/shutdown.sh];then echo$"Stop Tomcat" $CATALINA_HOME/bin/shutdown.sh fi ;; *) echo$"Usage: $0 {start|stop}" exit1 ;; esac exit$RETVAL

      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #!/bin/sh # description: 啟動tomcat多實例. ./etc/init.d/functions RETVAL=$? # tomcat實例目錄 export CATALINA_BASE="/home/bms-client/tomcat6-server2" # tomcat安裝目錄 export CATALINA_HOME="/home/tomcat-6.0.45" # 可選 export JVM_OPTIONS="-Xms128m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m" case"$1"in start) if[-f$CATALINA_HOME/bin/startup.sh];then echo$"Start Tomcat" $CATALINA_HOME/bin/startup.sh fi ;; stop) if[-f$CATALINA_HOME/bin/shutdown.sh];then echo$"Stop Tomcat" $CATALINA_HOME/bin/shutdown.sh fi ;; *) echo$"Usage: $0 {start|stop}" exit1 ;; esac exit$RETVAL

      4.2 啟動腳本賦權限

      1 # chmod a+x tomcat.sh

      5. 啟動測試

      5.1 啟動/關閉a.ttlsa.com

      1 2 3 4 5 6 啟動 # cd /home/oms-client/tomcat6-server1/bin # ./tomcat.sh start 關閉 # cd /home/oms-client/tomcat6-server1/bin # ./tomcat.sh stop

      5.2 啟動/關閉b.ttlsa.com

      1 2 3 4 5 6 啟動 # cd /home/bms-client/tomcat6-server2/bin # ./tomcat.sh start 關閉 # cd /home/bms-client/tomcat6-server2/bin # ./tomcat.sh stop

      備注:一定需要cd到tomcat.sh的當前目錄下執行才可以

      具體XML修改結果為:

      <?xml version='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 information regarding copyright ownership.
        The ASF licenses this file to You under the Apache License, Version 2.0
        (the "License"); you may not use this 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 agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
      -->
      <!-- Note:  A "Server" is not itself a "Container", so you may not
           define subcomponents such as "Valves" at this level.
           Documentation at /docs/config/server.html
       -->
      <Server port="48005" shutdown="SHUTDOWN">
      
        <!--APR library loader. Documentation at /docs/apr.html -->
        <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
        <!--Initialize Jasp/" target="_blank">asper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
        <Listener className="org.apache.catalina.core.JasperListener" />
        <!-- Prevent memory leaks due to use of particular java/javax APIs-->
        <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
        <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
        <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
        <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
      
        <!-- Global JNDI resources
             Documentation at /docs/jndi-resources-howto.html
        -->
        <GlobalNamingResources>
          <!-- Editable user database that can also be used by
               UserDatabaseRealm to authenticate users
          -->
          <Resource name="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>
      
        <!-- A "Service" is a collection of one or more "Connectors" that share
             a single "Container" Note:  A "Service" is not itself a "Container", 
             so you may not define subcomponents such as "Valves" at this level.
             Documentation at /docs/config/service.html
         -->
        <Service name="Catalina">
        
          <!--The connectors can use a shared executor, you can define one or more named thread pools-->
          <!--
          <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
              maxThreads="150" minSpareThreads="4"/>
          -->
          
          
          <!-- A "Connector" represents an endpoint by which requests are received
               and responses are returned. Documentation at :
               Java/" target="_blank">Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
               Java AJP  Connector: /docs/config/ajp.html
               APR (HTTP/AJP) Connector: /docs/apr.html
               Define a non-SSL HTTP/1.1 Connector on port 8080
          -->
          <Connector port="48080" protocol="HTTP/1.1" 
                     connectionTimeout="20000" 
                     redirectPort="8445" />
          <!-- A "Connector" using the shared thread pool-->
          <!--
          <Connector executor="tomcatThreadPool"
                     port="8080" protocol="HTTP/1.1" 
                     connectionTimeout="20000" 
                     redirectPort="8443" />
          -->           
          <!-- Define a SSL HTTP/1.1 Connector on port 8443
               This connector uses the JSSE configuration, when using APR, the 
               connector should be using the OpenSSL style configuration
               described in the APR documentation -->
          <!--
          <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
                     maxThreads="150" scheme="https" secure="true"
                     clientAuth="false" sslProtocol="TLS" />
          -->
      
          <!-- Define an AJP 1.3 Connector on port 8009 -->
          <Connector port="48009" protocol="AJP/1.3" redirectPort="8445" />
      
      
          <!-- An Engine represents the entry point (within Catalina) that processes
               every request.  The Engine implementation for Tomcat stand alone
               analyzes the HTTP headers included with the request, and passes them
               on to the appropriate Host (virtual host).
               Documentation at /docs/config/engine.html -->
      
          <!-- You should set jvmRoute to support load-balancing via AJP ie :
          <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">         
          --> 
          <Engine name="Catalina" defaultHost="localhost">
      
            <!--For clustering, please take a look at documentation at:
                /docs/cluster-howto.html  (simple how to)
                /docs/config/cluster.html (reference documentation) -->
            <!--
            <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
            -->        
      
            <!-- The request dumper valve dumps useful debugging information about
                 the request and response data received and sent by Tomcat.
                 Documentation at: /docs/config/valve.html -->
            <!--
            <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
            -->
      
            <!-- This Realm uses the UserDatabase configured in the global JNDI
                 resources under the key "UserDatabase".  Any edits
                 that are performed against this UserDatabase are immediately
                 available for use by the Realm.  -->
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                   resourceName="UserDatabase"/>
      
            <!-- Define the default virtual host
                 Note: XML Schema validation will not work with Xerces 2.2.
             -->
            <Host name="localhost"  appBase="webapps"
                  unpackWARs="true" autoDeploy="true"
                  xmlValidation="false" xmlNamespaceAware="false">
      
            	<Context path="" docBase="/home/bms-client/hello" reloadable="false"/>
      	 <!-- SingleSignOn valve, share authentication between web applications
                   Documentation at: /docs/config/valve.html -->
              <!--
              <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
              -->
      
              <!-- Access log processes all example.
                   Documentation at: /docs/config/valve.html -->
              <!--
              <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
                     prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
              -->
      
            </Host>
          </Engine>
        </Service>
      </Server>
Copyright © Linux教程網 All Rights Reserved