目录
首先带着疑问
问题一:去哪配置?
核心配置在Tomcat目录下conf/
目录下的server.xml
文件中问题二:怎么配置?
如下
<!--
Server 根元素,创建⼀个Server实例,⼦标签有 Listener、GlobalNamingResources、Service
-->
<Server>
<!--定义监听器-->
<Listener/>
<!--定义服务器的全局JNDI资源 -->
<GlobalNamingResources/>
<!-- 定义⼀个Service服务,⼀个Server标签可以有多个Service服务实例 -->
<Service/>
</Server>
说明Tomcat来监听port端口来执行关闭
<!--
port:关闭服务器的监听端⼝
shutdown:关闭服务器的指令字符串
-->
<Server port="8005" shutdown="SHUTDOWN">
<!-- 以⽇志形式输出服务器 、操作系统、JVM的版本信息 -->
<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 -->
<!-- 加载(服务器启动) 和 销毁 (服务器停⽌) APR。 如果找不到APR库, 则会输出⽇志, 并不影响 Tomcat启动 -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<!-- 避免JRE内存泄漏问题 -->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<!-- 加载(服务器启动) 和 销毁(服务器停⽌) 全局命名服务 -->
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<!-- 在Context停⽌时重建 Executor 池中的线程, 以避免ThreadLocal 相关的内存泄漏 -->
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html
GlobalNamingResources 中定义了全局命名服务
-->
<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">
...
</Service>
</Server>
Listener
GlobalNamingResources (中定义了全局命名服务)
Service (如下)
以上标签Listener
、GlobalNamingResources
一般情况下保持默认即可,不需要而外操作,重点在于Service 标签
<Service name="Catalina">
...
</Service>
该标签⽤于创建 Service 实例,默认使⽤ org.apache.catalina.core.StandardService
。 默认情况下,Tomcat 仅指定了Service 的名称, 值为 "Catalina"。
用来定义共享线程池的。
默认是被注释掉的,且属性不全。那么该如何使用呢?
默认情况下,Service 并未添加共享线程池配置。 如果我们想添加⼀个线程池, 可以在 下添加如下配置:
<Executor name="commonThreadPool"
namePrefix="thread-exec-"
maxThreads="200"
minSpareThreads="100"
maxIdleTime="60000"
maxQueueSize="Integer.MAX_VALUE"
prestartminSpareThreads="false"
threadPriority="5"
className="org.apache.catalina.core.StandardThreadExecutor"/>
org.apache.catalina.core.StandardThreadExecutor
。如果想使⽤⾃定义线程池⾸先需要实现 org.apache.catalina.Executor
接⼝Connector 标签⽤于创建链接器实例默认情况下,server.xml
配置了两个链接器,⼀个⽀持HTTP协议,⼀个⽀持AJP协议,⼤多数情况下,我们并不需要新增链接器配置,只是根据需要对已有链接器进⾏优化。
<!--org.apache.coyote.http11.Http11NioProtocol , ⾮阻塞式 Java NIO 链接器-->
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
maxThreads
、minSpareThreads
等属性配置内部线程池。可以使用共享线程池:
<Connector port="8080"
protocol="HTTP/1.1"
executor="commonThreadPool"
maxThreads="1000"
minSpareThreads="100"
acceptCount="1000" //往往和maxThreads保持一致
maxConnections="1000" //往往和maxThreads保持一致
connectionTimeout="20000"
compression="on" //要不要启动gzip压缩
compressionMinSize="2048" //压缩处理的最小大小,超过此大小才压缩
disableUploadTimeout="true" //单独的给servlet放宽超时时间
redirectPort="8443"
URIEncoding="UTF-8" />
可以看到
Connector
标签和Executor
都有maxThreads
和minSpareThreads
标签。每一个Connector都可以自己定义一个线程池,如果每个Connector都自己定义线程池就浪费了。所以为什么不用一个呢?于是就有了Executor标签来定义一个共享线程池。
Engine 表示 Servlet 引擎
<Engine name="Catalina" defaultHost="localhost">
...
</Engine>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
...
</Host>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
当请求来临时,日志存放起来,
directory
值的目录<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
hostname是localhost,appBase是webapps,去webapps文件夹下找应用程序文件夹,没有指定,默认就去ROOT文件夹下:
www.abc.com
,www.def.com
。appBase分别为webapps
、webapps2
可以看到localhost:8080访问不到了,因为这个虚拟主机被我们修改了
而abc和def均可以找到。同时def.com的Home也被修改为Home-webapps2了。
用于配置一个Web应用——一个虚拟主机下可以配置多个应用
<Host name="www.abc.com" appBase="webapps" unpackWARs="true"
autoDeploy="true">
<!--
docBase:Web应⽤⽬录或者War包的部署路径。可以是绝对路径,也可以是相对于 Host appBase的
相对路径。
path:Web应⽤的Context 路径。如果我们Host名为localhost, 则该web应⽤访问的根路径为:
http://localhost:8080/web_demo。
-->
<Context docBase="E://dengxhh" path="/dengxh"></Context>
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
刚才我们访问的是
http://www.abc.com:8080
,那么现在我想输入http://www.abc.com:8080/dengxh
,dengxh所代表的位置是我的磁盘上的某个文件夹,这个文件夹里放的我的程序资源。如何做?
为了方便演示,我们就拿ROOT中的程序做例子,把webapps中的ROOT文件夹复制到E盘根目录,取名为dengxhh
在Host标签中,增加 <Context docBase="E://dengxhh" path="/dengxh"></Context>
重启Tomcat,访问www.abc.com/dengxh/ ,成功跳转了。
如此操作就可以在一个虚拟主机中,配置多个应用,只需要在host标签中配置多个Context标签,区分的时候只需要在虚拟主机后跟上Context的path属性的值即可找到对应的资源。
例如:Host name="www.abc.com" ,Context path ="dengxh" ,那么浏览器中输入 www.abc.com/dengxh
即可。
思维导图在线观看地址:https://www.processon.com/view/link/5fe4da565653bb054783ca6e
评论已关闭。