测试本地Tomcat配置多个Service组件在不同端口接受不同请求,原来的server.xml配置如下:
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--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>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<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>
我们知道server.xml包含的组件包括Server、Service、Connector、Engine、Host、Context等。
其中Server是根元素,一个Tomcat对应一个Server。一个Service对应一个服务实例,<Servicename="Catalina">它用来将Connector元素和Engine元素包装在一起接受客户端发送的请求,处理后生成响应。可以包含多个Service元素。一个Service可以包含多个Connector和一个Engine,其中的Connector作用是接收客户端发送的请求,生成Request和Response将请求交给Engine处理并返回响应。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
Connector包含了接收请求的端口,请求的协议以及重定向的端口。常见的协议比如HTTP协议和AJP协议,AJP协议用于Tomcat和其他服务器直接进行通信,redirectPort用于如果只接受HTTPS协议而实际发送的请求却是HTTP协议,则将请求重定向到redirectPort指定的端口。
<Engine name="Catalina" defaultHost="localhost">
一个Service只包含一个Engine,它用于处理Connector接收的请求,name属性和Service的name属性一致,一个Engine可以包含多个Host。如果请求的主机地址在Engine的Host元素配置中没匹配到,就将请求交给defaultHost属性指定的主机。一个Engine必须包含一个defaultHost指定的Host元素配置。
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
一个Host元素相当于一个处理请求的虚拟主机,appBase属性指定了web应用部署的路径,unpackWARs指定了是否解压WAR包根据解压后的WAR包路径部署,autoDeploy指定是否自动部署,如果是自动部署,则更新部署的应用文件后,Tomcat会重新加载更新后的文件,所以一般使用自动部署,如果不使用自动部署可以在Host中配置Context。
现在新增一个Service元素,来将同一web应用部署在不同端口,接收客户端的请求:
<Service name="test">
<Connector port="8089" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8099" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="test" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps2"
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>
需要修改Connector的端口号,注意不要冲突,然后启动Tomcat,出现下面的错误:
Exception in thread "main"
四月 27, 2018 2:36:26 下午 org.apache.tomcat.util.digester.Digester startElement
严重: Begin event threw error
java.lang.OutOfMemoryError: PermGen space
四月 27, 2018 2:36:28 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-8.0.39\webapps2\manager
四月 27, 2018 2:36:28 下午 org.apache.tomcat.util.digester.Digester endElement
严重: End event threw error
java.lang.OutOfMemoryError: PermGen space
四月 27, 2018 2:36:29 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-8.0.39\webapps2\ROOT
四月 27, 2018 2:36:29 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\Program Files\apache-tomcat-8.0.39\webapps2\ROOT has finished in 18 ms
四月 27, 2018 2:36:29 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-8.0.39\webapps2\webstandard
Exception in thread "Timer-1"
四月 27, 2018 2:36:41 下午 org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor run
严重: Unexpected death of background thread ContainerBackgroundProcessor[StandardEngine[Catalina]]
java.lang.OutOfMemoryError: PermGen space
从错误日志可以看出是永久代内存溢出,可以通过在MyEclipse的Window->Preferences->MyEclipse->Servers->启动应用的Tomcat->JDK在Optional Java VM arguments中手动配置虚拟机内存参数:
-XX:MaxPermSize=256M -XX:MaxPermSize=512M
如果任然出现错误,可以继续增加内存大小。