一臺機器同時執行兩個或多個Tomcat
阿新 • • 發佈:2018-12-30
今天經理交給我一個任務,讓我在伺服器上再裝一個測試用的tomcat,因為我們的系統中有用到呼叫移動CMPP簡訊介面給客戶發簡訊的功能,限制了IP,只能在伺服器上除錯。沒轍,以前也想過在自己的機器上同時跑兩個tomcat,以為只要改一下埠號就沒問題,其實這只是其中的一步而已。
當第一個tomcat啟動後,後面tomcat的server.xml中的埠不管怎麼改,仍然會報埠衝突。後來在dos下執行才發現所有的tomcat都會去找
CATALINA_HOME和CATALINA_BASE這兩個環境變數,因此步驟如下:
1.使用壓縮版的tomcat不能使用安裝版的。
2.第一個tomcat的配置不變。
3.增加環境變數CATALINA_HOME2,值為新的tomcat的地址;增加環境變數CATALINA_BASE2,值為新的tomcat的地址。
4.修改新的tomcat中的startup.bat
5.修改新的tomcat中的catalina.bat,把其中的CATALINA_HOME改為CATALINA_HOME2,CATALINA_BASE改為CATALINA_BASE2。
6.修改conf/server.xml檔案:
6.1 <Server port="8005" shutdown="SHUTDOWN">把埠改為沒有是使用的埠。
6.2 <Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" /> 把埠改為沒有是使用的埠。
6.3<Connector port="8009"
enableLookups="false" redirectPort="8443" protocol="AJP/1.3" /> 把埠改為沒有是使用的埠。
7成功!
下面是我配置好的server.xml
<?xml version='1.0' encoding='utf-8'?> <!-- 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 8005->8007 --> <Server port="8007" 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" /> <!-- 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> <Service name="Catalina"> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : 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->8090 --> <Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- Define an AJP 1.3 Connector on port 8009->8011 --> <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <!-- 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"> <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>