Tomcat8.5 实现不同端口访问不同web应用
1、项目背景
在近期的项目开发的需要同时访问两个不同的JavaWeb应用,网上的解决方案有许多,最后选择了通过端口来实现这一需求。
2、实现原理
通过对Tomcat8.5中的conf下server.xml配置文件,设置不同的service节点的端口及 Context节点中的baseDoc目录来实现该需求。
3、实现步骤
3.1 寻找Server.xml文件
找到Server.xml文件,做备份后,开始修改文件内容。
3.2 打开Server.xml ,找到“<service name=”节点,如图红色区域内容
复制<service > </service>之间的内容,并设置<Service name="Catalina.myweb01">如下:
设置<Service name="Catalina.myweb02”>如下:
保存文件。
3.3 重新启动Tomcat8.5,查看Conf目录下是否生成“Catalina.myweb01”和“Catalina.myweb02”
如果没有生成,就运行bin目录的的
这样就可以生成 “Catalina.myweb01”和“Catalina.myweb02”,再启动Tomcat服务。
4、测试
预期结果:http://localhost:8081/ 显示内容是“this is a test01! hahahahaha”;
预期结果:http://localhost:8082/ 显示内容是“this is a test02! hahahahaha”;
测试结果如下:
需求实现,和预期的结果一致。
5、总结
在重启Tomcat时,只启动服务是无法生成Service 的节点文件的。再经过多次试验和分析发现这一特性。最后附上Server.xml文件,分享出来,希望对别人有所帮助,至少不走弯路。
附:Server.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<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" />
<Connector port="8080" protocol="AJP/1.3" 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>
<Service name="Catalina.myweb01">
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina.myweb01" 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 path="" docBase="C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\test01" reloadable="true"
caseSensitive="false" debug="0"></Context>
</Host>
</Engine>
</Service>
<Service name="Catalina.myweb02">
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina.myweb02" 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 path="" docBase="C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\test02" reloadable="true"
caseSensitive="false" debug="0"></Context>
</Host>
</Engine>
</Service>
</Server>