一、简介
Tomcat服务器是一个开源的、轻量的Web服务器,是目前比较流行的Web服务器之一,在中小型系统和并发量并不是特别大的场景下被广泛的使用。
二、下载和目录结构
下载
可直接到Tomcat的官网进行下载(https://tomcat.apache.org/),在官网的左侧列有Tomcat比较大的版本,如Tomcat 7、Tomcat 8等
选择某个大的版本之后,右边就会列出该版本的最新版本,下面以Tomcat 8为例,其最高版本是8.5.54,每个版本又分为二级制版本和源码版本,如果下载源码版本,需要自己进行编译(可参考解压后包中build.txt文件说明)
ps:提到这里要说一下Tomcat 8.0.19里开始对于websocket协议的处理有个bug,会导致full GC,这个bug在8.0.33被修复了,之前也是排查了很久
https://www.mail-archive.com/search?l=dev@tomcat.apache.org&q=subject:%22%5C%5BBug+57546%5C%5D+Memory+Leak+in+SecureNioChannel%22&o=newest&f=1 One instance of “org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler” loaded by “java.net.URLClassLoader @ 0xe5613ed8” occupies 101,526,264 (48.71%) bytes. The memory is accumulated in one instance of “java.util.concurrent.ConcurrentHashMap$Node[]” loaded by “”.
目录结构
bin目录:里面放的是脚本(.sh或.bat文件),如:tomcat的启动、关闭脚本、查看tomcat版本的脚本(version.sh 线上排查问题时可了解Tomcat的具体版本)
conf目录:里面放的是配置文件,如:我们经常会用到的server.xml配置文件
lib目录:里面存放的是jar文件,是tomcat所依赖的jar
logs目录:默认里面是空的,当我们部署项目后,会在里面生成日志。如经常用到的catalina的日志
webapps目录:里面存放具体的应用,我们可以把我们的项目放到这个目录
三、Tomcat的整体结构和配置文件
整体结构
各组件含义如下:
1、Server组件
Server组件表示整个Servlet容器,整个Tomcat运行环境只有一个实例
2、Service组件
Service表示一个或多个Connector的集合,这些Connector共享同一个Container来处理其请求。在同一个Tomcat实例内可以包含任意多个Service实例,他们彼此独立。
3、Connector组件
Connector即为链接器,用于监听并转化Socket请求,同事将读取的Socket请求交由Container处理,支持不同协议和不同的I/O方式
4、Container组件
Container表示能够执行客户端请求并返回响应的一类对象。在Tomcat中存在不同级别的容器,Engine、Host、Context、Wrapper
配置文件
配置文件的结构和Tomcat的整体结构是一致的,默认的配置文件对于每个部分的注释都比较清晰。
<?xml version="1.0" encoding="UTF-8"?>
<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
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 an SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the
AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
This connector uses the APR/native implementation which always uses
OpenSSL for TLS.
Either JSSE or OpenSSL style configuration may be used. OpenSSL style
configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!--
<Connector protocol="AJP/1.3"
address="::1"
port="8009"
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>
通过这一篇对Tomcat的整体结构有了初步的认识,了解了Tomcat核心模块的构成和怎么组装起来的,下面一篇来调试源码,看下Tomcat是如何启动的