實現單Tomcat多Server配置

目標是實現單個應用在同個tomcat中運行多個實例。每一個實例除端口、日誌輸出不同以外,其餘相同,應用只發布和配置一次。如下步驟是按照Tomcat 7.0.22進行的。java

  • 將默認tomcat中的conf/server.xml更名爲server1.xml,內容以下:
<?xml version='1.0' encoding='utf-8'?>
<Server port="9000" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <Service name="Catalina">
    <Connector port="9080" protocol="HTTP/1.1" connectionTimeout="20000" acceptCount="200" maxThreads="800" URIEncoding="UTF-8"/>
    <Connector port="9010" protocol="AJP/1.3" />
    <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true"></Host>
    </Engine>
  </Service>
</Server>
  •  按照一樣的作法,順着端口號+1造成server2.xml,server3.xml
  • 更改tomcat的日誌配置conf/logging.properties,造成單一的日誌輸出。
handlers = java.util.logging.ConsoleHandler
.handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
  • 編寫startServer.sh/stopServer.sh,能夠使用startServer.sh server1方式啓動,stopServer.sh server1中止服務器,如下是startServer.sh的樣例。
#!/bin/sh
PRG="$0"
PRGDIR=`dirname "$PRG"`
EXECUTABLE=catalina.sh
export CATALINA_OUT=`cd "$PRGDIR/.." >/dev/null; pwd`/logs/"$1".out
echo Using config :conf/"$1".xml
echo Using CATALINA_OUT:"$CATALINA_OUT"
exec "$PRGDIR"/"$EXECUTABLE" start -config conf/"$1".xml

如今就能夠在webapps中部署應用,Catalina/localhost配置上下文以及數據源了。web

相關文章
相關標籤/搜索