2-1-1 Introduction to the overall architecture and components of tomcat

1. Introduction of tomcat

1.1. Introduction to tomcat directory

1.2 Browser request server process of Tomcat overall architecture

Insert picture description here

注意:The browser uses the Http protocol to access the server. Http is an application layer protocol used to define the format of data communication.
The specific data transmission uses the TCP/IP protocol, which is a transport layer protocol.

Second, the introduction of tomcat

2.1 The general process of Tomcat processing request of the overall structure of Tomcat

Tomcat is an Http server (can receive and process http requests, so tomcat is a server).

We use a browser to initiate a request to a certain website, and the Http request is sent. Then after receiving the request in the remote, the Http server will call a specific program (Java class for processing), often different requests have different Java classes Complete processing.

Insert picture description here
Insert picture description here
After the HTTP server receives the request, it passes the request to the Servlet container for processing, and the Servlet container calls the business
class through the Servlet interface . Servlet接⼝和Servlet容器这⼀整套内容叫作Servlet规范.

注意:Tomcat not only implements the Servlet container in accordance with the requirements of the Servlet specification, but it also has the function of an HTTP server.

Two important identities of Tomcat

  1. http server
  2. Tomcat is a servlet container

2.2 Servlet container processing request process of Tomcat overall architecture

When the user requests a URL resource

  1. The HTTP server (tomcat) will encapsulate the request information with the ServletRequest object
  2. Further call a specific Servlet in the Servlet container
  3. In 2), after the Servlet container gets the request, it finds the corresponding Servlet according to the mapping relationship between URL and Servlet
  4. If the Servlet has not been loaded, use the reflection mechanism to create the Servlet, and call the init method of the Servlet to complete the initialization
  5. Then call the service method of this specific Servlet to process the request, and the request processing result is encapsulated with the ServletResponse object
  6. Return the ServletResponse object to the HTTP server, and the HTTP server will send the response to the client
    Insert picture description here

Insert picture description here

2.3 Overview of the overall architecture of Tomcat

Through the above explanation, we found that tomcat has two very important functions

  1. Interact with the client browser, perform socket communication, and convert the byte stream and Request/Response objects
  2. Servlet container handles business logic

Insert picture description here

Tomcat has designed two core component connectors (Connector) and container (Container) to complete the two core functions of Tomcat.

Connector , responsible for external communication: processing Socket connections, responsible for the transformation of network byte streams and Request and Response objects;
container , responsible for internal processing: loading and managing Servlet, and specifically processing Request requests

Three, Tomcat connector component Coyote

3.1. Introduction to the connector component Coyote and supported protocols and IO models

Coyete is the component name of the connector in Tomcat, which is the external interface. The client establishes a connection with the server through Coyote, sends a request and accepts a response.

  1. Coyote encapsulates the underlying network communication (Sockey request and response processing)
  2. Coyote is a Catalina container (container component) completely decoupled from the specific request protocol and IO operation mode
  3. Coyote converts the Socket input and encapsulates it into a Request object. After further encapsulation, it is handed over to the Catalina container for processing. After processing the request, Catalina provides a Response object through Coyote to write the result into the output stream.
  4. Coyote is responsible for specific protocols (application layer) and IO (transport layer) related content
    Insert picture description here
    Tomcat Coyote supports IO models and protocols
    Tomcat supports multiple application layer protocols and I/O models, as follows:

Insert picture description here
Before 8.0, the default I/O method used by Tomcat was BIO, which was later changed to NIO. Regardless of whether NIO, NIO2 or APR, their performance
is better than the previous BIO. If APR is used, it can even reach the performance impact of Apache HTTP Server.

3.2 Connector components Coyote internal components and workflow

Insert picture description here
Coyote components and functions

Four, Tomcat Servlet container Catalina

4.1 Tomcat module hierarchical structure and Catalina's status

Tomcat is a web container composed of a series of configurable components (conf/server.xml), while Catalina is a
servlet container of Tomcat .

From another perspective, Tomcat is essentially a servlet container, because Catalina is the core of Tomcat, and
other modules provide support for Catalina. For example: Coyote module provides link communication, Jasper module provides JSP
engine, Naming provides JNDI service, and Juli provides log service.

Insert picture description here

4.2, Servlet container Catalina structure

Tomcat (we often have an understanding, Tomcat is an instance of Catalina, because Catalina is the core of Tomcat)
Insert picture description here

In fact, the entire Tomcat can be considered as a Catelina instance. When Tomcat starts, this instance is initialized. The Catalina instance completes the creation of other instances by loading server.xml. The bed frame manages a Server, and the Server creates and manages multiple services. The service can have multiple Connectors and one Container.

One Catalina instance (container)
One Server instance (container)
Multiple Service instances (containers)
Each Service instance can have multiple Connector instances and one Container instance

  • Catalina
    is responsible for parsing the Tomcat configuration file (server.xml) to create and manage the server component

  • The Server
    server represents the entire Catalina Servlet container and other components, and is responsible for assembling and starting the Servlet engine and the Tomcat connector. Server provides an elegant way to start and shut down the entire system by implementing the Lifecycle interface.

  • Service
    service is an internal component of Server, and a Server contains multiple Services. It binds several Connerctor components to a Container

  • Container
    container, responsible for processing the user's servlet request, and returning the object to the module of the web user

4.3 The specific structure of the Container component

There are several specific components under the Container component, namely Engine, Host, Context and Wrapper. These 4 types of components (containers) are sub-relationships. Tomcat makes the Servlet container very flexible through a layered architecture.

  • Engine
    represents the entire Catalina Servlet engine, which is used to manage multiple virtual sites. A Service can only have one Engine at most, but one Engine can contain multiple Hosts.
  • Host
    represents a virtual host, or a site, Tomcat can be configured with multiple virtual host addresses, and a virtual host can contain multiple Context
  • Context
    represents a web application, and a web application can contain multiple wrappers
  • Wrapper
    represents a servlet. Wrapper is the bottom layer in the container and cannot contain sub-containers

The configuration of the above components is actually reflected in conf/server.xml.

Five, Tomcat server core configuration detailed explanation

5.1, Tomcat server core configuration instructions

Question 1: Where do I go to configure? Verify the configuration in the tomcat directory in the conf/server.xml file.
Question: How to configure?
note:

The configuration of Tomcat as a server is mainly the configuration of the server.xml file;
server.xml contains the configuration of the Servlet container, that is, the configuration of Catalina;
the explanation of the Xml file is mainly the use of tags

The main label structure is as follows:


<!--
 Server 根元素,创建⼀个Server实例,⼦标签有 Listener、GlobalNamingResources、
Service
-->
<Server>
 <!--定义监听器-->
 <Listener/>
 <!--定义服务器的全局JNDI资源 -->
 <GlobalNamingResources/>
 <!--
 定义⼀个Service服务,⼀个Server标签可以有多个Service服务实例
 -->
 <Service/>
</Server>

5.2 Server tab

<!--
 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>

5.3 Service label


<!--
 该标签⽤于创建 Service 实例,默认使⽤ org.apache.catalina.core.StandardService。
 默认情况下,Tomcat 仅指定了Service 的名称, 值为 "Catalina"。
 Service ⼦标签为 : Listener、Executor、Connector、Engine,
 其中:
 Listener ⽤于为Service添加⽣命周期监听器,
 Executor ⽤于配置Service 共享线程池,
 Connector ⽤于配置Service 包含的链接器,
 Engine ⽤于配置Service中链接器对应的Servlet 容器引擎
-->
<Service name="Catalina">
 ...
</Service>

5.4, ​​Executor label

<!--
默认情况下,Service 并未添加共享线程池配置。 如果我们想添加⼀个线程池, 可以在
<Service> 下添加如下配置:
 name:线程池名称,⽤于 Connector中指定
 namePrefix:所创建的每个线程的名称前缀,⼀个单独的线程名称为
namePrefix+threadNumber
 maxThreads:池中最⼤线程数
 minSpareThreads:活跃线程数,也就是核⼼池线程数,这些线程不会被销毁,会⼀直存在
 maxIdleTime:线程空闲时间,超过该时间后,空闲线程会被销毁,默认值为6000(1分钟),单位
毫秒
 maxQueueSize:在被执⾏前最⼤线程排队数⽬,默认为Int的最⼤值,也就是⼴义的⽆限。除⾮特
殊情况,这个值 不需要更改,否则会有请求不会被处理的情况发⽣
 prestartminSpareThreads:启动线程池时是否启动 minSpareThreads部分线程。默认值为
false,即不启动
 threadPriority:线程池中线程优先级,默认值为5,值从1到10
 className:线程池实现类,未指定情况下,默认实现类为 
org.apache.catalina.core.StandardThreadExecutor。如果想使⽤⾃定义线程池⾸先需要实现
org.apache.catalina.Executor接⼝
-->
<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"/>

Insert picture description here

5.5, Connector tab

The Connector tag is used to create linker instances.
By default, server.xml is configured with two linkers, one supports the HTTP protocol, and one supports the AJP protocol. In
most cases, we do not need to add a new linker configuration. Just optimize the existing linker as needed

<!--
port:
 端⼝号,Connector ⽤于创建服务端Socket 并进⾏监听, 以等待客户端请求链接。如果该属性设置
为0, Tomcat将会随机选择⼀个可⽤的端⼝号给当前Connector 使⽤
protocol:
 当前Connector ⽀持的访问协议。 默认为 HTTP/1.1 , 并采⽤⾃动切换机制选择⼀个基于 JAVA
NIO 的链接器或者基于本地APR的链接器(根据本地是否含有Tomcat的本地库判定)
connectionTimeOut:
Connector 接收链接后的等待超时时间, 单位为 毫秒。 -1 表示不超时。
redirectPort:
 当前Connector 不⽀持SSL请求, 接收到了⼀个请求, 并且也符合security-constraint 约束,
需要SSL传输,Catalina⾃动将请求重定向到指定的端⼝。
executor:
 指定共享线程池的名称, 也可以通过maxThreads、minSpareThreads 等属性配置内部线程池。

URIEncoding:
 ⽤于指定编码URI的字符编码, Tomcat8.x版本默认的编码为 UTF-8 , Tomcat7.x版本默认为ISO-
8859-1
-->
<!--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" />

Can use shared thread pool

<Connector port="8080" 
 protocol="HTTP/1.1"
 executor="commonThreadPool"
 maxThreads="1000" 
 minSpareThreads="100" 
 acceptCount="1000" 
 maxConnections="1000" 
 connectionTimeout="20000"
 compression="on" 
 compressionMinSize="2048" 
 disableUploadTimeout="true" 
 redirectPort="8443" 
 URIEncoding="UTF-8" />


5.6, Engine label and Host label

<!--
name: ⽤于指定Engine 的名称, 默认为Catalina
defaultHost:默认使⽤的虚拟主机名称, 当客户端请求指向的主机⽆效时, 将交由默认的虚拟主机处
理, 默认为localhost
-->
<Engine name="Catalina" defaultHost="localhost">
 ...
</Engine>

5.7, Host label

The Host tag is used to configure a virtual host

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
 ...
</Host>

Insert picture description here
Insert picture description here
Insert picture description here

5.8, Context tag

The Context tag is used to configure a web application, as follows:


<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="/Users/yingdian/web_demo" path="/web3"></Context> 
 
 <Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
 prefix="localhost_access_log" suffix=".txt"
 pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>


Two doubts:

1. The relationship between the components of the Container and the Catalina instance

Insert picture description here

The relationship between them

Two, BIO, NIO, NIO2

The relationship and difference between BIO, NIO, and NIO2

Guess you like

Origin blog.csdn.net/qq_42082278/article/details/111827075