Although the structure of Tomcat is very complex, but Tomcat is very modular, find the most core module of Tomcat, the problem can be solved, familiar with the overall architecture of Tomcat is very important for in-depth understanding of Tomcat!

First, Tomcat top-level architecture

First, the top structure of Tomcat is shown as follows:

The topmost container in Tomcat is Server, which represents the entire Server. As shown in the figure above, a Server can contain at least one Service to provide specific services.

A Service consists of two main parts: Connector and Container. As you can see from the figure above, these two components are at the heart of Tomcat, and their roles are as follows:

Tomcat has only one Server. A Server can contain multiple Services, and a Service has only one Container, but can have multiple Connectors. This is because a Service can have multiple connections, such as providing Http and Https links at the same time. It can also provide connections to different ports of the same protocol, as shown in the following diagram:

Multiple connectors and a Container form a Service. With a Service, it can provide services to the outside world. However, a Service needs a living environment. So the entire Tomcat life cycle is controlled by the Server.

In addition, the above inclusion or parent-child relationship can be seen in the server. XML configuration file in the conf directory of Tomcat. The following image shows a complete server. XML configuration file with the comments removed (Tomcat version 8.0).

Detailed configuration file

The document can be viewed on the Tomcat official website: tomcat.apache.org/tomcat-8.0-…

The Sevr label is set to port 8005. Shutdown = “shutdown” means that the shutdown command is listened on port 8005. If the command is received, Tomcat is shutdown. A Server has one Service. You can also configure multiple services. The content on the left of a Service belongs to a Container, and the Connector is under a Service.

2. Summary of Tomcat top-level Architecture

1. Tomcat has only one Server. A Server can have multiple services, and a Service can have multiple Connectors and a Container.

2. Server controls the life and death of Tomcat;

3, Service is provided to the outside world.

4. Connector is used to accept requests and encapsulate requests into Request and Response for specific processing;

5. Container encapsulates and manages servlets and handles request requests.

Server and Service are far away from most developers, and most of the configuration in our development belongs to Connector and Container. So let’s talk about Connector and Container.

3. The delicate relationship between Connector and Container

From the above, we can roughly know that after a Request is sent to Tomcat, it will first go through Service and then be handed to our Connector. The Connector is used to receive the Request and encapsulate the received Request into Request and Response for specific processing. After the Request and Response are wrapped, they are sent to the Container for processing. After the Container processes the Request, the Container sends the Request back to the Connector. Finally, the Connector sends the processing result back to the client through the Socket.

At the bottom of the Connector layer, the Socket is used for connection. Request and Response are wrapped in HTTP protocol, so the Connector needs to implement both TCP/IP and HTTP protocol!

Tomcat needs to receive the request first. To receive the request, we first need to look at the Connector!

In order to help you avoid detour, I asked BAT colleagues to summarize a set of technical videos, covering Java engineering, high performance and distribution, performance tuning, Spring, Netty source code analysis and big data and other knowledge points. If you want to know more details, you can join the Java backend technology group: 819940388, free large Internet Java technology video to share with you. In fact, I also like technology, there are some Ali Daniu in the group, there are also a line of Internet senior HR, recently in the interview of friends or looking for a job can come in and have a look!

4. Analysis of Connector architecture

The Connector accepts and encapsulates a Request into a Request and a Response, and then sends the Request to the Container for processing. After the Container processes the Request, the Connector returns the Request to the client. Therefore, Connector can be divided into four aspects:

First take a look at the structure diagram of Connector (Figure B), as shown below:

Connetr uses a ProtocolHandler to handle requests. Different protocolHandlers represent different types of connections, such as: Http11Protocol uses the normal Socket to connect, Http11NioProtocol uses the NioSocket to connect.

The ProtocolHandler consists of three components: Endpoint, Processor, and Adapter.

1. The Endpoint processes the network connection of the underlying Socket. The Processor encapsulates the Socket received by the Endpoint into a Request, and the Adapter sends the Request to the Container for processing.

The Endpoint is used to implement the TCP/IP protocol, and the Processor is used to implement the HTTP protocol. The Adapter ADAPTS requests to the Servlet container for specific processing.

3. The Endpoint abstraction implements the Acceptor and AsyncTimeout internal classes defined in AbstractEndpoint and a Handler interface. Acceptor is used to listen for requests, AsyncTimeout is used to check the timeout of asynchronous requests, and Handler is used to process the received Socket and internally invoke the Processor for processing.

By now, we should be able to answer questions (1), (2), and (3) easily, but (4) we still don’t know. How does the Container process and return the result to the Connector after processing?

5. Container architecture analysis

Container is used to encapsulate and manage servlets and process Request requests. The Connector contains four sub-containers as shown in Figure C:

The functions of the four sub-containers are as follows:

Find a Tomcat file directory for comparison, as shown below:

The difference between Context and Host is that Context represents an application. In our Tomcat default configuration, each folder directory under Webapps is a Context. The ROOT directory holds the main application and the other directories hold the child applications. The entire Webapps is a Host site.

When you access the Context, you can use the domain name to access the Context. www.ledouit.com, if it is a Host (webapps) under other applications, you can use www.ledouit.com/docs to access, of course, the default specified ROOT application (ROOT) can be set, but the default main application of the Host site is in the ROOT directory.

We know what a Container is, but we still don’t know how the Container is processed and how the processing results are returned to the Connector. Don’t worry! Here’s how the Container works!

How does the Container handle requests

Container processing requests are handled using the Pipeline-Valve Pipeline!

The pipeline-valve responsibility chain mode refers to that in the process of a request processing, there are many processors to process the request in turn, and each processor is responsible for its own processing. After processing, the processed request will be returned, and the next processor will continue processing.

But! The responsibility chain pattern used by Pipeline-ALve is a little different from the normal responsibility chain pattern! There are two main differences:

We know that Containr contains four BaseValve child containers. StandardEngineValve, StandardHostValve, StandardContextValve, StandardWrapperValve.

The processing flow chart of Pipeline is as follows (Figure D) :

1. After receiving the request, the Connector will first call the Pipeline of the top-level container to handle it. The Pipeline of the top-level container here is EnginePipeline (EnginePipeline).

EngineValve1 / EngineValve2 / StandardEngineValve Then Host HostValve1, HostValve2, etc., and StandardHostValve is executed, then Context pipes and Wrapper pipes are called, and StandardWrapperValve is executed.

3, When executed to StandardWrapperValve, FilterChain will be created in StandardWrapperValve and its doFilter method will be called to process the request. The FilterChain contains the Filter and Servlet configured to match the request. The doFilter method calls the doFilter method of all the filters and the service method of the Servlet in turn, and the request is processed!

4. When all the pipeline-Valve processes are complete and the specific request is processed, the result can be returned to the Connector, which will return the result to the client through the Socket.

conclusion

So far, we have had A general understanding of the overall architecture of Tomcat. From Figures A, B, C, and D, we can see the basic elements and functions of each component. We should have an outline in our mind! If you were asked to talk briefly about Tomcat during your interview, would you be able to blurt out the above? Your interviewer will be impressed when you can just blurt it out! You can download information by scanning code into the group. In fact, I also like technology. There are some Ali Daniu in the group, as well as senior HR from the Internet.

blog.csdn.net/qq_42894896…