Tomcat source code analysis (B) ----- Tomcat overall architecture and components

Foreword

Tomcat formerly known as Catalina, but Catalina is a lightweight Servlet container. In the United States, catalina is a beautiful island. So the moral of the Tomcat Tomcat may be trying to design a beautiful and elegant lightweight web server. Tomcat 4.x version from the start in addition to being supported by Servlet container outside, adding a lot of extra features, such as: jsp, el, naming, etc., so that not only the Tomcat Catalina.

Since the first Tomcat is a Servlet container, we should be more concerned about the Servlet.

So, what is the Servlet it?

In the beginning of the rise of the Internet, when the Sun's (later acquired by Oracle) already saw this opportunity, then he devised to support Applet for Web applications. But the fact is not expected so well, Sun sad reminder to find Applet did not bring much impact to the industry. After reflection, Sun wanted to since there have been opportunities, market prospects are very good, we can not give up vain ah, how to do it? So he put out energy to engage in a set of norms, then Servlet born!

The so-called Servlet, in fact, Sun to make Java to achieve a dynamic interactive web page, to enter the field of Web programming and the development of a set of standards!

A Servlet mainly to do the following three things:

  1. Request object is created and populated, comprising: URI, parameters, Method, a request header, the request-information and the like
  2. Create a Response object
  3. Execute business logic, and outputs the result to the client through the output stream Response

Servlet no main method, so if you want to perform, you need a 容器to perform inside, the container is to support Servlet functions exist, Tomcat is actually a container implementation Servlet.

Overall Chart

From the graph we see that the core of two components - Connector (Connector) and the container (Container) play a 心脏role, they are essential! 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):

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.

 Further, the above-described relationship or comprising a parent-child relationship, can be in the tomcat conf directory server.xmlseen profile

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

 

Let us one by one to analyze the functions of the various components:

  1. ServerIt means that the server provides an elegant way to start and stop the whole system, does not have to start and stop individual connectors and the container
  2. ServiceExpress service, Serveryou can run multiple services. For example, you can run inside a Tomcat service orders, payment services, user services, etc.
  3. Each Servicemay contain 多个Connectorand 一个Container. Because each service allows simultaneous support of multiple protocols, but the final execution of the Servlet each protocol is the same
  4. ConnectorRepresents a connector, such as a service can support AJP protocol, the Http Https protocol and protocol, each protocol can be used to support a connector
  5. ContainerRepresents a container, the container can be seen Servlet
    • Engine - Engine
    • Host - Host
    • Context - Context
    • Wrapper - Wrappers
  6. Service服务之下还有各种支撑组件,下面简单罗列一下这些组件
    • Manager -- 管理器,用于管理会话Session
    • Logger -- 日志器,用于管理日志
    • Loader -- 加载器,和类加载有关,只会开放给Context所使用
    • Pipeline -- 管道组件,配合Valve实现过滤器功能
    • Valve -- 阀门组件,配合Pipeline实现过滤器功能
    • Realm -- 认证授权组件

除了连接器和容器,管道组件和阀门组件也很关键,我们通过一张图来看看这两个组件

Connector和Container的微妙关系

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

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

Connector架构分析

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

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

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

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

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进行处理。

Container如何处理请求的

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

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

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

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

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

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

Pipeline的处理流程图如下:

(1)Connector在接收到请求后会首先调用最顶层容器的Pipeline来处理,这里的最顶层容器的Pipeline就是EnginePipeline(Engine的管道);

(2)在Engine的管道中依次会执行EngineValve1、EngineValve2等等,最后会执行StandardEngineValve,在StandardEngineValve中会调用Host管道,然后再依次执行Host的HostValve1、HostValve2等,最后在执行StandardHostValve,然后再依次调用Context的管道和Wrapper的管道,最后执行到StandardWrapperValve。

(3)当执行到StandardWrapperValve的时候,会在StandardWrapperValve中创建FilterChain,并调用其doFilter方法来处理请求,这个FilterChain包含着我们配置的与请求相匹配的Filter和Servlet,其doFilter方法会依次调用所有的Filter的doFilter方法和Servlet的service方法,这样请求就得到了处理!

(4)当所有的Pipeline-Valve都执行完之后,并且处理完了具体的请求,这个时候就可以将返回的结果交给Connector了,Connector在通过Socket的方式将结果返回给客户端。

总结

好了,我们已经从整体上看到了Tomcat的结构,但是对于每个组件我们并没有详细分析。后续章节我们会从几个方面来学习Tomcat:

  1. 逐一分析各个组件
  2. 通过断点的方式来跟踪Tomcat代码中的一次完整请求

 

 

Guess you like

Origin www.cnblogs.com/java-chen-hao/p/11316795.html