Request processing flow Tomcat05--

1. Request Process

        Designed so multi-layered container, tomcat is how to determine each request should Wrapper containers which are handled by the servlet up? tomcat with Mapper component to complete this task.

        Function Mapper component is the user's request URL locate a Servlet, its working principle is: Mapper component in saving configuration information for the Web application, in fact, the mapping relationship between container components and access paths, such as domain name Host container configuration, Web application Context container configure the path and the path Wrapper container Servlet mapping, configuration information can be understood as a multi-layered map.

        When a request comes, Mapper component through resolution requests the URL of the domain name and path, and then save the Map to go find their own, will be able to locate to a Servlet. It is noteworthy that, a request to locate a URL only Wrapper container, which is a Servlet.

1.1 requests from application level analytical process

Users enter the URL in your browser: http: // localhost: 8080 / servlet_demo01 / bbs / findAll request process in the tomcat

1. Connector request via connector

In server.xml configuration file, there is a connector port is listening 8080

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

2. Connector in to find the corresponding Service / Engine

There configure a connector in the engine below the engine has a default host address

<Engine name="Catalina" defaultHost="localhost">

the value of the name attribute refers to the Host localhost node.

3. The engine look Host, at a plurality of engine is the Host. Host is the equivalent of a virtual site, we can configure the Host time to configure the address of the host.

 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

4. appBase disposed in the path of the Host, which then will go into the webapps to find named servlet_demo01 (Context) under the application of the request path

5. After the positioning of the particular application, in the path behind / bbs / findAll (Wrapper), to web.xml configuration, target specific Servlet

FIG timing resolution request flow 1.2

1. Acceptor Connector assembly socket listening client is connected and receiving Endpoint Socket

2. Connect to the thread pool Executor process, started in response to a request tasks.

3. processor component reads the message packet, the line resolution request, the request body, a request header, encapsulated Request object

4. Mapper component value based on the URL of the request line and the Host request header matches the value by which the Host container, Context container, Wrapper container processing request

5. coyoteAdaptor component is responsible and associated components Connector Engine container, the generated Request object and the object in response to the transmitted Response Engine vessel, call Pipeline.

Conduit 6. Engine start processing vessel, the pipeline comprising a plurality of value, each responsible for part of the processing logic value, after performing the basic value executes value - StandardEngineValue, is responsible for invoking the container Host Pipeline

7. Host pipeline start processing container, a similar process, the final implementation Connector container Pipeline

8. Connector pipeline start processing container, a similar process, and finally the container Pipeline execution Wrapper

Wrapper conduit 9. The start processing container, similar to the process, and finally performs processing method Servlet container object corresponding Wrapper.

1.3 resolution request flow from the source code level

We need to deploy our project to the source, in order to debug tomcat access to the source code, then how can deploy to tomcat in the source code?

Figure: We only need to copy the project to the next home / webapps in the source package

Then, find BootStrap, in debug mode, restart the tomcat source program.

Note: The Firefox browser, because once sends http request twice, once resource request, once the site is requesting an icon, so that is not conducive to our request tracking process using Google Chrome.

Request flow diagram:

1. 在浏览器输入连接,回车之后,程序首先经过NioEndpoind,接收浏览器的请求

2. 接收到请求之后,进行请求的处理

3. 然后将连接交给线程池Executor处理,开始执行请求响应任务

3.1 获取Processor对象

3.2 然后调用Processor的process方法

4. 将浏览器的请求解析成Request与Response对象

 5. 解析完成之后,调用adaptor对象中的service方法,将request与response对象传递过去

这个是adaptor就是coyoteAdaptor

6. 调用容器并执行

7. 进入StandardEngineValve,拿到Host主机

8. host再获取管道,调用StandardHostValve

9. 进去StandardHostValve,拿到Context

10. context再获取管道,调用StandardContextValve

11. 进去StandardContextValve,拿到Wrapper,并获取管道,调用StandardWrapperValve

12. 然后在StandardWrapperValve中有Servlet的初始化与赋值

 

因为,Wrapper中封装的就是Servlet

13. 接着将Servlet封装到过滤器类中

14. 执行过滤器

15. 执行Servlet的service方法

注意:这里的servlet就是请求的Servlet对象

总结:

        tomcat中的各个组件各司其职,组件之间松耦合,确保了整体架构的可伸缩性和可扩展性,那么在组件内部,如何增强组件的灵活性和扩展性了?在tomcat中,每个Catalina组件采用责任链来完成具体的请求处理。

        在Tomcat中定了Pipeline和Valve两个接口,Pipeline用于构建责任链,后者代表责任链上的每个处理器,Pipeline中维护了一个基础的Valve,它始终位于Pipeline的末端(最后执行),封装了具体的请求处理和输出响应的过程。当然,我们也可以调用addValve()方法,为Pipeline添加其他的Valve,后添加的Valve位于基础的Valve之前,并按照添加顺序执行。Pipeline通过获取首个Valve来启动整个链条的执行。

      tomcat的请求可以解析为两部分,一部分是Connector的请求解析,交给CoyoteAdaptor,将路径映射,在交给后面的Catalina容器去处理。

发布了128 篇原创文章 · 获赞 6 · 访问量 3207

Guess you like

Origin blog.csdn.net/weixin_43318134/article/details/103944695