Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the architecture modules of Web server Tomcat

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

What are the architecture modules of Web server Tomcat? I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

As the saying goes, standing on the shoulders of giants to see the world, generally learning is the first overview of the whole, and then part by part of one by one broken, and finally the formation of ideas, to understand the specific details, the structure of Tomcat is very complex, but Tomcat is very modular, found the core module of Tomcat, the problem can be easily solved, understand the overall structure of Tomcat for the future in-depth understanding of Tomcat is very important!

1. Tomcat top-level architecture

Let's start with a top-level overall structure diagram of Tomcat (figure A), as follows:

The top-level container in Tomcat is Server, which represents the entire server. As you can see from the figure above, a Server can contain at least one Service to provide services.

Service mainly consists of two parts: Connector and Container. As you can see from the image above, the heart of Tomcat is these two components, and their functions are as follows:

1. Connector is used to handle connection-related matters and provides Socket with Request and Response-related transformations

2. Container is used to encapsulate and manage Servlet, and specifically handle Request requests.

There is only one Server in a Tomcat, a Server can contain multiple Service, a Service can have only one Container, but there can be multiple Connectors, this is because a service can have multiple connections, such as providing both Http and Https links, and can also provide connections to different ports of the same protocol, as shown below (Engine, Host, Context below):

Multiple Connector and a Container form a Service, with Service can provide services to the outside world, but Service also needs a living environment, and someone must be able to give her life and control the power of her life and death, then it must be Server! So the entire life cycle of Tomcat is controlled by Server.

In addition, the above inclusion relationship or parent-child relationship can be seen in the server.xml configuration file in the conf directory of tomcat. The following figure shows a complete server.xml configuration file (Tomcat version 8.0) after the comments are deleted.

Details of the configuration file can be found on Tomcat's official website: http://tomcat.apache.org/tomcat-8.0-doc/index.html

The configuration file above can also be understood more clearly through a structure diagram below:

The port number set by the Server tag is 8005 Server shutdown = "SHUTDOWN", which means listening for the "SHUTDOWN" command on port 8005 and shutting down the Tomcat if it is received. A Server has a Service, of course, it can also be configured, there are multiple Service, the content on the left side of the Service belongs to Container, and under the Service is Connector.

2. Summary of Tomcat top-level architecture:

(1) there is only one Server in a Tomcat, a Server can have multiple Service, a Service can have multiple Connector and a Container

(2) Server is in charge of life and death of the whole Tomcat.

(4) Service provides external services.

(5) Connector is used to accept requests and encapsulate them into Request and Response for specific processing.

(6) Container is used to encapsulate and manage Servlet, and specifically handle request requests.

Knowing the hierarchical architecture of the entire Tomcat and the relationship and role between the various components, for the vast majority of developers, Server and Service are indeed very far away for us, and most of the content in our development belongs to 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 after a request is sent to Tomcat, it will first go through Service and then give it to our Connector,Connector to receive the request and package the received request as Request and Response for specific processing. After Request and Response are encapsulated, it will be processed by Container. After Container has processed the request, it will be returned to Connector. Finally, Connector will return the processing result to the client through Socket, so that the whole request will be processed.

The bottom layer of Connector uses Socket to connect, and Request and Response are encapsulated in accordance with the HTTP protocol, so Connector needs to implement both TCP/IP and HTTP protocols!

Since Tomcat processes the request, we definitely need to receive the request first. To receive the request, we first need to take a look at Connector!

IV. Analysis of Connector architecture

Connector is used to accept requests and encapsulate them into Request and Response, which are then handed over to Container for processing. After Container processing, they are returned to Connector to the client.

Therefore, we can understand Connector in four aspects:

(1) how does Connector accept the request?

(2) how to encapsulate the request into Request and Response?

(3) how are the encapsulated Request and Response handed over to Container for processing?

(4) how to give it to Connector and return it to the client after Container processing?

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

Connector uses ProtocolHandler to process requests, and different ProtocolHandler represents different connection types. For example, Http11Protocol uses ordinary Socket to connect, and Http11NioProtocol uses NioSocket to connect.

ProtocolHandler contains three parts: Endpoint, Processor and Adapter.

(1) Endpoint is used to handle the network connection of the underlying Socket, and Processor is used to encapsulate the Socket received by Endpoint into Request,Adapter to hand over the Request to Container for specific processing.

(2) because Endpoint handles the underlying Socket network connection, Endpoint is used to implement the TCP/IP protocol, while Processor is used to implement the HTTP protocol. Adapter adapts the request to the Servlet container for specific processing.

(3) two inner classes of Acceptor and AsyncTimeout and a Handler interface defined in AbstractEndpoint, the abstract implementation of Endpoint. Acceptor is used to listen for requests, AsyncTimeout is used to check the timeout of asynchronous Request, Handler is used to process the received Socket, and Processor is called internally for processing.

At this point, we should easily answer the question (1) (2) (3), but (4) we still don't know, so let's take a look at how Container is processed and how the finished result is returned to Connector after processing.

V. Analysis of Container architecture

Container is used to encapsulate and manage Servlet, as well as to handle Request requests. There are four sub-containers in Connector. The structure diagram is as follows (figure C):

The functions of the four sub-containers are:

(1) Engine: engine, which is used to manage multiple sites. A Service can have at most one Engine.

(2) Host: represents a site, or virtual host. You can add a site by configuring Host.

(3) Context: represents an application, corresponding to a set of programs normally developed, or a WEB-INF directory and the following web.xml file

(4) Wrapper: each Wrapper is encapsulated with a Servlet

Check the Tomcat file directory below, as shown in the following figure:

The difference between Context and Host is that Context represents an application. Under the default configuration in our Tomcat, every folder directory under webapps is a Context, in which the main application is stored in the ROOT directory, the sub-applications are stored in other directories, and the whole webapps is a Host site.

When we access an application Context, we can access it directly using the domain name if it is under ROOT, for example: www.aistudy.com. If it is another application under Host (webapps), we can use www.aistudy.com/docs to access it. Of course, the specified root application (ROOT) can be set by default, but the default main application under the Host site is under the ROOT directory.

Seeing here, we know what Container is, but we still don't know how Container handles it and how to return the processed result to Connector after processing. No rush! Let's start to explore how Container is handled!

6. How does Container handle requests

Container processing requests are processed using Pipeline-Valve pipes! (Valve means valve)

Pipeline-Valve is the responsibility chain mode, which means that in the process of processing a request, many processors process the request in turn, and each processor is responsible for doing their own corresponding processing. After processing, the processed request is returned, and then the next processing continues to process.

However, the chain of responsibility model used by Pipeline-Valve is somewhat different from the ordinary one! There are two main differences:

(1) each Pipeline has a specific Valve, and it is executed at the end of the pipe. This Valve, called BaseValve,BaseValve, cannot be deleted.

(2) the pipe of the lower container is called in the BaseValve of the pipe of the upper container.

We know that Container contains four child containers, and the corresponding BaseValve of these four sub-containers are: StandardEngineValve, StandardHostValve, StandardContextValve, and StandardWrapperValve.

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

(1) after receiving the request, Connector will first call the Pipeline of the top-level container for processing. Here, the Pipeline of the top-level container is EnginePipeline (the pipeline of Engine).

(2) EngineValve1, EngineValve2 and so on will be executed in the Engine pipeline, and finally StandardEngineValve will be executed, Host pipeline will be called in StandardEngineValve, Host HostValve1, HostValve2 and so on will be executed in turn, StandardHostValve will be executed finally, Context pipeline and Wrapper pipeline will be called in turn, and finally StandardWrapperValve will be executed.

(3) when StandardWrapperValve is executed, a FilterChain is created in StandardWrapperValve and its doFilter method is called to process the request. This FilterChain contains the Filter and Servlet that we configured to match the request, and its doFilter method calls all Filter's doFilter methods and Servlet's service methods in turn, so that the request is processed!

(4) after all the Pipeline-Valve has been executed and the specific request has been processed, the returned result can be handed over to Connector, and Connector returns the result to the client through Socket.

After reading the above, have you mastered the methods of the architecture module of Web server Tomcat? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report