首页 > 代码库 > tomcat instance
tomcat instance
一、前言
以前一直不太明白CATALINA_HOME与CATALINA_BASE有什么不同,那时一般都是把多个项目同时部署到一个tomcat实例中,因此在配置环境变量的时候也就只配了CATALINA_HOME,并没有配过CATALINA_BASE,这是由于如果是tomcat单实例的话,tomcat会默认用CATALINA_HOME作为CATALINA_BASE,也就是说他们两个是同一个路径。今天仔细看了下,原来CATALINA_HOME与CATALINA_BASE是不同意义的。
CATALINA_HOME: tomcat的安装目录,里面其实只要两个文件夹就可以了 -bin与-lib
CATALINA_BASE:tomcat的工作目录, 里面有两个文件夹是必须的 -conf与-webapps,其它的logs,temp,work在项目部署时会自动生成
形象点说一个例子,就如eclipse: eclipse本身有一个安装目录,就是平时我们解压后的目录,这个就好比CATALINA_HOME,除了这个目录,我们在使用eclipse的时候还会指定一个workspace,这个就好比CATALINA_BASE
二、目录结构
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可以放到其它任何地方
CATALINA_BASE:
三、start.bat与stop.bat
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
四、server.xml配置
<?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>
五、后记
如此配置后,我们就可以创建多个tomcat实例,我们可以在每个实例中部署一个项目,这样我们就可以关闭重新部署一个项目时而不影响其它的项目。此为一个tomcat启动多个实例,而不是启动多个tomcat。文中可能有不对的地方,欢迎指出
tomcat instance