Summary of tomcat knowledge points

The core of tomcat?

Connector和Container:
	1、Connector用于处理连接相关的事情,底层是socket通信,将http请求和服务端的响应封装成request和respones; 
	2、Container用于封装和管理Servlet,以及具体处理Request请求;
一个Tomcat中只有一个Server,一个Server可以包含多个Service,一个Service只有一个Container,但是可以有多个Connector,这是因为一个服务可以有多个连接,如同时提供Http和Https连接,也可以提供向相同协议不同端口的连接。

一个请求发送到Tomcat之后,首先经过Service然后会交给Connector,Connector用于接收请求并将接收的请求封装为Request和Response来具体处理,Request和Response封装完之后再交由Container进行处理,Container处理完请求之后再返回给Connector,最后在由Connector通过Socket将处理的结果返回给客户端,这样整个请求的就处理完了!
Connector最底层使用的是Socket来进行连接的,Request和Response是按照HTTP协议来封装的,所以Connector同时需要实现TCP/IP协议和HTTP协议!

Top-level architecture of tomcat:

(1) There is only one Server in Tomcat, one Server can have multiple Services, and one Service can have multiple Connectors and one Container;
(2) Server is in charge of the life and death of the entire Tomcat;
(4) Service provides services to the outside world;
(5) Connector is used to accept requests and encapsulate the requests into Request and Response for specific processing;
(6) Container is used to encapsulate and manage Servlet, and specifically process request requests;

Connector?

Connector uses ProtocolHandler to process requests. Different ProtocolHandlers represent different connection types. For example, Http11Protocol uses ordinary Socket to connect, and Http11NioProtocol uses NioSocket to connect.
The ProtocolHandler consists of three components: Endpoint, Processor, and Adapter.
(1) The Endpoint is used to process the network connection of the underlying Socket, the Processor is used to encapsulate the Socket received by the Endpoint into a Request, and the Adapter is used to hand the Request to the Container for specific processing.
(2) Endpoint is used to process the underlying Socket network connection, so Endpoint is used to implement TCP and IP protocols, and Processor is used to implement HTTP protocols, and Adapter adapts the request to the Servlet container for specific processing.
(3) The abstract implementation of Endpoint implements two internal classes of Acceptor and AsyncTimeout defined in AbstractEndpoint and a Handler interface. Acceptor is used to monitor requests, AsyncTimeout is used to check the timeout of asynchronous Requests, Handler is used to process the received Socket, and the Processor is called internally for processing.

Start the process?

1,创建Bootstrap启动类实例,并做一些初始化工作,初始化类加载器和创建Catalina实例,然后再启动Catalina线程。Catalina实例执行start方法。load方法解析server.xml配置文件,并加载Server、Service、Connector、Container、Engine、Host、Context、Wrapper一系列的容器。加载完成后,调用getServer().start()来开启一个新的Server。首先利用Digester类解析server.xml文件,得到容器的配置,并创建相应的对象,并关联父子容器。依次创建的是StandardServer、StandardService、StandardEngine、StandardHost。
然后拿到StandardServer实例调用init()方法初始化Tomcat容器的一系列组件。一些容器初始化的的时候,都会调用其子容器的init()方法,初始化它的子容器。顺序是StandardServer、StandardService、StandardEngine、Connector。每个容器都在初始化自身相关设置的同时,将子容器初始化。

在初始化、开启一系列组件、容器的过程中,由tomcat管理的组件和容器,都有一个共同的特点,都实现了Lifecycle接口,由Tomcat管理其生命周期。Lifecycle提供一种统一的管理对象生命周期的接口。通过Lifecycle、LifecycleListener、LifecycleEvent,Catalina实现了对tomcat各种组件、容器统一的启动和停止的方式。
在Tomcat服务开启过程中启动的一些列组件、容器,都继承了LifecycleBase这个抽象类,其中的init()、start() 方法、stop() 方法,为其子类实现了统一的start和stop管理。方法中具体的方法由子类去实现,这里采用的是模板方法模式。

总结Tomcat启动的过程:
	在Catalina的load方法里,就已经调用了StandardServer里的init方法,一层一层初始化了globalNamingResources,StandardService--》StandardEngine,executors,MapperListener,Connector--》CoyoteAdapter,protocolHandler。至此就将tomcat的catalina中的组件、容器初始化完成。 接下来就是调用start方法一层一层开启,StandardServer的startInternal方法,按层次start:globalNamingResources,StandardService--》StandardEngine,executors,MapperListener,Connector--》StandardHost,StandardContext,protocolHandler。顺序基本同init过程。StandardEngine在start时,会init子容器,并调用子容器的start方法。子容器依次这样init、start,就开启了StandardHost和StandardContext。


2,Bootstrap的初始化init方法:
	1,先初始化类加载器(commonLoader,catalinaLoader,sharedLoader)。
	2,给当前线程设置特定的类加载器catalinaLoader。
	3,向SecurityClassLoad安全机制的类加载器中添加catalinaLoader类加载器。
	4,初始化Catalina启动类,并根据构造函数startupClass.getConstructor().newInstance()创建Catalina启动类。
	5,设置启动参数。

	Bootstrap的初始化load方法(加载参数)。

3,Bootstrap的demon.start()方法就会调用Catalina的start方法。

The chain of responsibility model?

The chain of responsibility mode is that many objects have each object's reference to it and are connected to form a chain, and the request is passed on this chain until an object on the chain processes the request, or each object can process the request , And pass it to the next one until every object on the chain is processed. In this way, any processing node can be added to the chain without affecting the client.
This design mode is almost completely used in tomcat. The container setting of tomcat is the chain of responsibility mode. From Engine to Host to Context to Wrapper, requests are passed through a chain.

Template method pattern?

Tomcat has two main methods init() and start() during the startup process. Here we take start() as an example. The start() method in the LifecycleBase class will call startInternal(). This method is an abstract method. The subclass implements it, so the subclass needs to implement this specific opening method. The start method that is finally called is consistent with other fixed steps, and the specific opening implemented by the subclass will also be executed.

tomcat wrong?

When tomcat starts abnormally, the general situation is to go to the logs directory and check the cetalina.log log information to find the abnormality. If an exception occurred during operation, it is placed in localhost.log.

Embedded Tomcat?

非传统的部署方式,将Tomcat嵌入到主程序中进行运行。
优点:
	灵活部署、任意指定位置、通过复杂的条件判断。比如,springboot中内嵌的tomcat。
Tomcat类:Tomcat类是外部调用的入口
1.位置:org.apache.catalina.startup.Tomcat
2.该类是public的。
3.该类有Server、Service、Engine、Connector、Host等属性。
4.该类有init()、start()、stop()、destroy()等方法。

Embedded tomcat source code in springboot?

@EnableAutoConfiguration in @SpringBootApplication, the tomcat configuration is turned on by default, and there is an internal class EmbeddedTomcat in EmbeddedServletContainerAutoConfiguration that is the startup entry of embedded tomcat, which is implemented by calling the Tomcat class.

Maven integrated Tomcat plug-in startup analysis?

Tomcat7RunnerCli is a boot class, Tomcat7RunnerCli mainly relies on Tomcat7Runner, Tomcat7Runner calls the Tomcat class.

There are three operating modes for Tomcat?

Bio, nio, apr, the operating efficiency of Tomcat in different modes is quite different.
apr (Apache Portable Runtime):
To solve asynchronous IO problems from the operating system level, greatly improve performance, apr and native must be installed, and apr is supported when directly started.

Tomcat configures the number of threads?

Configure the maximum number of threads in, which is a thread pool and connection timeout period.

tomcat optimization?

1. Increase the number of threads in the thread pool.
2. Increase the heap memory. Generally, you can specify 80% of the physical memory. Add configuration information to the configuration file.
3. Set enableLookups="false" in the connector to cancel the DNS check.
4. Set compression. =”on” to compress
5, set useURIValidationHack to “false” to reduce unnecessary checking of some URLs and reduce overhead.

How many ways to deploy tomcat?

1. Put the web project directly under webapps, Tomcat will automatically deploy it
2. Configure the node in the server.xml file, set the relevant attribute (doBase)
, and then configure it through Catalina: enter conf\Catalina Under the \localhost file, create an xml file whose name is the name of the site.

How does tomcat container create servlet class instance? What principle was used? ?

When the container starts, it reads the web.xml files in all web applications in the webapps directory, then parses the xml files, and reads the servlet registration information. Then, the servlet classes registered in each application are loaded and instantiated by reflection.
(Sometimes it is also instantiated on the first request) In the servlet registration, if it is a positive number, it will be instantiated at the beginning. If it is not written or a negative number, it will be instantiated for the first time.

Tomcat's class loading mechanism?

The class loading mechanism of tomcat violates the parent delegation mechanism of jdk. To achieve isolation, each webappClassLoader loads class files in its own directory and will not be passed to the parent class loader. The classes that CommonClassLoader can load can be used by CatalinaClassLoader and SharedClassLoader, thus realizing the sharing of public class libraries, while the classes that CatalinaClassLoader and Shared ClassLoader can load themselves are isolated from each other. WebAppClassLoader can use the classes loaded into SharedClassLoader, but each WebAppClassLoader instance is isolated from each other. If the CommonClassLoader of tomcat wants to load the classes in the WebAppClassLoader, it can be implemented using the thread context class loader. Using the thread context loader, the parent class loader can request the child class loader to complete the action of class loading.

Guess you like

Origin blog.csdn.net/qq_28822933/article/details/88367187