一、概述
方式一、同一个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