在开发中遇到过这样一种需求:客户端希望通过8080端口访问的是一种资源,而通过8089访问的是另一种资源,在只使用一个Tomcat的情况下我们需要怎么做呢?
一提到端口,我们自然而然的就想到了Tomcat的conf目录下的server.xml,我们先来看看里面的内容:
<?xml version='1.0' encoding='utf-8'?>
<!--指定8005端口监听关闭Tomcat请求 -->
<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表示客户端与service之间的连接,prot用来指定服务端要创建的端口号,在这个端口监听客户端的请求 -->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!--接收而处理来自connector的请求 -->
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<!--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" />
<!--表示一个web应用程序 -->
<Context path="/test" docBase="D:\demo_test.war" reloadable="true" />
</Host>
</Engine>
</Service>
</Server>
以上注释了部分节点的含义,通过在<Host>
节点中添加<Context>
来指定一个web应用,这时我们启动tomcat,在地址栏里输入http://locathost:8080/test/index.jsp
就可以访问到demo_test里面的资源。接下来我们该如何指定第二个端口去访问第二个呢?我们可以通过再添加一个<Service>
节点来达到这个目的,具体的配置内容如下:
<?xml version='1.0' encoding='utf-8'?>
<!--指定8005端口监听关闭Tomcat请求 -->
<Server port="8005" shutdown="SHUTDOWN">
<Service name="Catalina">
......
</Service>
<Service name="formal">
<Connector port="8090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="formal" 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="/formal" docBase="D:\demo_formal" reloadable="true" />
</Host>
</Engine>
</Service>
</Server>
通过添加一个新的<Service>
节点,指定其端口号为8090,通过<Context>
节点添加web应用,这时我们重新启动Tomcat,在地址栏里输入http://locathost:8090/formal/index.jsp
,我们就可以访问到demo_formal中的资源了。
tips:在做的过程中遇到一个问题,通过两个端口部署到服务器之后其中8090端口外网无法访问,当时的原因是服务器的防火墙没有对8090端口进行开放,在防火墙中的高级设置中将需要开放的端口配置到入站出站规则中即可。