Four pictures show you the Tomcat system architecture--let the interviewer tremble at the Tomcat answer series! __tomcat

Source: Internet
Author: User
Tags wrapper

As the saying goes, standing on the shoulders of giants to see the world, general learning is also the first overview of the overall, and then one by one part of the break, and finally formed a train of thought to understand the specific details, Tomcat's structure is very complex, but Tomcat very modular, found the most core Tomcat module, The problem can be edged and understood, and the overall architecture of Tomcat is essential for further insight into Tomcat. first, Tomcat top level architecture

First the top-level structure of Tomcat (figure A), as follows:

The topmost container in Tomcat is the server, which represents the entire servers, as you can see from the image above that a server can contain at least one service for specific services.

Service consists mainly of two parts: Connector and container. As you can see from the image above, Tomcat's heart is the two components, and their role is as follows:

1, connector used to deal with connection-related things, and provide socket and request and response related to the transformation;
2, container for the encapsulation and management of the servlet, as well as the specific processing request requests;

One Tomcat has only one server, one server can contain multiple services, one service has only one container, but it can have multiple connectors, because one can have multiple connections, If you provide both HTTP and HTTPS links, you can also provide a connection to different ports of the same protocol, as shown below (Engine, Host, context below):

Multiple Connector and a Container on the formation of a service, with the service can provide external services, but the service also needs a living environment, someone must be able to give her life, master the power of death, it is not the Server. So the entire Tomcat lifecycle is controlled by the Server.

In addition, the above inclusion relationship, or parent-child relationship, can be seen in the Server.xml configuration file in Tomcat's Conf directory, and the following figure is a complete server.xml configuration file (Tomcat version 8.0) After the annotation content has been deleted.

Detailed profile file contents can be viewed at Tomcat's website: http://tomcat.apache.org/tomcat-8.0-doc/index.html

The configuration file above can be more clearly understood by a chart below:

The port number set by the Server tab is 8005,shutdown= "shutdown", which means that the "shutdown" command is listening on Port 8005, and Tomcat is turned off if it is received. A server has a service, of course, can also be configured, a service has more than the left side of the content belong to container, the service below is connector. second, the Tomcat top-level architecture summary:

(1) Tomcat has only one server, one server can have multiple service, a service can have multiple connector and a container;
(2) Server governs the life and death of Tomcat;
(4) Service is the external provision of services;
(5) connector is used to accept requests and encapsulate requests into request and response for specific processing;
(6) container is used to encapsulate and manage the servlet, as well as to handle request requests specifically;

Knowing the relationship between the layered architecture of the entire Tomcat top-level and the components and the role of each component, the server and service are really far away for the vast majority of developers, and the vast majority of the configuration in our development is connector and container , so let's introduce connector and container. Iii. The delicate relationship between connector and container

From the above we can roughly know that a request is sent to Tomcat, first through the service and then to our connector,connector to receive the request and encapsulate the received request as requested and response for specific processing, Request and response after the encapsulation and then to container for processing, container processing the request and then return to the connector, and finally in the connector through the socket processing results returned to the client, So the whole request was disposed of.

Connector at the bottom of the socket used to connect, request and response is in accordance with the HTTP protocol to encapsulate, so connector also need to implement the TCP/IP protocol and HTTP protocol.

Since Tomcat is processing the request, it must first receive the request and receive the request. We need to look at connector first. Iv. Connector Architecture Analysis

The connector is used to accept the request and encapsulate the request into request and response, then hand it over to container for processing, container after processing, and returning it to the client at connector.

Therefore, we can divide the connector into four aspects to understand:

(1) connector How to accept the request.
(2) How to encapsulate the request into requests and response.
(3) The package after the request and response how to hand over to container for processing.
(4) How to container after processing to connector and return to the client.

First look at the connector chart (Figure B), as follows:

Connector is the use of Protocolhandler to handle requests, different protocolhandler represent different types of connections, such as: Http11protocol using a common socket to connect, The Http11nioprotocol uses Niosocket to connect.

The Protocolhandler consists of three parts: Endpoint, Processor, Adapter.

(1) endpoint is used to handle the network connection of the underlying socket, processor is used to encapsulate the socket received by endpoint into Request,adapter to give the request to container for specific processing.

(2) endpoint because it is the processing of the underlying socket network connection, so endpoint is used to implement the TCP/IP protocol, and processor to implement the HTTP protocol, adapter will request appropriate to the Servlet container for specific processing.

(3) The endpoint abstract implements the acceptor and asynctimeout two internal classes and a handler interface defined within the Abstractendpoint. Acceptor is used to listen for requests, asynctimeout to check the timeout of the asynchronous request, handler to handle the received socket, and internally invoke processor for processing.

At this point, we should be very easy to answer (1) (2) (3) The question, but (4) still don't know, then we will look at how the container is processed and after the processing is done to return the processing results to connector. v. Container architecture Analysis

Container is used to encapsulate and manage the servlet, as well as to specifically process request requests, containing 4 child containers within the connector, as shown below (Figure C):

The function of the 4 sub containers is respectively:

(1) Engine: engine, used to manage multiple sites, a service can only have a Engine;
(2) Host: Represents a site, can also be called a virtual host, through the configuration host can add sites;
(3) Context: Represents an application, corresponding to the usual development of a set of procedures, or a Web-inf directory and the following web.xml file;
(4) Wrapper: Each wrapper encapsulates a servlet;

Let's look at a Tomcat file directory, as shown in the following figure:

The difference between context and host is that it represents an application, and each folder directory under the default configuration of our Tomcat is a WebApps, where the main application is stored in the root directory, and the other directory holds the child application. And the entire WebApps is a host site.

When we visit the application context, we can access it directly using the domain name if it is root, for example: www.ledouit.com, if it is another application under host (WebApps), you can access it using Www.ledouit.com/docs , of course, the default specified root application (root) can be set, except that the default main use under the host site is the root directory.

See here we know what container is, but still don't know how container is handled and how to return the processed results to connector after processing. Don't worry. The bottom is to begin to discuss how container is handled. vi. container How to handle a request

Container processing requests are handled using the Pipeline-valve pipeline. (Valve is the meaning of the valve)

Pipeline-valve is the responsibility chain mode, the responsibility chain mode is that in a request processing process, there are many handlers in turn to the request processing, each processor is responsible for doing their own corresponding processing, processing after the processing of the request to return, and then let the next processing to continue processing.

But. The responsibility chain model used by Pipeline-valve is somewhat different from the common pattern of responsibility chain. The difference mainly has the following two points:

(1) Each pipeline has a specific valve, and is the last execution of the pipeline, this valve is called basevalve,basevalve is not deleted;

(2) The pipeline of the lower container is invoked in the basevalve of the pipeline of the upper container.

We know that the container contains four sub containers, and that the basevalve of these four sub containers correspond to the following: Standardenginevalve, Standardhostvalve, Standardcontextvalve, Standardwrappervalve.

The pipeline Process flow chart is as follows (Figure D):

(1) connector after receiving the request will first call the topmost container pipeline to deal with, here the topmost container pipeline is Enginepipeline (engine pipeline);

(2) in the engine pipeline, the EngineValve1, EngineValve2, and so on will be executed, and the standardenginevalve will be executed, and the host pipeline will be invoked in Standardenginevalve, Then executes the host's HostValve1, HostValve2, and so on, and finally executes the standardhostvalve, and then calls the context pipes and wrapper pipes in turn, Final execution to Standardwrappervalve.

(3) When executing to Standardwrappervalve, the Filterchain is created in Standardwrappervalve and its Dofilter method is invoked to process the request. This filterchain contains the filter and servlet that we configure to match the request, and its Dofilter method calls all the filter's Dofilter methods and the Servlet's service method in turn, so the request is processed.

(4) When all the Pipeline-valve are finished, and the specific request is processed, this time can return the results to connector, connector in the way through the socket to return the results to the client. Summary

At this point, we have a general understanding of Tomcat's overall architecture, from figures A, B, C, and D to see the basic elements and functions of each component. We should have a rough outline in our mind. If you're interviewing for a brief chat about Tomcat, can you blurt it out? When you can blurt out, the interviewer is sure to impress you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.