您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關連接器在Tomcat中是怎么設計的,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
從連接器(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,根據名稱我們知道,這是協議處理類,所以連接器內部的一個重要子模塊就是ProtocolHandler。
關于生命周期
我們看到Connector繼承了LifecycleMBeanBase,我們來看看Connector的最終繼承關系:
我們看到最終實現的是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.
這段注釋翻譯就是,這個接口是提供給組件聲明周期管理的,并且提供了聲明周期流轉圖。這里我們只需要知道正常流程即可:
New--->Init()---->Start()---->Stop()--->Destory()
從生命周期探索連接器
根據上面的生命周期說明,我們可以知道連接器(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 設置了一個適配器,我們看看這個適配器是做啥的,跟蹤源碼如下:
/** * 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(),所以我們查看Connector的start()源碼:
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的實現類,分別有AjpProcessor、Http11Processor、StreamProcessor,分別代表tomcat支持三種應用層協議,分別是:
AJP協議
HTTP.1協議
HTTP2.0協議
這里我們以常用的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),而適配器最終的工作是轉換Request和Response對象為HttpServletRequest和HttpServletResponse,從而可以去調用容器,到這里整個連接器的流程和作用我們就已經分析完了。
小結
那么我們來回憶下整個流程,我畫了一張時序圖來說明:
這張圖包含了兩個流程,一個是組件的初始化,一個是調用的流程。連接器(Connector)主要初始化了兩個組件,ProtcoHandler和EndPoint,但是我們從代碼結構發現,他們兩個是父子關系,也就是說ProtcoHandler包含了EndPoint。后面的流程就是各個子組件的調用鏈關系,總結來說就是Acceptor負責接收請求,然后注冊到Poller,Poller負責處理請求,然后調用processor處理器來處理,最后把請求轉成符合Servlet規范的request和response去調用容器(Container)。點擊免費“領取Java架構資料”
我們流程梳理清楚了,接下來我們來結構化的梳理下:
回到連接器(Connector)是源碼,我們發現,上述說的模塊只有ProtocolHandler和Adapter兩個屬于連接器中,也就是說,連接器只包含了這兩大子模塊,那么后續的EndPoint、Acceptor、Poller、Processor都是ProtocolHandler的子模塊。 而Acceptor和Poller兩個模塊的核心功能都是在EndPoint 中完成的,所以是其子模塊,而Processor比較獨立,所以它和EndPoint是一個級別的子模塊。
我們用圖來說明下上述的關系:
根據上圖我們可以知道,連接器主要負責處理連接請求,然后通過適配器調用容器。那么具體流程細化可以如下:
Acceptor監聽網絡請求,獲取請求。
Poller獲取到監聽的請求提交線程池進行處理。
Processor根據具體的應用協議(HTTP/AJP)來生成Tomcat Request對象。
Adapter把Request對象轉換成Servlet標準的Request對象,調用容器。
關于連接器在Tomcat中是怎么設計的就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。