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 "%r" %s %b" /> </Host> </Engine> </Service> </Server>
5、後記
如此配置後,咱們就能夠建立多個tomcat實例,咱們能夠在每一個實例中部署一個項目,這樣咱們就能夠關閉從新部署一個項目時而不影響其它的項目。此爲一個tomcat啓動多個實例,而不是啓動多個tomcat。文中可能有不對的地方,歡迎指出