tomcat學習筆記(一)系統架構和原理
- Tomcat
- 瀏覽器訪問服務器的流程
- tomcat請求處理大致程序
- tomcat系統總體架構
- coyote 連接器組件
- coyote內部組件和作業流程
- servlet容器Catalina
- tomcat核心組態檔server.xml
Tomcat
b/s(瀏覽器/服務器模式) 瀏覽器是客戶端(發送http請求)
瀏覽器訪問服務器的流程
http請求只是定義了資料的組織格式(通信格式),是一個應用層協議,資料傳輸依靠的是TCP/IP協議,
1、用戶輸入url地址,然后出發訪問,或者是搜索框輸入關鍵詞進行搜索,
2、瀏覽器發起TCP連接請求(三次握手)
3、服務器接受請求建立連接
4、瀏覽器生成HTTP格式的資料包
5、通過TPC/IP發送請求資料包
6、服務器決議HTTP格式資料包
7、服務器執行請求
8、服務器生成HTTP格式的資料包
9、服務器通過TPC/IP發送回應資料包給瀏覽器
10、瀏覽器決議HTTP格式的資料包
11、瀏覽器呈現靜態資料給用戶,靜態資料(html/js/圖片)
tomcat請求處理大致程序
tomcat是一個http服務器,能夠接受并且處理http請求,
如果tomcat直接呼叫業務處理類完成業務處理的話,那么tomcat便和我們的java類耦合在一起了,tomcat顯然不是這么做的,
HTTP服務器接受到請求之后,吧請求交給Servlet容器來處理,servlet容器通過servlet介面呼叫業務類,servlet介面和servlet容器一套內容被稱作servlet規范
tomcat既按照servlet規范的要求去實作了servlet容器規范,同時也是一個HTTP服務器,
1、HTTP服務器會把請求資訊使用ServletRequest物件封裝起來
2、進一步呼叫servlet容器中的某個具體的servlet
3、根據url和servlet的映射關系,找到相應的servlet
4、如果servlet還沒有被加載,就用反射機制創建這個servlet,并呼叫servlet的init方法來完成初始化
5、接著呼叫這個具體的servlet的servcie方法來處理請求,請求處理結果使用servletResponse物件封裝
6、把servletResponse物件回傳給HTTP服務器,HTTP服務器會把相應發送給客戶端
tomcat系統總體架構
通過上述,tomcat有兩個非常重要的功能需要完成
1、要和客戶端瀏覽器進行互動進行socket通信,將位元組流和request/response物件進行轉換,
2、servlet處理業務邏輯
tomcat有兩個重要組件,一個是Connector連接器組件,一個是Container容器組件,
連接器:和客戶互動(socket通信),對應角色是http服務器功能
容器組件:處理業務邏輯(對應角色:servlet容器)
coyote 連接器組件
1、coyote封裝了底層的網路通信(socket請求及相應處理)
2、coyote是catalina容器(容器組件)與具體的請求協議及io操作方式的完全解耦
3、coyote將socket輸入轉換封裝為Request物件,進一步封裝后交友catalina容器進行處理,處理請求完成后,catalina通過coyote提供的response物件將結果寫入輸出流
4、coyote負責的具體協議(應用層)和io相關內容
應用層默認協議是HTTP/1.1,傳輸層默認的io模型是NIO
coyote內部組件和作業流程
EndPoint:通信端點,即終端,通信監聽的介面,是具體socket接受和發送處理器,是對傳輸層的抽象,因此EndPoint用來實作TCP/IP協議的
Processor 是協議處理介面,應用協議決議處理,processor接受來自EndPoint的socket,讀取位元組流決議成tomcat Request和Response物件,并通過Adapter將其提交到容器處理,processor是應用層協議的抽象,
Adapter:由于協議不同,客戶端發過來的請求資訊也不盡相同,tomcat定義了自己的request類,但是這個物件不是標準的servletrequest,不能用tomcatrequest作為引數來呼叫容器,引入coyoteAdapter,這是配接器模式的經典應用,連接器嗲用coyoteAdapter的service方法,傳入的是tomcat request物件,coyoteAdapter將tomcat request物件轉換為servletRequest,在呼叫容器,
servlet容器Catalina
tomcat是一個由一系列可配置(conf/server.xml)的舉薦構成的web容器,而catalina是tomcat的servlet容器
從另一個角度來說,tomcat本質上就是一個servlet容器,因為catalina才是tomcat的核心,其他模塊都是為Catalina提供支撐的,
tomcat往往我們認為是Catalina的一個實體,
Catalina實體通過加載server.xml完成其他實體的創建,創建并管理一個server,server創建并管理多個服務,每個服務又可以有多個connector和一個container,
**catalina:**負責決議tomcat組態檔,一次來創建服務器server組件并進行管理
**server:**服務器標識整個catalina servlet容器以及其他組件,負責組裝啟動servlet引擎,tomcat連接器,server通過實作lifecycle介面,提供了一種優雅的啟動和關閉整個系統的方式
**service:**服務是server內部的組件,一個server包含多個service,他講若干個connector組件系結到了一個contaner
**container:**容器,負責處理用戶的servlet請求,并回傳物件給web用戶的模塊
tomcat核心組態檔server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<!-- 創建server實體,子標簽有listner和service、GlobalNamingResources -->
<!-- 監聽8005埠執行關閉 -->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<!-- 只有一個service,子標簽:listener,excutor,connector,engine -->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- 默認情況下,Service 并未添加共享執行緒池配置, 如果我們想添加?個執行緒池, 可以在 -->
<!-- <Service> 下添加如下配置: -->
<!-- name:執行緒池名稱,?于 Connector中指定 -->
<!-- namePrefix:所創建的每個執行緒的名稱前綴,?個單獨的執行緒名稱為 -->
<!-- namePrefix+threadNumber -->
<!-- maxThreads:池中最?執行緒數 -->
<!-- minSpareThreads:活躍執行緒數,也就是核?池執行緒數,這些執行緒不會被銷毀,會?直存在 -->
<!-- maxIdleTime:執行緒空閑時間,超過該時間后,空閑執行緒會被銷毀,默認值為6000(1分鐘),單位 -->
<!-- 毫秒 -->
<!-- maxQueueSize:在被執?前最?執行緒排隊數?,默認為Int的最?值,也就是?義的?限,除?特 -->
<!-- 殊情況,這個值 不需要更改,否則會有請求不會被處理的情況發? -->
<!-- prestartminSpareThreads:啟動執行緒池時是否啟動 minSpareThreads部分執行緒,默認值為 -->
<!-- false,即不啟動 -->
<!-- threadPriority:執行緒池中執行緒優先級,默認值為5,值從1到10 -->
<!-- className:執行緒池實作類,未指定情況下,默認實作類為 -->
<!-- org.apache.catalina.core.StandardThreadExecutor,如果想使??定義執行緒池?先需要實作 -->
<!-- org.apache.catalina.Executor接? -->
<Executor name="commonThreadPool"
namePrefix="thread-exec-"
maxThreads="200"
minSpareThreads="100"
maxIdleTime="60000"
maxQueueSize="Integer.MAX_VALUE"
prestartminSpareThreads="false"
threadPriority="5"
className="org.apache.catalina.core.StandardThreadExecutor"/>
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<!-- 可以配置多個connector,埠8080,請求協議,連接超時,重定向埠 -->
<!--
port:
端?號,Connector ?于創建服務端Socket 并進?監聽, 以等待客戶端請求鏈接,如果該屬性設定
為0, Tomcat將會隨機選擇?個可?的端?號給當前Connector 使?
protocol:
當前Connector ?持的訪問協議, 默認為 HTTP/1.1 , 并采??動切換機制選擇?個基于 JAVA
NIO 的聯結器或者基于本地APR的聯結器(根據本地是否含有Tomcat的本地庫判定)
connectionTimeOut:
Connector 接收鏈接后的等待超時時間, 單位為 毫秒, -1 表示不超時,
redirectPort:
當前Connector 不?持SSL請求, 接收到了?個請求, 并且也符合security-constraint 約束,
需要SSL傳輸,Catalina?動將請求重定向到指定的端?,
executor:
指定共享執行緒池的名稱, 也可以通過maxThreads、minSpareThreads 等屬性配置內部執行緒池,
URIEncoding:
?于指定編碼URI的字符編碼, Tomcat8.x版本默認的編碼為 UTF-8 , Tomcat7.x版本默認為ISO-
8859-1
-->
<!--org.apache.coyote.http11.Http11NioProtocol , ?阻塞式 Java NIO 聯結器-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the
AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
This connector uses the APR/native implementation which always uses
OpenSSL for TLS.
Either JSSE or OpenSSL style configuration may be used. OpenSSL style
configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!-- 處理ajp協議 -->
<!-- <Connector protocol="AJP/1.3" -->
<!-- address="::1" -->
<!-- port="8009" -->
<!-- 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 -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<!--
name: 用于指定Engine 的名稱, 默認為Catalina
defaultHost:默認使?的虛擬主機名稱, 當客戶端請求指向的主機?效時, 將交由默認的虛擬主機處
理, 默認為localhost
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="www.abc.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!--
docBase:Web應??錄或者War包的部署路徑,可以是絕對路徑,也可以是相對于 Host appBase的
相對路徑,
path:Web應?的Context 路徑,如果我們Host名為localhost, 則該web應?訪問的根路徑為:
http://localhost:8080/web_demo,
-->
<Context docBase="D:\ChromeCoreDownloads\ROOT" path="/root"></Context>
<!-- 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"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="www.def.com" appBase="webapps2"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- 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"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/287917.html
標籤:其他
