请求的处理是整个Tomcat的核心。深入了解Tomcat的请求过程,对于我们理解我们的应用项目,对于我们解决问题,对于我们今后开发项目都有深远的影响

如果看过Tomcat原理系列之二:由点到线,请求主干;一定对请求链具体走了哪些组件有了印象。我们再进一步拆解请求链,将链上涉及的类一一道来。

Connector

Connector组件作为Server的一部分,主要用于接收,解析http请求,并将请求封装成requset交给Container容器进行处理.
Connector是如何接受请求的呢?

Connector使用ProtocolHandler(协议处理器)来处理请求的, 不同的ProtocolHandler处理不同模式的请求类型.
例如:
Http11Protocol支持BIO类型的Socket来连接的,
Http11NioProtocol支持NIO类型的NioSocket来连接的
((基于Tomcat8)因为Tomcat高版本默认使用NIO模式,本文以NIO类型的处理来讲)
Tomcat原理系列之三:请求链上的那些类_Tomcat

Http11NioProtocol:

通过Http11NioProtocol的构造方法. 我们可以看出,主要包括两大部分:
Endpoint:端点, 也就是socket的请求的入口.
ConnectionHandler: Connection 处理器

public Http11NioProtocol() {
        endpoint=new NioEndpoint();//端点
        cHandler = new Http11ConnectionHandler(this);//connection处理器
        ((NioEndpoint) endpoint).setHandler(cHandler);
        setSoLinger(Constants.DEFAULT_CONNECTION_LINGER);
        setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT);
        setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY);
    }
1.Endpoint的NIO实现NioEndpoint

NioEndpoint是整个Tomcat的请求的入口.
NioEndponit类中有个几个内部类非常重要,也是请求的必经之地.我们按照请求的走过的顺序一个个的解开看.
Tomcat原理系列之三:请求链上的那些类_Tomcat_02
(不同版本的Tomcat可能有所不同,但是基本框架是类似的.)

1. Acceptor线程:

接收socket线程. Acceptor本身就是一个线程,run()方法里有while循环执行 serverSock.accept()接收线程. 此处有一个点要注意, 虽然是基于NIO的Endpoint但是这里还是阻塞式接收socket连接的方法. 也就是说会阻塞到serverSock.accept();
(1)当获取到SocketChannel对象后, 调用setSocketOptions(socket)方法, 将SocketChannel封装到NioChannel对象中.
(2)并调用Poller的register(Niochannel socket)方法,将NioChannel进一步封装到NioSocketWrapper对象中,
(3)最后在将NioSocketWrapper对象封装到PollerEvent对象压到Pollerevents队列里中去.

这里是一个典型的生产-消费者模式Acceptor 是events queue的生产者, Poller是envets queue的消费者.
(代码有删减,只把重要的提取出来)

@Override
       public void run() {
           while (running) {
                   try {
                       //接收线程(没有连接时阻塞在此处)
                       socket = serverSock.accept();
                   } catch (IOException ioe) {
                   }
                   // Successful accept, reset the error delay
                   errorDelay = 0;

                   // Configure the socket
                   if (running && !paused) {
                       // setSocketOptions()======= 对socket进一步处理
                       if (!setSocketOptions(socket)) {
                           closeSocket(socket);
                       }
                   } else {
                       closeSocket(socket);
                   }
               } catch (Throwable t) {
                   ExceptionUtils.handleThrowable(t);
                   log.error(sm.getString("endpoint.accept.fail"), t);
               }
           }
           state = AcceptorState.ENDED;
       }
2. Poller线程:

主要用于以较少的资源轮询已连接套接字以保持连接,当数据可用时转给worker工作线程.
Poller线程run方法,while循环消费events queue里的PollerEvent事件 . 通过 event()方不断取出PollerEvent对象,然后将PollerEvent对象中的NioSocketWrapper包装类OP_READ事件注册到Poller的Selector选择器去.{此处我们才真正看到NIO的影子}
在注册完OP_READ事件后. 紧接着执行**selector.selectedKeys()**方法将就绪的通道key返回,然后通过selectedKey访问就绪的通道. 取出NioSocketWrapper对象.
接着调用Poller的processKey()方法,根据sk是OPEN_READ事件或者OPEN_WRITE事件,调用NioEndpoint.processSocket()方法将NioSocketWrapper封装到SocketProcessor对象中, 并提交到NioEndpoint.executor线程池(Worker线程池)去执行

@Override
       public void run() {
           // Loop until destroy() is called
           while (true) {

               boolean hasEvents = false;

               try {
                   if (!close) {
                       hasEvents = events();
                       if (wakeupCounter.getAndSet(-1) > 0) {
                           //if we are here, means we have other stuff to do
                           //do a non blocking select
                           keyCount = selector.selectNow();
                       } else {
                           keyCount = selector.select(selectorTimeout);
                       }
                       wakeupCounter.set(0);
                   }
                   if (close) {
                       events();
                       timeout(0, false);
                       try {
                           selector.close();
                       } catch (IOException ioe) {
                           log.error(sm.getString("endpoint.nio.selectorCloseFail"), ioe);
                       }
                       break;
                   }
               } catch (Throwable x) {
                   ExceptionUtils.handleThrowable(x);
                   log.error("",x);
                   continue;
               }
               //either we timed out or we woke up, process events first
               if ( keyCount == 0 ) hasEvents = (hasEvents | events());

               Iterator<SelectionKey> iterator =
                   keyCount > 0 ? selector.selectedKeys().iterator() : null;
               // Walk through the collection of ready keys and dispatch
               // any active event.
               while (iterator != null && iterator.hasNext()) {
                   SelectionKey sk = iterator.next();
                   NioSocketWrapper attachment = (NioSocketWrapper)sk.attachment();
                   // Attachment may be null if another thread has called
                   // cancelledKey()
                   if (attachment == null) {
                       iterator.remove();
                   } else {
                       iterator.remove();
                       processKey(sk, attachment);
                   }
               }//while

               //process timeouts
               timeout(keyCount,hasEvents);
           }//while

           getStopLatch().countDown();
       }
3.Worker线程组:

SocketProcessor处理socket连接的任务提交到worker线程池去执行

 public boolean processSocket(SocketWrapperBase<S> socketWrapper,
           SocketEvent event, boolean dispatch) {
       try {
           if (socketWrapper == null) {
               return false;
           }
           SocketProcessorBase<S> sc = processorCache.pop();
           if (sc == null) {
               sc = createSocketProcessor(socketWrapper, event);
           } else {
               sc.reset(socketWrapper, event);
           }
           Executor executor = getExecutor();
           if (dispatch && executor != null) {
               executor.execute(sc);//将socket处理任务提交到worker线程池
           } else {
               sc.run();
           }
       } catch (RejectedExecutionException ree) {
           getLog().warn(sm.getString("endpoint.executor.fail", socketWrapper) , ree);
           return false;
       } catch (Throwable t) {
           ExceptionUtils.handleThrowable(t);
           // This means we got an OOM or similar creating a thread, or that
           // the pool and its queue are full
           getLog().error(sm.getString("endpoint.process.fail"), t);
           return false;
       }
       return true;
   }
4.SocketProcessor类:

NioEndpoint.SocketProcessor类作为一个任务, run()方法中,将socket的包装类NioEndpoint.NioSocketWrapper交给ProtocolHandler协议处理的ConnectionHandler进行处理

2.ConnectionHandler连接处理器

NioSocketWrapper 此时来到 ConnectionHandler这里.

Http11Processor Http协议处理器(http协议实现)

ConnectionHandler.process()方法,首先得到一个Http11Processor 处理器, 然后会将NioSocketWrapper交给Http11Processor 的进行处理

在这里我们回顾一个经典的问题:HTTP协议的组成部分,或者叫做HTTP协议的报文组成
1.请求行
2.请求头
3.空行
4.消息体
Tomcat原理系列之三:请求链上的那些类_Tomcat_03

Http11Processor.service()方法中就是HTTP协议实现的地方.
Http11Processor会创建一个Http11InputBuffer对象.
HTTP协议如何实现的?

[请求行]Http11InputBuffer.parseRequestLine()用来解析请求行
[请求头]Http11InputBuffer.parseHeaders() 和 Http11InputBuffer.parseHeader()方法 用来解析 请求头
整个消息头就这样被解析
[请求体]get请求可以直接读取uri问号后面的部分. post的请求的参数读取在哪呢?Tomcat采用了延迟解析.,延迟大servlet中去解析. 通过request.getParameterxxx()方法或者getInputStream()这类方法去获取消息体.但是这些方法最终都指向的Request.parseParameters ()

这里多讲一句: parseParameters()会判断请求体的大小. 以前我们以为post携带的请求体没有限制,其实是有限制的.这个大小限制就是Tomcat里配置的maxPostSize参数. 默认是2M

Http11Processor的父类在初始化的时候,会创建Request对象,Response对象.
解析完请求行,请求头后. NioSocketWrapper对象此时变成了Request对象.
然后就开始下一程.
Tomcat原理系列之三:请求链上的那些类_Tomcat_04

CoyoteAdapter:(Request,Response)

Http11Processor.service()方法处理后, 会调用CoyoteAdapter.service(request,repose) 对request,repose做一些预处理后,又开始了下一程connector.getService().getContainer().getPipeline().getFirst().invoke(
request, response);

Container:(Request,Response)

Request,Response 对象层层经过Engine,Host,Context,Wrapper的valve.

Valve

Valve作为一个个基础的阀门,扮演着业务实际执行者的角色.

Tomcat原理系列之三:请求链上的那些类_Tomcat原理_05

1.StandardWrapperValve(最后一个valve)

在StandardWrapperValve.invoke方法中会根据配置的Filter过滤器创建ApplicationFilterChain过滤器链.

ApplicationFilterChain filterChain =
              ApplicationFilterFactory.createFilterChain(request, wrapper, servlet);

然后执行过滤器链的doFilter方法

  filterChain.doFilter(request.getRequest(),
                                  response.getResponse());
2.ApplicationFilterChain

ApplicationFilterChain使用责任了模式,执行过滤器的doFilter方法. 当执行完最后一个Filter方法后.
调用

servlet.service(request, response);

此时到了我们常见的servlet了.终于到了我们的业务代码了

总结:

socket
----->Connector接收socket连接封装成NioSocketWrapper对象
----->ConnectionHandler取得HTTP协议内容,创建Requset,Reponse对象
----->Container携带Requset,Reponse对象层级执行valve的invoke方法,到达最后一个StandardWrapperValve, 创建ApplicationFilterChain过滤器链,
----->ApplicationFilterChain过滤器链执行doFilter方法,执行完成后调用servlet.service()方法
----->业务代码

个人理解有误差,望指出. Tomcat中涉及的细节很多.需要慢慢品味. 但是请求的大体行动路线,就如上描述的那样. 希望读者不断的去看源码挖掘更深的细节. 学习Tomact优秀的设计