Tomcat architecture

Reprinted articles https://blog.csdn.net/u010870518/article/details/79006434

Four maps take you through the Tomcat system architecture - allows the interviewer to answer trembling Tomcat series!

Disclaimer: This article is a blogger original article, follow the  CC 4.0 BY-SA  copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u010870518/article/details/79006434

As the saying goes, standing on the shoulders of giants speaking world, is also the first time a general overview about learning a whole, and all of them one by one part of the break, the final form ideas, to understand the specific details, Tomcat structure is very complex, but very modular Tomcat, find the Tomcat core module, the problem and the solution can maneuver, Tomcat understand the overall architecture of the future in-depth understanding of Tomcat is critical!

A, Tomcat top architecture

The first top-level structure (Figure A) a Tomcat, as follows:

Write pictures described here

Tomcat topmost containers are Server, it represents the entire server can be seen from the diagram above, a-Service Server may comprise at least one, for a particular service.

Service consists mainly of two parts: Connector and Container. Tomcat can be seen that these two components of the heart from the figure above, their role is as follows:

1, Connector for processing connection-related things, and provide related conversion Socket Request and the Response;
2, Container for packaging and managing Servlet, and specific processing request Request;

Only one of a Tomcat Server, a Server can contain multiple Service, a Service is only one Container, but can have multiple Connectors, because a service can have multiple connections, such as while providing Http and Https link can also be provided connected to different ports of the same protocol, a schematic diagram is as follows (Engine, Host, Context we will discuss below):

Write pictures described here

More Connector and a Container on the formation of a Service, with Service can provide services, but also a living environment Service, someone must be able to give her life, grasp the power of life and death, it is none other than the non-Server up! So the whole life cycle is controlled by the Tomcat Server.

Furthermore, the inclusion relationship or a parent-child relationship, are available in the conf directory of tomcat server.xmlseen in profile, below is a complete deletion of the comment after the contents of server.xmlthe configuration file (Tomcat version 8.0)

Write pictures described here

Detailed configuration file content to Tomcat can view the official website: http://tomcat.apache.org/tomcat-8.0-doc/index.html

The upper profile, also may be more clearly understood from the lower side of a block diagram:

Write pictures described here

Server port number tag set for 8005, shutdown = "SHUTDOWN", said in 8005 the port monitor "SHUTDOWN" command, if the reception will be closed to the Tomcat. A Server has a Service, of course, can also be configured, there are more than a Service, Service contents belong to the left of the Container, Service Below is a Connector.

Two, Tomcat top architecture Summary:

(1)Tomcat中只有一个Server,一个Server可以有多个Service,一个Service可以有多个Connector和一个Container;
(2) Server掌管着整个Tomcat的生死大权;
(4)Service 是对外提供服务的;
(5)Connector用于接受请求并将请求封装成Request和Response来具体处理;
(6)Container用于封装和管理Servlet,以及具体处理request请求;

知道了整个Tomcat顶层的分层架构和各个组件之间的关系以及作用,对于绝大多数的开发人员来说Server和Service对我们来说确实很远,而我们开发中绝大部分进行配置的内容是属于Connector和Container的,所以接下来介绍一下Connector和Container。

三、Connector和Container的微妙关系

由上述内容我们大致可以知道一个请求发送到Tomcat之后,首先经过Service然后会交给我们的Connector,Connector用于接收请求并将接收的请求封装为Request和Response来具体处理,Request和Response封装完之后再交由Container进行处理,Container处理完请求之后再返回给Connector,最后在由Connector通过Socket将处理的结果返回给客户端,这样整个请求的就处理完了!

Connector最底层使用的是Socket来进行连接的,Request和Response是按照HTTP协议来封装的,所以Connector同时需要实现TCP/IP协议和HTTP协议!

Tomcat既然处理请求,那么肯定需要先接收到这个请求,接收请求这个东西我们首先就需要看一下Connector!

四、Connector架构分析

Connector用于接受请求并将请求封装成Request和Response,然后交给Container进行处理,Container处理完之后在交给Connector返回给客户端。

因此,我们可以把Connector分为四个方面进行理解:

(1)Connector如何接受请求的?
(2)如何将请求封装成Request和Response的?
(3)封装完之后的Request和Response如何交给Container进行处理的?
(4)Container处理完之后如何交给Connector并返回给客户端的?

首先看一下Connector的结构图(图B),如下所示:

Write pictures described here

Connector就是使用ProtocolHandler来处理请求的,不同的ProtocolHandler代表不同的连接类型,比如:Http11Protocol使用的是普通Socket来连接的,Http11NioProtocol使用的是NioSocket来连接的。

其中ProtocolHandler由包含了三个部件:Endpoint、Processor、Adapter。

(1)Endpoint用来处理底层Socket的网络连接,Processor用于将Endpoint接收到的Socket封装成Request,Adapter用于将Request交给Container进行具体的处理。

(2)Endpoint由于是处理底层的Socket网络连接,因此Endpoint是用来实现TCP/IP协议的,而Processor用来实现HTTP协议的,Adapter将请求适配到Servlet容器进行具体的处理。

(3)Endpoint的抽象实现AbstractEndpoint里面定义的Acceptor和AsyncTimeout两个内部类和一个Handler接口。Acceptor用于监听请求,AsyncTimeout用于检查异步Request的超时,Handler用于处理接收到的Socket,在内部调用Processor进行处理。

至此,我们应该很轻松的回答(1)(2)(3)的问题了,但是(4)还是不知道,那么我们就来看一下Container是如何进行处理的以及处理完之后是如何将处理完的结果返回给Connector的?

五、Container架构分析

Container用于封装和管理Servlet,以及具体处理Request请求,在Connector内部包含了4个子容器,结构图如下(图C):

Write pictures described here

4个子容器的作用分别是:

(1)Engine:引擎,用来管理多个站点,一个Service最多只能有一个Engine;
(2)Host:代表一个站点,也可以叫虚拟主机,通过配置Host就可以添加站点;
(3)Context:代表一个应用程序,对应着平时开发的一套程序,或者一个WEB-INF目录以及下面的web.xml文件;
(4)Wrapper:每一Wrapper封装着一个Servlet;

下面找一个Tomcat的文件目录对照一下,如下图所示:

Write pictures described here

Context和Host的区别是Context表示一个应用,我们的Tomcat中默认的配置下webapps下的每一个文件夹目录都是一个Context,其中ROOT目录中存放着主应用,其他目录存放着子应用,而整个webapps就是一个Host站点。

我们访问应用Context的时候,如果是ROOT下的则直接使用域名就可以访问,例如:www.ledouit.com,如果是Host(webapps)下的其他应用,则可以使用www.ledouit.com/docs进行访问,当然默认指定的根应用(ROOT)是可以进行设定的,只不过Host站点下默认的主营用是ROOT目录下的。

看到这里我们知道Container是什么,但是还是不知道Container是如何进行处理的以及处理完之后是如何将处理完的结果返回给Connector的?别急!下边就开始探讨一下Container是如何进行处理的!

六、Container如何处理请求的

Container处理请求是使用Pipeline-Valve管道来处理的!(Valve是阀门之意)

Pipeline-Valve是责任链模式,责任链模式是指在一个请求处理的过程中有很多处理者依次对请求进行处理,每个处理者负责做自己相应的处理,处理完之后将处理后的请求返回,再让下一个处理着继续处理。

Write pictures described here

但是!Pipeline-Valve使用的责任链模式和普通的责任链模式有些不同!区别主要有以下两点:

(1)每个Pipeline都有特定的Valve,而且是在管道的最后一个执行,这个Valve叫做BaseValve,BaseValve是不可删除的;

(2)在上层容器的管道的BaseValve中会调用下层容器的管道。

我们知道Container包含四个子容器,而这四个子容器对应的BaseValve分别在:StandardEngineValve、StandardHostValve、StandardContextValve、StandardWrapperValve。

Pipeline的处理流程图如下(图D):

Write pictures described here

(1) Connector after receiving the first call request will be Pipeline topmost container for processing, where the topmost container Pipeline is EnginePipeline (Engine conduit);

(2) the order will be executed Engine pipeline EngineValve1, EngineValve2 etc., and finally performs StandardEngineValve, in StandardEngineValve will call the Host pipeline, and then followed by the implementation of HostValve1 Host, HostValve2, and finally in the implementation of StandardHostValve, then turn call Context Wrapper pipes and pipelines, final implementation to StandardWrapperValve.

(3) when performing the StandardWrapperValve, it will create in StandardWrapperValve in FilterChain, and call its doFilter method to process the request, the FilterChain contains the request that matches the Filter and Servlet our configuration, it doFilter method will call all the Filter doFilter method and the method of the Servlet service, so that requests have been addressed!

(4) When all of the Pipeline-Valve have been executed, and the process is finished particular request, this time may be the results returned to the Connector, Connector returns the result to the client through the Socket embodiment.

to sum up

So far, we already have a general understanding of the overall architecture of Tomcat, Figure A, B, C, D can be seen the basic elements and the role of each component. We should have in mind a rough outline of! If you do the interview, so you simply talk about Tomcat, above what you can blurt it? When you are able to blurt out, the interviewer will certainly impress you in!

Guess you like

Origin www.cnblogs.com/x-x-736880382/p/11926958.html