Tomcat請求處理在源碼中的輪轉(zhuǎn)解析
Tomcat基礎(chǔ)架構(gòu)
Tomcat作為Java開發(fā)者接觸過最重要的web容器,在啟動和處理請求過程中做了海量的事情,初級開發(fā)者很少關(guān)心,使用SpringMvc之類上層框架一帶而過,然而這些部分是Java和網(wǎng)絡集大成之作,筆者要帶著大家走一遍一次請求,加深tomcat的認知。最好先調(diào)試好Tomcat源碼
BootStrap和Catalina
BootStrap
BootStrap就是Tomcat的main函數(shù)所在位置,在使用過程中執(zhí)行腳本catlina.sh或者bat文件即可執(zhí)行java命令并調(diào)用BootStrap的main函數(shù)實現(xiàn)tomcat的啟動
public static void main(String args[]) { if (daemon == null) { // Don't set daemon until init() has completed Bootstrap bootstrap = new Bootstrap(); try { bootstrap.init(); } catch (Throwable t) { handleThrowable(t); t.printStackTrace(); return; } daemon = bootstrap; } else { // When running as a service the call to stop will be on a new // thread so make sure the correct class loader is used to // prevent a range of class not found exceptions. Thread.currentThread().setContextClassLoader(daemon.catalinaLoader); } ...... }
Catalina
BootStrap是服務器的入口, 會通過start、stop、stopServer、stopServer等反射調(diào)用Catalina的對應方法
public void stopServer() throws Exception { Method method = catalinaDaemon.getClass().getMethod("stopServer", (Class []) null); method.invoke(catalinaDaemon, (Object []) null); }
Tomcat的核心配置文件server.xml
先來看一下tomcat的容器結(jié)構(gòu)
tomcat容器有多層,Server、service、Engine、Host、Context等,圖中有省略
比如一個名稱為mytomcat的web項目,對應到Container部分,Context名稱就是mytomcat
再看一下Tomcat的配置文件server.xml
<Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase" /> </GlobalNamingResources> <Service name="Catalina"> <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine defaultHost="localhost" name="Catalina"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" /> </Realm> <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt" /> </Host> </Engine> </Service>
在啟動tomcat的時候,catalina的load方法,會讀取server.xml文件的內(nèi)容,通過Digester組件把server.xml解析成Server對象,并且把Server變成Catalina的成員變量。
我們看一下catalina的load方法中digester.parse()方法執(zhí)行后的容器代碼結(jié)構(gòu)
這樣的話,Tomcat的容器的層級結(jié)構(gòu)就建立起來了。
網(wǎng)絡處理部分
Connector
tomcat可以處理多種協(xié)議,統(tǒng)一使用Connector組件來實現(xiàn),在tomcat中一個Service可以對應多個Connector
ProtocolHandler
connector中有個很重要的成員變量protocolHandler, 用來實現(xiàn)多種協(xié)議的解析,
比如http協(xié)議?;趖cp做集群的AJP協(xié)議
Endpoint
AbstractEndpoint是Tcp的處理入口,并且持有Executor;Connector的executor和ProtocolHandler的線程池都是使用這個executor
當有網(wǎng)絡任務到來的時候,tomcat會使用這個線程池來處理socket事件
Acceptor 和 Poller
Acceptor是封裝新連接的任務,Endpoint使用單獨的線程維護 Poller 是java NIO中Selector的封裝,當新連接到來時喚醒Acceptor線程注冊pollerEvent事件來監(jiān)聽連接
Poller的實現(xiàn)是java的Selector
以上就是Tomcat請求處理在源碼中的輪轉(zhuǎn)解析的詳細內(nèi)容,更多關(guān)于Tomcat請求處理的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Nginx + Tomcat實現(xiàn)請求動態(tài)數(shù)據(jù)和請求靜態(tài)資源的分離詳解
這篇文章主要給大家介紹了關(guān)于Nginx + Tomcat實現(xiàn)請求動態(tài)數(shù)據(jù)和請求靜態(tài)資源的分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07centos系統(tǒng)下LNMP環(huán)境一鍵安裝
centos下的LNMP環(huán)境一鍵安裝實現(xiàn)方法,需要的朋友可以參考下。2010-06-06詳解實現(xiàn)Nginx+Tomcat實現(xiàn)單IP、多域名、多站點的訪問
這篇文章主要介紹了詳解實現(xiàn)Nginx+Tomcat實現(xiàn)單IP、多域名、多站點的訪問的相關(guān)資料,這里提供實例幫助到大家實現(xiàn)改功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08nginx部署https網(wǎng)站的實現(xiàn)步驟(親測)
本文詳細介紹了使用Nginx在保持與http服務兼容的情況下部署HTTPS,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-02-02