一、简介

Tomcat服务器是一个开源的、轻量的Web服务器,是目前比较流行的Web服务器之一,在中小型系统和并发量并不是特别大的场景下被广泛的使用。

二、下载和目录结构

下载

可直接到Tomcat的官网进行下载(https://tomcat.apache.org/),在官网的左侧列有Tomcat比较大的版本,如Tomcat 7、Tomcat 8等
Tomcat概述 - 图7image.gif
选择某个大的版本之后,右边就会列出该版本的最新版本,下面以Tomcat 8为例,其最高版本是8.5.54,每个版本又分为二级制版本和源码版本,如果下载源码版本,需要自己进行编译(可参考解压后包中build.txt文件说明)
Tomcat概述 - 图9image.gif
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 “”.

目录结构

Tomcat概述 - 图11image.gif

bin目录:里面放的是脚本(.sh或.bat文件),如:tomcat的启动、关闭脚本、查看tomcat版本的脚本(version.sh 线上排查问题时可了解Tomcat的具体版本)
conf目录:里面放的是配置文件,如:我们经常会用到的server.xml配置文件
lib目录:里面存放的是jar文件,是tomcat所依赖的jar
logs目录:默认里面是空的,当我们部署项目后,会在里面生成日志。如经常用到的catalina的日志
webapps目录:里面存放具体的应用,我们可以把我们的项目放到这个目录

三、Tomcat的整体结构和配置文件

整体结构

Tomcat概述 - 图13image.gif

各组件含义如下:
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的整体结构是一致的,默认的配置文件对于每个部分的注释都比较清晰。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Server port="8005" shutdown="SHUTDOWN">
  3. <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  4. <!-- Security listener. Documentation at /docs/config/listeners.html
  5. <Listener className="org.apache.catalina.security.SecurityListener" />
  6. -->
  7. <!--APR library loader. Documentation at /docs/apr.html -->
  8. <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  9. <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  10. <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  11. <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  12. <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  13. <!-- Global JNDI resources
  14. Documentation at /docs/jndi-resources-howto.html
  15. -->
  16. <GlobalNamingResources>
  17. <!-- Editable user database that can also be used by
  18. UserDatabaseRealm to authenticate users
  19. -->
  20. <Resource name="UserDatabase" auth="Container"
  21. type="org.apache.catalina.UserDatabase"
  22. description="User database that can be updated and saved"
  23. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
  24. pathname="conf/tomcat-users.xml" />
  25. </GlobalNamingResources>
  26. <!-- A "Service" is a collection of one or more "Connectors" that share
  27. a single "Container" Note: A "Service" is not itself a "Container",
  28. so you may not define subcomponents such as "Valves" at this level.
  29. Documentation at /docs/config/service.html
  30. -->
  31. <Service name="Catalina">
  32. <!--The connectors can use a shared executor, you can define one or more named thread pools-->
  33. <!--
  34. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
  35. maxThreads="150" minSpareThreads="4"/>
  36. -->
  37. <!-- A "Connector" represents an endpoint by which requests are received
  38. and responses are returned. Documentation at :
  39. Java HTTP Connector: /docs/config/http.html
  40. Java AJP Connector: /docs/config/ajp.html
  41. APR (HTTP/AJP) Connector: /docs/apr.html
  42. Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
  43. -->
  44. <Connector port="8080" protocol="HTTP/1.1"
  45. connectionTimeout="20000"
  46. redirectPort="8443" />
  47. <!-- A "Connector" using the shared thread pool-->
  48. <!--
  49. <Connector executor="tomcatThreadPool"
  50. port="8080" protocol="HTTP/1.1"
  51. connectionTimeout="20000"
  52. redirectPort="8443" />
  53. -->
  54. <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
  55. This connector uses the NIO implementation. The default
  56. SSLImplementation will depend on the presence of the APR/native
  57. library and the useOpenSSL attribute of the
  58. AprLifecycleListener.
  59. Either JSSE or OpenSSL style configuration may be used regardless of
  60. the SSLImplementation selected. JSSE style configuration is used below.
  61. -->
  62. <!--
  63. <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
  64. maxThreads="150" SSLEnabled="true">
  65. <SSLHostConfig>
  66. <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
  67. type="RSA" />
  68. </SSLHostConfig>
  69. </Connector>
  70. -->
  71. <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
  72. This connector uses the APR/native implementation which always uses
  73. OpenSSL for TLS.
  74. Either JSSE or OpenSSL style configuration may be used. OpenSSL style
  75. configuration is used below.
  76. -->
  77. <!--
  78. <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
  79. maxThreads="150" SSLEnabled="true" >
  80. <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
  81. <SSLHostConfig>
  82. <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
  83. certificateFile="conf/localhost-rsa-cert.pem"
  84. certificateChainFile="conf/localhost-rsa-chain.pem"
  85. type="RSA" />
  86. </SSLHostConfig>
  87. </Connector>
  88. -->
  89. <!-- Define an AJP 1.3 Connector on port 8009 -->
  90. <!--
  91. <Connector protocol="AJP/1.3"
  92. address="::1"
  93. port="8009"
  94. redirectPort="8443" />
  95. -->
  96. <!-- An Engine represents the entry point (within Catalina) that processes
  97. every request. The Engine implementation for Tomcat stand alone
  98. analyzes the HTTP headers included with the request, and passes them
  99. on to the appropriate Host (virtual host).
  100. Documentation at /docs/config/engine.html -->
  101. <!-- You should set jvmRoute to support load-balancing via AJP ie :
  102. <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
  103. -->
  104. <Engine name="Catalina" defaultHost="localhost">
  105. <!--For clustering, please take a look at documentation at:
  106. /docs/cluster-howto.html (simple how to)
  107. /docs/config/cluster.html (reference documentation) -->
  108. <!--
  109. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
  110. -->
  111. <!-- Use the LockOutRealm to prevent attempts to guess user passwords
  112. via a brute-force attack -->
  113. <Realm className="org.apache.catalina.realm.LockOutRealm">
  114. <!-- This Realm uses the UserDatabase configured in the global JNDI
  115. resources under the key "UserDatabase". Any edits
  116. that are performed against this UserDatabase are immediately
  117. available for use by the Realm. -->
  118. <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  119. resourceName="UserDatabase"/>
  120. </Realm>
  121. <Host name="localhost" appBase="webapps"
  122. unpackWARs="true" autoDeploy="true">
  123. <!-- SingleSignOn valve, share authentication between web applications
  124. Documentation at: /docs/config/valve.html -->
  125. <!--
  126. <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
  127. -->
  128. <!-- Access log processes all example.
  129. Documentation at: /docs/config/valve.html
  130. Note: The pattern used is equivalent to using pattern="common" -->
  131. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  132. prefix="localhost_access_log" suffix=".txt"
  133. pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  134. </Host>
  135. </Engine>
  136. </Service>
  137. </Server>

image.gif
通过这一篇对Tomcat的整体结构有了初步的认识,了解了Tomcat核心模块的构成和怎么组装起来的,下面一篇来调试源码,看下Tomcat是如何启动的