tomcat是一個(gè)web服務(wù)器,運(yùn)行jsp和servlet,使用HTTP與客戶(hù)端(通常是瀏覽器)進(jìn)行通信。
下圖是tomcat的架構(gòu),可以看出:核心內(nèi)容是Connector和Container組件。
一個(gè)Server服務(wù)器中可能有多個(gè)Service,Service可以暫時(shí)理解為“服務(wù)”。Server負(fù)責(zé)管理Service的生命周期,讓外界能夠訪問(wèn)。
Service將Connector和Container聯(lián)系在一起了,類(lèi)似“一紙婚約”,將一對(duì)夫妻結(jié)合,組成一個(gè)家庭。
下面就看一下核心內(nèi)容部分吧
1、Connector在學(xué)Javaweb基礎(chǔ)的時(shí)候,提到過(guò)當(dāng)從瀏覽器傳過(guò)來(lái)http請(qǐng)求之后,web容器會(huì)創(chuàng)建一個(gè)request和response對(duì)象。在Tomcat中,負(fù)責(zé)該創(chuàng)建工作的是Connector組件。
Connector連接器可以有一個(gè)或多個(gè),默認(rèn)情況下會(huì)開(kāi)啟兩個(gè)HTTP協(xié)議和AJP協(xié)議。
2、Container先來(lái)看一下Container的詳細(xì)結(jié)構(gòu)圖:
Container容器有四個(gè)子容器:Engine、Host、Context和Wrapper,他們之間是父子包含關(guān)系。
1)、Engine 2)、Host(虛擬主機(jī)) 3)、Context(所屬的web應(yīng)用上下文) 4)、Wrapper(針對(duì)的是每個(gè)具體的servlet,可以說(shuō)他是最直接和servlet打交道的。)可以看一下Tomcat的server.xml文件,來(lái)深入了解各組件之間的關(guān)系。
<?xml version="1.0" encoding="UTF-8"?> <!--Server--> <Server port="8005" shutdown="SHUTDOWN"> <Service name="Catalina"> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector URIEncoding="GB18030" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/> <!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html --> <Engine defaultHost="localhost" name="Catalina"> <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/> <Context docBase="shop" path="/shop" reloadable="true" source="org.eclipse.jst.jee.server:shop"/></Host> </Engine> </Service> </Server>
設(shè)計(jì)思想 1)職責(zé)鏈模式設(shè)計(jì)Container時(shí)使用到了職責(zé)鏈模式,我們可以明顯的看到,Engine、host、context和Wrapper連接成一個(gè)鏈。當(dāng)請(qǐng)求到來(lái)時(shí),通過(guò)該鏈傳遞,最終到達(dá)處理請(qǐng)求的子容器。大話中的例子是“請(qǐng)假審批”,員工請(qǐng)假流程是一層一層進(jìn)行的,如果經(jīng)理可以審批,就不再繼續(xù)走流程。如果經(jīng)理無(wú)權(quán)力審批,就往上一層總經(jīng)理傳送,總經(jīng)理可以審批就審批,不可以審批,請(qǐng)求就繼續(xù)走下去,直到有人處理或請(qǐng)求被撤銷(xiāo)。
多個(gè)對(duì)象有機(jī)會(huì)處理請(qǐng)求,發(fā)出請(qǐng)求和處理請(qǐng)求兩者之間耦合關(guān)系減弱。對(duì)象連成一條鏈,沿著該鏈傳遞請(qǐng)求,直到有對(duì)象處理它為止。
2)命令模式在設(shè)計(jì)Connector和Container關(guān)系時(shí)用到了命令模式。
Tomcat中可以有多個(gè)Connector,多個(gè)Connector和一個(gè)Container來(lái)構(gòu)成了一個(gè)Service。Connector是請(qǐng)求者,而Container是接受命令并且執(zhí)行的人。類(lèi)似大話設(shè)計(jì)模式中,顧客點(diǎn)餐成烤串的例子。
將請(qǐng)求封裝成一個(gè)對(duì)象,我們可以用參數(shù)封裝各種請(qǐng)求,并且進(jìn)行請(qǐng)求排隊(duì)完成及時(shí)撤銷(xiāo)等功能。
3)觀察者模式這個(gè)模式是比較常用的了,含義也就不用多說(shuō)了,經(jīng)常被用來(lái)實(shí)現(xiàn)“異步處理”。在Tomcat中有很多地方都用到了該模式,比如:Servlet實(shí)力的創(chuàng)建、Session管理、Lifecycle等。
觀察者模式,也叫事件監(jiān)聽(tīng)機(jī)制,也叫發(fā)布-訂閱模式。想到什么?JMS的實(shí)現(xiàn)異步消息處理的Pub-Sub消息模型,還有NIO中將多個(gè)Channel注冊(cè)到selector中監(jiān)聽(tīng)消息等等等等。
大話中的例子是“老板回來(lái),我不知道”。
分享題目:深入理解Tomcat
當(dāng)前鏈接:http://sd-ha.com/article14/cjssge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、關(guān)鍵詞優(yōu)化、網(wǎng)站排名、企業(yè)建站、建站公司、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)