Tomcat中的鏈接器是如何設計的

上期回顧

上一篇文章《Tomcat在SpringBoot中是如何啓動的》從main方法啓動提及,窺探了SpringBoot是如何啓動Tomcat的,在分析Tomcat中咱們重點提到了,Tomcat主要包括2個組件,鏈接器(Connector)和容器(Container)以及他們的內部結構圖,那麼今天咱們來分析下Tomcat中的鏈接器是怎麼設計的以及它的做用是什麼。java

說明:本文tomcat版本是9.0.21,不建議零基礎讀者閱讀。apache

從鏈接器(Connector)源碼提及

既然是來解析鏈接器(Connector),那麼咱們直接從源碼入手,後面全部源碼我會剔除不重要部分,因此會忽略大部分源碼細節,只關注流程。源碼以下(高能預警,大量代碼):編程

public class Connector extends LifecycleMBeanBase  {
    public Connector() {
        this("org.apache.coyote.http11.Http11NioProtocol");
    }


    public Connector(String protocol) {
        boolean aprConnector = AprLifecycleListener.isAprAvailable() &&
                AprLifecycleListener.getUseAprConnector();

        if ("HTTP/1.1".equals(protocol) || protocol == null) {
            if (aprConnector) {
                protocolHandlerClassName = "org.apache.coyote.http11.Http11AprProtocol";
            } else {
                protocolHandlerClassName = "org.apache.coyote.http11.Http11NioProtocol";
            }
        } else if ("AJP/1.3".equals(protocol)) {
            if (aprConnector) {
                protocolHandlerClassName = "org.apache.coyote.ajp.AjpAprProtocol";
            } else {
                protocolHandlerClassName = "org.apache.coyote.ajp.AjpNioProtocol";
            }
        } else {
            protocolHandlerClassName = protocol;
        }

        // Instantiate protocol handler
        ProtocolHandler p = null;
        try {
            Class<?> clazz = Class.forName(protocolHandlerClassName);
            p = (ProtocolHandler) clazz.getConstructor().newInstance();
        } catch (Exception e) {
            log.error(sm.getString(
                    "coyoteConnector.protocolHandlerInstantiationFailed"), e);
        } finally {
            this.protocolHandler = p;
        }

        // Default for Connector depends on this system property
        setThrowOnFailure(Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE"));
    }
    
  
複製代碼

咱們來看看Connector的構造方法,其實只作了一件事情,就是根據協議設置對應的ProtocolHandler,根據名稱咱們知道,這是協議處理類,因此鏈接器內部的一個重要子模塊就是ProtocolHandlertomcat

關於生命週期

咱們看到Connector繼承了LifecycleMBeanBase,咱們來看看Connector的最終繼承關係:bash

咱們看到最終實現的是Lifecycle接口,咱們看看這個接口是何方神聖。我把其接口的註釋拿下來解釋下網絡

/**
 * Common interface for component life cycle methods.  Catalina components
 * may implement this interface (as well as the appropriate interface(s) for
 * the functionality they support) in order to provide a consistent mechanism
 * to start and stop the component.
 *            start()
 *  -----------------------------
 *  |                           |
 *  | init()                    |
 * NEW -»-- INITIALIZING        |
 * | |           |              |     ------------------«-----------------------
 * | |           |auto          |     |                                        |
 * | |          \|/    start() \|/   \|/     auto          auto         stop() |
 * | |      INITIALIZED --»-- STARTING_PREP --»- STARTING --»- STARTED --»---  |
 * | |         |                                                            |  |
 * | |destroy()|                                                            |  |
 * | --»-----«--    ------------------------«--------------------------------  ^
 * |     |          |                                                          |
 * |     |         \|/          auto                 auto              start() |
 * |     |     STOPPING_PREP ----»---- STOPPING ------»----- STOPPED -----»-----
 * |    \|/                               ^                     |  ^
 * |     |               stop()           |                     |  |
 * |     |       --------------------------                     |  |
 * |     |       |                                              |  |
 * |     |       |    destroy()                       destroy() |  |
 * |     |    FAILED ----»------ DESTROYING ---«-----------------  |
 * |     |                        ^     |                          |
 * |     |     destroy()          |     |auto                      |
 * |     --------»-----------------    \|/                         |
 * |                                 DESTROYED                     |
 * |                                                               |
 * |                            stop()                             |
 * ----»-----------------------------»------------------------------
 *
 * Any state can transition to FAILED.
 *
 * Calling start() while a component is in states STARTING_PREP, STARTING or
 * STARTED has no effect.
 *
 * Calling start() while a component is in state NEW will cause init() to be
 * called immediately after the start() method is entered.
 *
 * Calling stop() while a component is in states STOPPING_PREP, STOPPING or
 * STOPPED has no effect.
 *
 * Calling stop() while a component is in state NEW transitions the component
 * to STOPPED. This is typically encountered when a component fails to start and
 * does not start all its sub-components. When the component is stopped, it will
 * try to stop all sub-components - even those it didn't start. * * Attempting any other transition will throw {@link LifecycleException}. * * </pre> * The {@link LifecycleEvent}s fired during state changes are defined in the * methods that trigger the changed. No {@link LifecycleEvent}s are fired if the * attempted transition is not valid. 複製代碼

這段註釋翻譯就是,這個接口是提供給組件聲明週期管理的,而且提供了聲明週期流轉圖。這裏咱們只須要知道正常流程便可:app

New--->Init()---->Start()---->Stop()--->Destory()dom

從生命週期探索鏈接器

根據上面的生命週期說明,咱們能夠知道鏈接器(Connector)就是按照如此的聲明週期管理的,因此咱們找到了線索,因此鏈接器確定會先初始化而後再啓動。咱們查看其initInternal()方法能夠知道鏈接器初始化作了什麼事情,源碼以下:異步

@Override
    protected void initInternal() throws LifecycleException {

        super.initInternal();

        if (protocolHandler == null) {
            throw new LifecycleException(
                    sm.getString("coyoteConnector.protocolHandlerInstantiationFailed"));
        }

        // Initialize adapter
        adapter = new CoyoteAdapter(this);
        protocolHandler.setAdapter(adapter);
        if (service != null) {
            protocolHandler.setUtilityExecutor(service.getServer().getUtilityExecutor());
        }

        // Make sure parseBodyMethodsSet has a default
        if (null == parseBodyMethodsSet) {
            setParseBodyMethods(getParseBodyMethods());
        }

        if (protocolHandler.isAprRequired() && !AprLifecycleListener.isInstanceCreated()) {
            throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprListener",
                    getProtocolHandlerClassName()));
        }
        if (protocolHandler.isAprRequired() && !AprLifecycleListener.isAprAvailable()) {
            throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprLibrary",
                    getProtocolHandlerClassName()));
        }
        if (AprLifecycleListener.isAprAvailable() && AprLifecycleListener.getUseOpenSSL() &&
                protocolHandler instanceof AbstractHttp11JsseProtocol) {
            AbstractHttp11JsseProtocol<?> jsseProtocolHandler =
                    (AbstractHttp11JsseProtocol<?>) protocolHandler;
            if (jsseProtocolHandler.isSSLEnabled() &&
                    jsseProtocolHandler.getSslImplementationName() == null) {
                // OpenSSL is compatible with the JSSE configuration, so use it if APR is available
                jsseProtocolHandler.setSslImplementationName(OpenSSLImplementation.class.getName());
            }
        }

        try {
            protocolHandler.init();
        } catch (Exception e) {
            throw new LifecycleException(
                    sm.getString("coyoteConnector.protocolHandlerInitializationFailed"), e);
        }
    }
}
複製代碼

根據上面源碼,咱們發現主要是處理protocolHandler並初始化它,同時咱們注意到了protocolHandler 設置了一個適配器,咱們看看這個適配器是作啥的,跟蹤源碼以下:socket

/**
     * The adapter, used to call the connector.
     *
     * @param adapter The adapter to associate
     */
    public void setAdapter(Adapter adapter);
複製代碼

這個註釋已經說的很直白了,這個適配器就是用來調用鏈接器的。咱們再繼續看看protocolHandler的初始化方法

/**
     * Endpoint that provides low-level network I/O - must be matched to the
     * ProtocolHandler implementation (ProtocolHandler using NIO, requires NIO
     * Endpoint etc.).
     */
private final AbstractEndpoint<S,?> endpoint;

public void init() throws Exception {
        if (getLog().isInfoEnabled()) {
            getLog().info(sm.getString("abstractProtocolHandler.init", getName()));
            logPortOffset();
        }

        if (oname == null) {
            // Component not pre-registered so register it
            oname = createObjectName();
            if (oname != null) {
                Registry.getRegistry(null, null).registerComponent(this, oname, null);
            }
        }

        if (this.domain != null) {
            rgOname = new ObjectName(domain + ":type=GlobalRequestProcessor,name=" + getName());
            Registry.getRegistry(null, null).registerComponent(
                    getHandler().getGlobal(), rgOname, null);
        }

        String endpointName = getName();
        endpoint.setName(endpointName.substring(1, endpointName.length()-1));
        endpoint.setDomain(domain);

        endpoint.init();
    }
複製代碼

這裏出現了一個新的對象,endpoint,根據註釋咱們能夠知道endpoint是用來處理網絡IO的,並且必須匹配到指定的子類(好比Nio,就是NioEndPoint處理)。endpoint.init()實際上就是作一些網絡的配置,而後就是初始化完畢了。根據咱們上面的週期管理,咱們知道init()後就是start(),因此咱們查看Connectorstart()源碼:

protected void startInternal() throws LifecycleException {

        // Validate settings before starting
        if (getPortWithOffset() < 0) {
            throw new LifecycleException(sm.getString(
                    "coyoteConnector.invalidPort", Integer.valueOf(getPortWithOffset())));
        }

        setState(LifecycleState.STARTING);

        try {
            protocolHandler.start();
        } catch (Exception e) {
            throw new LifecycleException(
                    sm.getString("coyoteConnector.protocolHandlerStartFailed"), e);
        }
    }
複製代碼

其實就是主要調用protocolHandler.start()方法,繼續跟蹤,爲了方便表述,我會把接下來的代碼統一放在一塊兒說明,代碼以下:

//1.類:AbstractProtocol implements ProtocolHandler,
        MBeanRegistration
 public void start() throws Exception {
     // 省略部分代碼
    endpoint.start();
    }

//2. 類:AbstractEndPoint   
public final void start() throws Exception {
       // 省略部分代碼
        startInternal();
    }
 /**3.類:NioEndPoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
     * Start the NIO endpoint, creating acceptor, poller threads.
     */
    @Override
    public void startInternal() throws Exception {
        //省略部分代碼
       
            // Start poller thread
            poller = new Poller();
            Thread pollerThread = new Thread(poller, getName() + "-ClientPoller");
            pollerThread.setPriority(threadPriority);
            pollerThread.setDaemon(true);
            pollerThread.start();

            startAcceptorThread();
        }
    }
複製代碼

到這裏,其實整個啓動代碼就完成了,咱們看到最後是在NioEndPoint建立了一個Poller,而且啓動它,這裏須要補充說明下,這裏只是以NioEndPoint爲示列,其實Tomcat 主要提供了三種實現,分別是AprEndPoint,NioEndPoint,Nio2EndPoint,這裏表示了tomcat支持的I/O模型:

APR:採用 Apache 可移植運行庫實現,它根據不一樣操做系統,分別用c重寫了大部分IO和系統線程操做模塊,聽說性能要比其餘模式要好(未實測)。

NIO:非阻塞 I/O

NIO.2:異步 I/O

上述代碼主要是開啓兩個線程,一個是Poller,一個是開啓Acceptor,既然是線程,核心的代碼確定是run方法,咱們來查看源碼,代碼以下:

//4.類:Acceptor<U> implements Runnable
 public void run() {
 //省略了部分代碼
                U socket = null;
                    socket = endpoint.serverSocketAccept();
                // Configure the socket
                if (endpoint.isRunning() && !endpoint.isPaused()) {
                    // setSocketOptions() will hand the socket off to
                    // an appropriate processor if successful
                    //核心邏輯
                    if (!endpoint.setSocketOptions(socket)) {
                        endpoint.closeSocket(socket);
                    }
                } else {
                    endpoint.destroySocket(socket);
                }
            
        state = AcceptorState.ENDED;
}
//5.類:NioEndpoint
protected boolean setSocketOptions(SocketChannel socket) {
        // Process the connection
        //省略部分代碼
        try {
            // Disable blocking, polling will be used
            socket.configureBlocking(false);
            Socket sock = socket.socket();
            socketProperties.setProperties(sock);


            NioSocketWrapper socketWrapper = new NioSocketWrapper(channel, this);
            channel.setSocketWrapper(socketWrapper);
            socketWrapper.setReadTimeout(getConnectionTimeout());
            socketWrapper.setWriteTimeout(getConnectionTimeout());
            socketWrapper.setKeepAliveLeft(NioEndpoint.this.getMaxKeepAliveRequests());
            socketWrapper.setSecure(isSSLEnabled());
            //核心邏輯
            poller.register(channel, socketWrapper);
            return true;
  
    }
複製代碼

這裏能夠發現Acceptor主要就是接受socket,而後把它註冊到poller中,咱們繼續看看是如何註冊的。

/**6.類NioEndpoint
         * Registers a newly created socket with the poller.
         *
         * @param socket    The newly created socket
         * @param socketWrapper The socket wrapper
         */
        public void register(final NioChannel socket, final NioSocketWrapper socketWrapper) {
            socketWrapper.interestOps(SelectionKey.OP_READ);//this is what OP_REGISTER turns into.
            PollerEvent r = null;
            if (eventCache != null) {
                r = eventCache.pop();
            }
            if (r == null) {
                r = new PollerEvent(socket, OP_REGISTER);
            } else {
                r.reset(socket, OP_REGISTER);
            }
            addEvent(r);
        }
/** 7.類:PollerEvent implements Runnable
 public void run() {
    //省略部分代碼
    socket.getIOChannel().register(socket.getSocketWrapper().getPoller().getSelector(), SelectionKey.OP_READ, socket.getSocketWrapper());
        }
複製代碼

這裏發現最終就是採用NIO模型把其註冊到通道中。(這裏涉及NIO網絡編程知識,不瞭解的同窗能夠傳送這裏)。那麼註冊完畢後,咱們看看Poller作了什麼事情。

*/        
  /**8.類:NioEndPoint內部類 Poller implements Runnable
  **/  
  @Override
        public void run() {
            // Loop until destroy() is called
            while (true) {
                //省略部分代碼

                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 socketWrapper = (NioSocketWrapper) sk.attachment();
                    // Attachment may be null if another thread has called
                    // cancelledKey()
                    if (socketWrapper == null) {
                        iterator.remove();
                    } else {
                        iterator.remove();
                        //sock處理
                        processKey(sk, socketWrapper);
                    }
                }
        //省略部分代碼
        }    
複製代碼

這個就是經過selector把以前註冊的事件取出來,從而完成了調用。

//9.類: NioEndPoint內部類 Poller  implements Runnable     
protected void processKey(SelectionKey sk, NioSocketWrapper socketWrapper) {
         //省略大部分代碼
           processSocket(socketWrapper, SocketEvent.OPEN_WRITE, true)
    
}
       
//10.類:AbstractEndPoint        
public boolean processSocket(SocketWrapperBase<S> socketWrapper,
           SocketEvent event, boolean dispatch) {
       //省略部分代碼
           Executor executor = getExecutor();
           if (dispatch && executor != null) {
               executor.execute(sc);
           } else {
               sc.run();
           }
      
       return true;
   }  
//11.類:SocketProcessorBase  implements Runnable   
public final void run() {
       synchronized (socketWrapper) {
           // It is possible that processing may be triggered for read and
           // write at the same time. The sync above makes sure that processing
           // does not occur in parallel. The test below ensures that if the
           // first event to be processed results in the socket being closed,
           // the subsequent events are not processed.
           if (socketWrapper.isClosed()) {
               return;
           }
           doRun();
       }
   }
   
//類:12.NioEndPoint   extends AbstractJsseEndpoint<NioChannel,SocketChannel> 
protected void doRun() {
       //省略部分代碼
               if (handshake == 0) {
                   SocketState state = SocketState.OPEN;
                   // Process the request from this socket
                   if (event == null) {
                       state = getHandler().process(socketWrapper, SocketEvent.OPEN_READ);
                   } else {
                       state = getHandler().process(socketWrapper, event);
                   }
                   if (state == SocketState.CLOSED) {
                       poller.cancelledKey(key, socketWrapper);
                   }
               }

       } 
       
複製代碼

Poller調用的run方法或者用Executor線程池去執行run(),最終調用都是各個子EndPoint中的doRun()方法,最終會取一個Handler去處理socketWrapper。繼續看源碼:

//類:13.AbstractProtocol內部類ConnectionHandler implements AbstractEndpoint.Handler<S>
 public SocketState process(SocketWrapperBase<S> wrapper, SocketEvent status) {
            //省略部分代碼
    
            state = processor.process(wrapper, status);
      
            return SocketState.CLOSED;
        }
        
//類:14.AbstractProcessorLight implements Processor 
public SocketState process(SocketWrapperBase<?> socketWrapper, SocketEvent status)
            throws IOException {
            //省略部分代碼
           
            state = service(socketWrapper);
            
        return state;
    }
複製代碼

這部分源碼代表最終調用的process是經過一個Processor接口的實現類來完成的,這裏最終也是會調用到各個子類中,那麼這裏的處理器其實就是處理應用協議,咱們能夠查看AbstractProcessorLight的實現類,分別有AjpProcessorHttp11ProcessorStreamProcessor,分別表明tomcat支持三種應用層協議,分別是:

這裏咱們以經常使用的HTTP1.1爲例,繼續看源碼:

//類:15. Http11Processor extends AbstractProcessor
public SocketState service(SocketWrapperBase<?> socketWrapper)
        throws IOException {
        //省略大部分代碼
             getAdapter().service(request, response);
        //省略大部分代碼   
        } 
//類:16   CoyoteAdapter implements Adapter
public void service(org.apache.coyote.Request req, org.apache.coyote.Response res)
            throws Exception {

        Request request = (Request) req.getNote(ADAPTER_NOTES);
        Response response = (Response) res.getNote(ADAPTER_NOTES);
        postParseSuccess = postParseRequest(req, request, res, response);
            if (postParseSuccess) {
                //check valves if we support async
                request.setAsyncSupported(
                        connector.getService().getContainer().getPipeline().isAsyncSupported());
                // Calling the container
                connector.getService().getContainer().getPipeline().getFirst().invoke(
                        request, response);
            }
            
    }
複製代碼

這裏咱們發現協議處理器最終會調用適配器(CoyoteAdapter),而適配器最終的工做是轉換RequestResponse對象爲HttpServletRequestHttpServletResponse,從而能夠去調用容器,到這裏整個鏈接器的流程和做用咱們就已經分析完了。

小結

那麼咱們來回憶下整個流程,我畫了一張時序圖來講明:

這張圖包含了兩個流程,一個是組件的初始化,一個是調用的流程。鏈接器(Connector)主要初始化了兩個組件,ProtcoHandlerEndPoint,可是咱們從代碼結構發現,他們兩個是父子關係,也就是說ProtcoHandler包含了EndPoint。後面的流程就是各個子組件的調用鏈關係,總結來講就是Acceptor負責接收請求,而後註冊到PollerPoller負責處理請求,而後調用processor處理器來處理,最後把請求轉成符合Servlet規範的requestresponse去調用容器(Container)。

咱們流程梳理清楚了,接下來咱們來結構化的梳理下:

回到鏈接器(Connector)是源碼,咱們發現,上述說的模塊只有ProtocolHandlerAdapter兩個屬於鏈接器中,也就是說,鏈接器只包含了這兩大子模塊,那麼後續的EndPointAcceptorPollerProcessor都是ProtocolHandler的子模塊。 而AcceptorPoller兩個模塊的核心功能都是在EndPoint 中完成的,因此是其子模塊,而Processor比較獨立,因此它和EndPoint是一個級別的子模塊。

咱們用圖來講明下上述的關係:

根據上圖咱們能夠知道,鏈接器主要負責處理鏈接請求,而後經過適配器調用容器。那麼具體流程細化能夠以下:

  • Acceptor監聽網絡請求,獲取請求。
  • Poller獲取到監聽的請求提交線程池進行處理。
  • Processor根據具體的應用協議(HTTP/AJP)來生成Tomcat Request對象。
  • Adapter把Request對象轉換成Servlet標準的Request對象,調用容器。

總結

咱們從鏈接器的源碼,一步一步解析,分析了鏈接器主要包含了兩大模塊,ProtocolHandlerAdapterProtocolHandler主要包含了Endpoint模塊和Processor模塊。Endpoint模塊主要的做用是鏈接的處理,它委託了Acceptor子模塊進行鏈接的監聽和註冊,委託子模塊Poller進行鏈接的處理;而Processor模塊主要是應用協議的處理,最後提交給Adapter進行對象的轉換,以即可以調用容器(Container)。另外咱們也在分析源碼的過程當中補充了一些額外知識點:

  • 當前Tomcat版本支持的IO模型爲:APR模型、NIO模型、NIO.2模型
  • Tomcat支持的協議是AJP和HTTP,其中HTTP又分爲HTTP1.1和HTTP2.0
相關文章
相關標籤/搜索