编程知识 cdmana.com

Tomcat principle and system architecture

[Toc]## edition :https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.50/## One , Catalog description ![image-20201223212212784](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223212212784.png)- bin/ bin Two files in the directory : Start and stop script files Start :`startup.bat`(win),`startup.sh`(Linux & Mac) stop it :`shutdown.bat`(win),`shutdown.sh`(Linux & Mac)- conf/ Configuration file storage directory `logging.properties`: Log configuration `server.xml`: Server configuration file , For example, the designation of port `tomcat-users.xml`: Defined tomcat And the role that the character has `web.xml`: Global configuration , Familiar with , We java web Projects have their own web.xml, This is Tomcat Level of , Their own web.xml If you repeat it, your own will cover Tomcat Of .- lib/ Deposit jar package tomcat It is also based on java Developed , Its implementation also depends on some basic jar package - logs/ Keep the log - temp/ Temporary directory - webapps/ Default the directory to release the project When we release a project , The project we need to jar package ( Such as the release jar package , It will decompress ) Or the files are stored in the directory - work/ jsp Compile and execute the directory where the process files are stored ## Two , Browser access to the server process **Http Request processing :**![image-20201223213708344](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223213708344.png)http The request just defines the organization of the data ( Communication format ), It's an application layer protocol , It's data transmission TCP/IP Agreement .1. The user initiates a request (url Address 、 Click on 、 Search, etc ) Action by ** Browser ** Capture 2. ** Browser ** Transmit TCP Connection request , To the server (socket Deal with )3. ** Server ** Receiving requests and establishing connections ( Three handshakes )4. After the connection ,** Browser ** Generate Http Form of agreement ( For example, define the request header xxxx, Request to put in the body xxxxx) The information package of 5. ** Browser ** Transmit request packets ( It also depends on TCP Agreement )6. ** Server ** analysis Http The format of the package 7. ** Server ** Execute the request to complete the business logic 8. ** Server ** Generate Http Protocol format package 9. ** Server ** Transmit response packets ( Also rely on TCP Agreement )10. ** Browser ** analysis Http The format of the package 11. ** Browser ** Static transmission of data to users :![image-20201223214317941](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223214317941.png)## 3、 ... and ,Tomcat Overall system architecture ### 3.1 Tomcat The general flow of the request **Tomcat It's a Http Server ( Because it can receive and process Http Ask for )** We make ⽤ Browser to ⼀ One ⽹ The station initiates a request , It's sending out Http Ask for , So at the far end ,Http After the server receives this request , I can tune ⽤ The specific program (Java Class ) Enter ⾏ Deal with , Often different requests are made by different Java Class completes processing .![image-20201223221200909](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223221200909.png)** Approximate process :** Different from the picture above , Added Servlet Container decoupling ![image-20201223221419723](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223221419723.png)HTTP After receiving the request, the server gives it to Servlet Containers to handle ,Servlet The container passes through Servlet Pick up ⼝ Tune ⽤ Business .**Servlet Pick up ⼝ and Servlet Container this ⼀ The whole set is called Servlet Regulate .**> Be careful :Tomcat According to both Servlet The requirements of the specification have been fulfilled Servlet Containers , It also has HTTP Server functions . >> Tomcat Two important identities of >> 1)http Server >> 2)Tomcat yes ⼀ One Servlet Containers ### 3.2 Servlet Container processing request flow when ⽤ The user asks for something URL Resources 1. HTTP The server encapsulates the request information as Request thing , And then it turns into **ServletRequest** thing 2. Enter ⼀ Step to tune ⽤Servlet A specific... In a container **Servlet** 3. stay 2 in ,Servlet When the container gets the request , According to URL and Servlet The mapping of , Find the corresponding Servlet 4. If Servlet It hasn't been loaded yet , Just ⽤ The reflection mechanism establishes this Servlet, And adjust ⽤Servlet Of init⽅ Method to complete the initialization ( Reflection )5. And then the tune ⽤ This is specific Servlet Of service⽅ To handle the request , Request processing results in ⽤ServletResponse Object encapsulation 6. hold ServletResponse The object is returned to HTTP Server ,HTTP The server sends the response to the client ![image-20201223222637232](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223222637232.png)### 3.3 Tomcat The overall architecture of the system can be seen from the above ,Tomcat Two important functions :1. Browser and client ⾏ Interaction , Enter ⾏socket Communications , Combine the byte stream with Request/Response Wait for the object to enter ⾏ Conversion 2. Servlet Containers handle business logic Tomcat Designed two cores ⼼ Components ** Coupling (Connector)** and ** Containers (Container)** To complete Tomcat Two of them ⼤ nucleus ⼼ function :- ** Coupling **, Responsible for foreign exchange : Deal with Socket Connect , Responsible for ⽹ Network byte stream and Request and Response Transformation of objects ;- ** Containers **, Responsible for internal processing : Load and manage Servlet, And how to deal with it Request Ask for ;![image-20201223222843720](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223222843720.png)## Four ,Tomcat Coupling elements Coyote### 4.1 Introduction Coyote yes Tomcat Component name of the coupler in , It's an external connection ⼝. Client through Coyote Build with server ⽴ Connect 、 Send the request and accept the response .1. Coyote Encapsulating the underlying ⽹ Network communication (Socket Request and response processing )2. Coyote send **Catalina Containers ( The name of the container component )** With specific request agreement and IO operation ⽅ The formula is completely decoupled 3. Coyote Will Socket Lose ⼊ Conversion ** Package as Request thing **, Enter ⼀ Step after packaging (** Again encapsulated as ServletRequest**) Leave it to Catalina The container goes into ⾏ Deal with , After processing the request , Catalina Through Coyote Provided Response The object writes the result ⼊ Output stream 4. Coyote Responsible for the specific ** Agreement ( Should ⽤ Layer )** and **IO( Transport layer )** Related content ![image-20201223224257261](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223224257261.png)**Tomcat Coyote Supported by IO Models and protocols :**![image-20201223224941404](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223224941404.png) Application layer default protocol :HTTP/1.1 Transport layer default IO Model :NIO> stay 8.0 Before ,Tomcat Presupposition adopts ⽤ Of I/O⽅ The formula is BIO( Synchronous blocking IO), Then it was changed to NIO. No matter NIO、NIO2 Or APR, In efficiency ⽅⾯ They are better than the previous ones BIO. If you pick ⽤APR, what ⾄ Can reach Apache HTTP Server The impact of the .### 4.2 Coyote Internal components and processes ![image-20201223230210550](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223230210550.png)| Components | Function description || ------------------- | ------------------------------------------------------------ || **EndPoint** | EndPoint yes Coyote Communication endpoint , namely ** Communication monitoring is connected to ⼝**, yes ** Specifically Socket Receive and transmit processors **, yes ** The abstraction of the transport layer **, therefore EndPoint⽤ Come ** Realize TCP/IP Agreement ** Of || **Processor** | Processor yes Coyote Protocol processing ⼝ , If you say EndPoint yes ⽤ To achieve TCP/IP Agreed , So Processor**⽤ To achieve HTTP Agreement **,Processor Take over ⾃EndPoint Of Socket, Read bit stream ** It can be interpreted as Tomcat Request and Response thing **, And ** Through Adapter Submit it to the container ** Deal with ,Processor yes ** Correspondence ⽤ Layer protocol abstraction ** || **ProtocolHandler** | Coyote The agreement follows ⼝, Through Endpoint and Processor , Implementation of specific protocols Li Neng ⼒.Tomcat According to the agreement and I/O Provides 6 Implementation classes : **AjpNioProtocol** , **AjpAprProtocol**, **AjpNio2Protocol** , **Http11NioProtocol** , **Http11Nio2Protocol** ,**Http11AprProtocol** || **Adapter** | Due to different agreements , The request information sent by the client is also different ,Tomcat Defined ⾃⼰ Of Request Class to encapsulate the request information .ProtocolHandler Pick up ⼝ Responsible for parsing requests and ⽣ become Tomcat Request Class . But this Request Objects are not standard ServletRequest, You can't ⽤Tomcat Request Adjust as an argument ⽤ Containers .Tomcat The designer's solution ⽅ The case is cited ⼊CoyoteAdapter, This is ** The classic operation of interface card mode ⽤**, Coupling adjustment ⽤ CoyoteAdapter Of Sevice⽅ Law , Pass on ⼊ Yes. Tomcat Request thing , CoyoteAdapter Responsible for ** Will Tomcat Request Into ServletRequest**,** Adjust again ⽤ Containers ** |## 5、 ... and ,Tomcat Servlet Containers Catalina### 5.1 Tomcat Module hierarchical structure diagram and Catalina Location Tomcat yes ⼀ It's up to ⼀ Series configurable (conf/server.xml) Of the components of Web Containers ,⽽Catalina yes Tomcat Of servlet Containers .**Tomcat It's essentially ⼀ paragraph Servlet Containers **, Because **Catalina It's just Tomcat The core of ⼼** , ** Other modules are for Catalina Provide ⽀ Hold on **. ⽐ Such as : - Through Coyote Module provides link communication - Jasper Module provides JSP engine - Naming Provide JNDI Service - Juli Provide ⽇ Volunteer service .** Module hierarchy diagram :**![image-20201223235736095](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201223235736095.png)### 5.2 Servlet Containers Catalina Structure ** It can also be said that :Tomcat It's just a Catalina Examples of , Because Catalina yes Tomcat At the heart of .**Tomcat/Catalina For example :![image-20201224001249519](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201224001249519.png)Tomcat This instance item will be initialized at startup ,Catalina Examples are loaded by server.xml Complete the establishment of other examples , Set up and manage ⼀ One Server,Server Build and manage multiple services , Every service ⼜ There can be multiple Connector and ⼀ One Container.- ⼀ One Catalina For example ( Containers ) - ⼀ One **Server** For example ( Containers ) - Multiple **Service** For example ( Containers )** Correspondence : Every time ⼀ One Service There can be more than one under example Connector Examples and ⼀ One Container For example **- **Catalina** Responsible for analysis Tomcat Configuration of ⽂ Pieces of (server.xml) , To create a server Server The components go in parallel ⾏ management - **Server** The server represents the whole Catalina Servlet Containers and other components , Responsible for assembly and start-up Servlet engine 、Tomcat Coupling .Server By implementing `Lifecycle` Pick up ⼝, Provides ⼀ It's an elegant way to start and shut down the whole system ⽅ type - **Service** Service is Server Internal components ,⼀ One Server Contains multiple Service. It will be like ⼲ One Connector The element is tied to ⼀ One Container- **Container** Containers , Responsible for handling ⽤ It's not like servlet Ask for , And return the object to web⽤ User module ### 5.2 Container The specific structure of the element ![image-20201224002346585](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201224002346585.png)Container There's ⼏ Specific components , The difference is **Engine**、**Host**、**Context** and **Wrapper**. This 4 Kind of components ( Containers ) yes ⽗⼦ Relationship .Tomcat Through ⼀ A hierarchical architecture , bring Servlet The container is very flexible .- **Engine** Means the whole Catalina Of Servlet engine ,⽤ To manage multiple virtual sites ,⼀ One Service There can only be ⼀ One Engine, however ⼀ An engine can contain more than one Host- **Host** representative ⼀ Virtual hosts , Or ⼀ Sites , You can give Tomcat Configure multiple virtual host addresses ,⽽⼀ Under a virtual host Can contain more than one Context- **Context** Express ⼀ One Web Should ⽤ The program , ⼀ One Web Should ⽤ Can contain more than one Wrapper- **Wrapper** Express ⼀ One Servlet,Wrapper As the bottom of the container , Can not contain ⼦ Containers ** The configuration of the above components is actually reflected in conf/server.xml in .**![image-20201224002723935](https://typora-files.oss-cn-beijing.aliyuncs.com/file/image-20201224002723

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224152743232d.html

Scroll to Top