tomcat instance

1、前言
html

之前一直不太明白CATALINA_HOME与CATALINA_BASE有什么不一样,那时通常都是把多个项目同时部署到一个tomcat实例中,所以在配置环境变量的时候也就只配了CATALINA_HOME,并无配过CATALINA_BASE,这是因为若是是tomcat单实例的话,tomcat会默认用CATALINA_HOME做为CATALINA_BASE,也就是说他们两个是同一个路径。今天仔细看了下,原来CATALINA_HOME与CATALINA_BASE是不一样意义的。java

CATALINA_HOME: tomcat的安装目录,里面其实只要两个文件夹就能够了 -bin与-libweb

CATALINA_BASE:tomcat的工做目录, 里面有两个文件夹是必须的 -conf与-webapps,其它的logs,temp,work在项目部署时会自动生成shell

形象点说一个例子,就如eclipse: eclipse自己有一个安装目录,就是平时咱们解压后的目录,这个就比如CATALINA_HOME,除了这个目录,咱们在使用eclipse的时候还会指定一个workspace,这个就比如CATALINA_BASEapache

2、目录结构tomcat

CATALINA_HOME:
apache-tomcat-7.0.37
    -bin
    -lib

CATALINA_BASE:
tomcatBase
    -instance_1
        -conf
        -webapps
        -(启动、关闭实例的bat文件,每一个instance下面都有这两个文件,但不必定要放在这里,等会后面解释)
    -instance_2
        -conf
        -webapps
        -(启动、关闭实例的bat文件)
    -instance_3
        -conf
        -webapps
        -(启动、关闭实例的bat文件)

CATALINA_HOME和CATALINA_BASE能够放在任何地方,只要有权限就能够

CATALINA_HOME:里面的tomcatBase能够放到其它任何地方app

CATALINA_BASE:eclipse

3、start.bat与stop.batwebapp

REM start.bat

@echo off
echo tomcat base starting...
echo cleanning the cache...

setlocal

REM ---------------------
REM		clean cache
REM ---------------------
if exist logs rmdir /S/Q logs
if exist temp rmdir /S/Q temp
if exist work rmdir /S/Q work

REM 指定JAVA_HOME
SET JAVA_HOME=%JAVA_HOME% 
if not defined JAVA_HOME goto ERR_1
SET CATALINA_HOME=%CATALINA_HOME%
if not defined CATALINA_HOME goto ERR_2
SET PATH=%JAVA_HOME%/bin;%PATH%
REM 指定CATALINA_BASE,若是是放到了instance下的话,直接调用%CD%,就能够了,若是是放在其它地方,就便用具体的路径,能够是相对的路径,也能够是绝对路径
SET CATALINA_BASE=%CD%
SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava
SET JAVA_OPTS=-Xms256m -Xmx1024m
SET START_BAT=%CATALINA_HOME%/bin/startup.bat
if not exist %START_BAT% goto ERR_3
%START_BAT% start
goto SUCC_END

REM ---------------------
REM		error handle
REM ---------------------
:ERR_1
echo Error: please define JAVA_HOME first
goto ERR_END

:ERR_2
echo Error: please define CATALINA_HOME first
goto ERR_END

:ERR_3
echo Error: not find the file %START_BAT%
goto ERR_END

REM ---------------------
REM		error ending
REM ---------------------
:ERR_END
goto END

REM ---------------------
REM		succes
REM ---------------------
:SUCC_END
goto END

:END
REM ---------------------
REM		program end
REM ---------------------

endlocal

REM stop.bat

@echo off
echo tomcat base stopping...

setlocal

SET JAVA_HOME=%JAVA_HOME%
if not defined JAVA_HOME goto ERR_1
SET CATALINA_HOME=%CATALINA_HOME%
if not defined CATALINA_HOME goto ERR_2
REM 必定要指定CATALINA_BASE,tomcat会根据CATALINA_BASE去找server.xml,而后再向里面的特定的port发送关闭请求来关闭不一样的tomcat实例
SET CATALINA_BASE=%CD%
SET JAVA_OPTS=-Xms256m -Xmx1024m
SET SHUTDOWN_BAT=%CATALINA_HOME%/bin/shutdown.bat
if not exist %SHUTDOWN_BAT% goto ERR_3
%SHUTDOWN_BAT% start
goto SUCC_END

REM ---------------------------
REM		error handle
REM ---------------------------
:ERR_1
echo Error: please define JAVA_HOME first
goto ERR_END

:ERR_2
echo Error: please define CATALINA_HOME first
goto ERR_END

:ERR_3
echo Error: not find the file %SHUTDOWN_BAT%
goto ERR_END

REM ---------------------------
REM		error end
REM ---------------------------
:ERR_END
goto END

REM ---------------------------
REM		success
REM ---------------------------
:SUCC_END
goto END

:END
REM ---------------------------
REM		programe end
REM ---------------------------

endlocal

4、server.xml配置jvm

<?xml version='1.0' encoding='utf-8'?>

<!-- 这个是copy tomcat原本的server.xml,如下主要讲要改三个端口 -->

<!-- 这里的port要改,能够改成没有被系统占用的任何一个端口,每一个instance里的server.xml中的端口要不相同,tomcat默认是8005 -->
<Server port="8205" shutdown="SHUTDOWN">
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper 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" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

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

  <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"/>
    -->
    
    <!-- 此处port要改,这是咱们用来访问项目的端口,tomcat默认为 8080 -->
    <Connector port="8280" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- 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="8209" protocol="AJP/1.3" redirectPort="8443" />
    <!-- 此处port要改,tomcat默认为 8009 -->


    <!-- 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"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- 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"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- 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
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

5、后记

如此配置后,咱们就能够建立多个tomcat实例,咱们能够在每一个实例中部署一个项目,这样咱们就能够关闭从新部署一个项目时而不影响其它的项目。此为一个tomcat启动多个实例,而不是启动多个tomcat。文中可能有不对的地方,欢迎指出

相关文章
相关标签/搜索