Tomcat Study Notes
Job: Package Server.v4 located under the project
Minicat4.0 existence question, the problem in the custom class loader webAppClassLoader instantiate not/server/LagouServlet. Class a classNotFoundException
Section 1. Browser access to the server process
1.1 Processing of HTTP RequestsSection 2. Overall Tomcat system architecture
Tomcat is an Http server (it can receive and process Http requests, so it is an Http server). We use a browser to send a request to a website. When the Http server receives the request, it calls a specific program (Java class) to process it. Often, different requests are handled by different Java classes.
The HTTP server receives the request and passes it to the Servlet container for processing, which invokes the business class through the Servlet interface. The whole set of Servlet interfaces and Servlet containers is called the Servlet specification. Note: Tomcat implements both the Servlet container as required by the Servlet specification and the HTTP server functionality.
Two important identities of Tomcat 1) HTTP server 2) Tomcat is a Servlet container
2.2 Processing Process of the Tomcat Servlet Container
When a user requests a URL resource 1) the HTTP server encapsulates the request information in a ServletRequest object
2) Go ahead and call a specific Servlet in the Servlet container
3) In 2), after receiving the request, the Servlet container finds the corresponding Servlet according to the mapping relationship between URL and Servlet
4) If the Servlet is not already loaded, create the Servlet using the reflection mechanism and call the init method of the Servlet to complete the initialization
5) The service method of the specific Servlet is then called to process the request, and the result of the request is encapsulated in a ServletResponse object
6) Return the ServletResponse object to the HTTP server, which sends the response to the client
2.3 Overall Tomcat System Architecture
From the above, we can see that Tomcat has two very important functions that need to be accomplished
1) Interact with the client browser, conduct socket communication, and convert byte stream and Request/Response objects
2) The Servlet container handles business logic
Tomcat has two core components, connectors and containers, designed to fulfill the two core functions of Tomcat.Connector, responsible for external communication:Handle Socket connection, responsible for the transformation of network byte stream and Request and Response objects;Container, responsible for internal handling:Load and manage servlets, as well as specifically handle Request requests
For details about the Core Configuration of the Tomcat server in Section 2, refer to the meanings of each tag in server. XML.
Section 3 Tomcat connector component Coyote
3.1 introduction of Coyote
Coyote is the component name of the Connector in Tomcat. It is the external interface. Through Coyote, the client establishes a connection to the server, sends requests, and receives responses.
(1) Coyote encapsulates the underlying network communication (Socket request and response processing)
(2) Coyote completely decouples Catalina containers (container components) from specific request protocols and IO operation modes
(3) Coyote encapsulates the Socket input conversion into a Request object, which is further encapsulated and sent to the Catalina container for processing. After the processing of the Request is completed, Catalina writes the result into the output stream through the Response object provided by Coyote
(4) Coyote is responsible for specific protocol (application layer) and IO (transport layer) related content
I/O models and protocols supported by Tomcat Coyote Tomcat supports a variety of application-layer protocols and I/O models as follows:Before 8.0, Tomcat adopted BIO as the default I/O mode, and then NIO. NIO, NIO2 and APR were all superior to BIO in terms of sexual performance. If you use APR, you can even achieve the performance impact of Apache HTTP Server.
3.2 Coyote internal components and processes:Coyote components and functions:
Section 4 Tomcat Servlet container Catalina:
4.1 Hierarchical structure diagram of Tomcat module and Catalina location
Tomcat is a Web container made up of a set of configurable (conf/server.xml) components, and Catalina is Tomcat’s servlet container. On the other hand, Tomcat is essentially a Servlet container, because Catalina is the core of Tomcat, and all other modules support Catalina. For example, the Coyote module provides the link communication, the Jasper module provides the JSP engine, the Naming provides the JNDI service, and the Juli provides the log service.
4.2 Structure of Servlet container Catalina
Tomcat (it is often recognized that Tomcat is an example of Catalina because Catalina is the core of Tomcat)
In fact, you can think of the entire Tomcat as an instance of Catalina, which is initialized when Tomcat starts
An instance creates other instances by loading server.xml, creates and manages a server, and a server creates and manages multiple services.
Each service can have multiple Connectors and one Container.
A Catalina instance (container)
A Server instance (container)
Multiple Service instances (containers)
Each Service instance can have multiple Connector instances and one Container instance
Catalina
Responsible for parsing the Tomcat configuration file (server.xml) to create and manage the server components
Server
The server represents the entire Catalina Servlet container and other components and is responsible for assembling and starting the Servlaet engine and Tomcat connections
Device. Server provides an elegant way to start and shut down the entire system by implementing the Lifecycle interface
Service
A Service is an internal component of a Server. A Server contains multiple services. It binds several Connector components to one
Container
Container
A container that handles user servlet requests and returns objects to web users
4.3 Container Structure
The Container component consists of Engine, Host, Context, and Wrapper. These four components (containers)
It’s a father-son relationship. Tomcat makes the Servlet container very flexible through a layered architecture.
Engine
Represents the entire Servlet Engine of Catalina, used to manage multiple virtual sites, a Service can only have one Engine at most,
But an engine can contain multiple hosts
Host
Represents a virtual host, or a site, for Tomcat can be configured with multiple virtual host addresses, while a virtual host
Multiple contexts can be contained
Context
Represents a Web application. A Web application can contain multiple wrappers
Wrapper
Represents a Servlet. The Wrapper is the lowest level of the container and cannot contain subcontainers
The configuration of the above components is actually in conf/server.xml.
Tomcat class loading mechanism:
If multiple Web applications can be deployed under a Single Tomcat, it has multiple WebAppClassLoaders.
The webAppClassLoader loads the class itself, breaking the parent delegation mechanism
Tomcat tuning:
The main focus is on custom thread pool parameters around JVM parameters and Connector connectors
Using aspects can be paired with Nginx, which handles static requests (static and static separation), thereby reducing the number of threads for Tomcat requests
Tomcat starts in the main method of the Bootstrap class. Tomcat has a top-level abstract parent class LifecycleBase
This template will be used multiple times. First, go to StandardServer and initialize the engine, executor,connector. All of these components will be initialized using the parent interface (Lifecycle) init method. Go back to the LifecycleBase initInternal() method and go through the respective implementations, such as standEngine– > StandardThreadExecutor—->connector
Finally, an address is bound to listen for client connections
. To be continued