一、概述
方式一、同一個Tomcat 同一個端口 部署多個項目
步驟一、默認tomcat支持多應用部署,將多個war包放置在tomcat下的webapps下即可
docker啟動
docker run -d --name tomcat8 -p 8083:8080 -v /Users/lihongxu6/docker/tomcat8/webapps:/usr/local/tomcat/webapps \
-v /Users/lihongxu6/docker/tomcat8/conf/server.xml:/usr/local/tomcat/conf/server.xml tomcat:8.5.40-jre8
步驟二、通過server查看基礎配置
<?xml version="1.0" encoding="UTF-8"?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!-- 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"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <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>
可以看到 監聽端口為:8080, 部署appBase 為webapps
訪問:【這里docker映射了8080到8083】
http://localhost:8083/test/aaa/index.html
http://localhost:8083/test1/index.html
http://localhost:8083/test2/index.html
步驟三、自定義虛擬路徑
上述訪問比較麻煩,規划成如下訪問
http://localhost:8083/test/aaa/index.html http://localhost:8083/auth/index.html
http://localhost:8083/test1/index.html http://localhost:8083/data/index.html
http://localhost:8083/test2/index.html http://localhost:8083/code/index.html
配置server.xml,在上述Host配置
<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" /> <Context docBase="test/aaa" path="/auth" reloadable="true" /> <Context docBase="test1" path="/data" reloadable="true" /> <Context docBase="test2" path="/code" reloadable="true" /> </Host>
再次訪問上述代碼即可。
path屬性:指定訪問該Web應用的URL入口。 如“/auth/”
docBase屬性:指定Web應用的文件路徑,可以給定絕對路徑,可以給定相對路徑。。如應用test1的文件路徑為<CATALINA_HOME>/webapps/test1
reloadable屬性:如果這個屬性設為true,tomcat服務器在運行狀態下會監視在WEB-INF/classes和WEB-INF/lib目錄下class文件的改動,如果監測到有class文件被更新的,服務器會自動重新加載Web應用。
方式二、同一個Tomcat 多個端口 部署多個項目
步驟一、$TOMCAT_HOME 路徑下新建文件夾webapps1,里面放要發布的項目 .war文件
Tomcat默認空間webapps,增加一個項目運行可以新建一個名為webapp-data(或者其他看實際情況)的文件夾,然后加入你要部署的新項目(myProject1)。
步驟二、更改conf中的配置文件:server.xml
新增Service節點
<?xml version="1.0" encoding="UTF-8"?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!-- 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" /> <GlobalNamingResources> <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"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <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" /> <Context docBase="test/aaa" path="/auth" reloadable="true" /> <Context docBase="test1" path="/data" reloadable="true" /> <Context docBase="test2" path="/code" reloadable="true" /> </Host> </Engine> </Service> <Service name="Catalina"> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8444" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps-data" 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" /> <Context docBase="test/aaa" path="/auth1" reloadable="true" /> <Context docBase="test1" path="/data1" reloadable="true" /> <Context docBase="test2" path="/code1" reloadable="true" /> </Host> </Engine> </Service> </Server>
步驟三、docker啟動
docker run -d --name tomcat82 -p 8081:8081 -p 8082:8080 --privileged=true -v /Users/lihongxu6/docker/tomcat8/webapps:/usr/local/tomcat/webapps \
-v /Users/lihongxu6/docker/tomcat8/webapps-data:/usr/local/tomcat/webapps-data -v /Users/lihongxu6/docker/tomcat8/conf/server.xml:/usr/local/tomcat/conf/server.xml \
tomcat:8.5.40-jre8
訪問:
http://localhost:8081/auth1/index.html
http://localhost:8081/data1/index.html
http://localhost:8081/code1/index.html
http://localhost:8082/auth/index.html
http://localhost:8082/data/index.html
http://localhost:8082/code/index.html