Interpretation of tomcat source code (I)
What is tomcat?
Tomcat By Apache Software Foundation Jakarta A of project development Servlet Container, per Sun Microsystems Technology provided Specification, which realizes the Servlet and JavaServer Page(JSP)Support and provide as Web Some unique features of the server, such as Tomcat Management and control platform, security domain management and Tomcat Valve, etc. because Tomcat It also contains a HTTP Server, which can also be regarded as a separate server of Web The server. However, you cannot Tomcat and Apache HTTP Server confusion, Apache HTTP The server is a C Language implemented HTTPWeb The server; these two items. HTTP web server Not tied together. Apache Tomcat Contains a configuration management tool, which can also be To edit XML Format configuration file.
In short: tomcat is an application that accepts http requests, parses http requests and feeds back to the client
tomcat can be said to be an official reference implementation of sun Servlet, because tomcat was first developed by sun and later donated to apache foundation
Before we understand tomcat, we need to know what the Servlet implemented by Tomcat is?
Starting with the rise of the Internet tide, we know that web pages are not only static but also dynamic. At the beginning, the common technology to realize dynamic web pages is CGI. However, as the inventor of Java, SUN | must make a super show and let the whole world know Java at once. However, we soon found that Applet is not very useful, Seeing the Internet becoming popular, we must catch the once - in - A - lifetime express
Thus, servlet was born. Servlet is actually a set of standards defined by SUN to enable Java to realize dynamic and interactive web pages and enter the field of web programming
This set of standards says that if you want to develop dynamic web pages in Java, you can define your own "Servlet", but you must implement my HTTPServlet interface, and then overload the doGet(), doPost() method. When users GET from the rover, they call the doGet method, and when they send form data from the Rover to the server, they call the doPost method, If you want to access the parameters passed by the user from the browser, just use the HttpServletRequest object, which has getparameter and getquerystring methods. If you want to return data to the browser after processing, use the HttpServletResponse object to call the getPrintWriter method to output data
If you want to implement a shopping cart, you need a session. It's very simple to call the getSession method from HttpServletRequest
How does a Servlet work
Servlet is a complex system, but it has three basic tasks. For each request, the servlet container will complete the following three operations:
(1) create a Request object and fill the Request object with information that may be used in the calling Servlet, such as parameters, headers, cookie s, query strings, URI s, etc. the Request object is an instance of the javax.servlet.ServletRequest interface or javax.servlet.ServletRequest interface
(2) create a response object calling Servlet to send response to Web client. The response object is an instance of javax.servlet.http.ServletResponse interface or javax.servlet.ServletResponse interface;
(3) call the service method of the Servlet and pass in the request object and response object as parameters. The Servlet reads information from the request object and sends response information through the response object
You wrote a "Servlet" and then you want to run it. You find that you can't run it directly through java. You need a container that can run servlets. Sun first implemented one, called java Web server, and donated it to Apache Software foundation in 1999, so it was renamed Tomcat. This is the origin of Tomcat
Therefore, Tomcat is a Servlet container, which can receive the user's request from the browser, forward it to the Servlet for processing, and send the processed response data back to the browser
However, the old CGI method is still used for html output by Servlet, which is output sentence by sentence. Therefore, it is very inconvenient to write and modify html. Therefore, Java Server Pages(JSP) comes to the rescue. JSP does not add any functions that can not be realized by Servlet in essence. In fact, JSP needs to be compiled into Servlet before running
However, it is more convenient to write static HTML in JSP. There is no need to output each line of HTML code with println statement. More importantly, with the separation of content and appearance, tasks of different nature in page making can be easily separated: for example, page designers design HTML and flow out the space for Java programs to insert dynamic content
Tomcat can run servlets and of course JSP
Since it is a Web server, tomcat can not only run servlets and JSP S, but also support access to static html, pictures and documents like Apache/nginx, but its performance is poor. In practical applications, Apache/nginx is generally placed at the front end as a load balancing server and a static resource server, followed by a cluster composed of tomcat
If the user requests a static resource, nginx will handle it directly. If it is a dynamic resource (such as xxx.jsp), nginx will forward it to a tomcat according to a certain algorithm to achieve the purpose of load balancing
How is Tomcat designed? What are the so-called components?
First, a brief description can be made through the following figure
The overall framework of Tomcat has four levels, of which Connector and Container are the most important
Server and Service
AJP (apache private protocol for communication between tomcat and apache static server)
logger (log management)
Note: Server is the Container of the entire Tomcat component, including one or more services. Service: service is a collection containing connectors and containers. Service receives user requests with appropriate connectors and sends them to the corresponding containers for processing.
As you can see from this picture, Tomcat There are only two core components Connector and Container(More details will follow) Connector+One Container Form a Service，Service It's a component that provides external services. Yes Service assembly Tomcat Just You can provide external services, but service alone is not enough. You have to have an environment to provide services, so the outermost layer Server Just for Service carry Soil for survival. So what are these components for? Connector Is a connector, which is mainly responsible for receiving requests and handing them to Container，Container It is a container, which mainly contains the components that specifically process requests. Service Mainly for association Container And Connector，A separate Container Or a separate Connector Can not fully process a request, only two are combined To complete the processing of a request. Server This is responsible for management Service Set, we can see one from the figure Tomcat It can provide a variety of services, So these Serice Is by Server The specific work includes: providing an external access interface Service，Internal maintenance Service collection Close, maintain Service Collection also includes management Service Life cycle, looking for a request Service,End a Service etc.
Tomcat All problems are handled in the container, and where does the container get the input information? Connector That's what I do. He will from socket The transmitted data is encapsulated into Request, Pass to container for processing. We usually use two Connector,One is called http connectoer， Used to pass http Demand. The other is called AJP， In our integration apache And tomcat At work, apache And tomcat It is through this agreement that we interact with each other. (speaking of) apache And tomcat The integration work, usually our purpose is to make apache Get static resources and let tomcat To parse the dynamic jsp perhaps servlet. )
In general, the Connector parses Http or Ajp requests.
We just saw, The containers are from large to small Engine, Host, Context, Wrapper, Each container from left to right is a one to many relationship, in other words, Engine Containers can have multiple Host container, Host Containers can have multiple Context container. Context Containers can have Multiple Wrapper container. Let's look at the explanation of each component: Container: It can be understood as a container for processing a certain type of request. Generally, the processing method is to wrap the processor processing the request as Valve(valve) Objects and put them in a certain order. The type is Pipeline(The Conduit)In the pipe. Container There are multiple seed types: Engine,Host,Context and Wrapper，These seed types Container Include in turn to handle requests of different granularity Container It contains some basic services, such as Loader,Manager and Realm. Engine: Engine contain Host and Context，After receiving the request, still give the corresponding Host In the corresponding Context Deal with it inside. Host: Is what we understand as a virtual host. Context: It's the specific of our subordinates Web In the context of the application, each request is processed in the corresponding context. Wrapper: Wrapper For each Servlet of Container，each Servlet Have corresponding Wrapper To manage. Yes See Server,Service,Connector,Container,Engine,Host,Context and Wrapper The role of these core components The range is decremented layer by layer and included layer by layer. Here are some quilt Container Basic components used: Loader: Be Container Used to load various required Class. Manager: Be Container Used to manage Session Pool. Realm: It is used to handle authorization and authentication in security.
requirements are transferred to the container, and when appropriate, they will be transferred to the next container for processing. The container contains a variety of components, which can be understood as providing a variety of value-added services. For example:
manager: When a container is filled with manager Component, the container supports session Managed, in fact tomcat Inside session Administration, Just rely on context Inside manager component. logger: When a container is filled with logger After the component, what happens in the container is recorded by the component, We Usually in logs/ I can see it in this directory catalina_log.time.txt as well as localhost.time.txt and localhost_examples_log.time.txt. This is because we are: engin, host as well as context(examples) These three containers are installed logger Component, which is also the default installation, also known as standard configuration . loader: loader This component is usually only given to us context Container use, loader Is used to start context as well as Manage this context of classloader Yes. pipline: pipeline Is such a thing, using the responsibility chain model. When a container decides to pass the information from its superior When the request is given to the sub container, he puts the demand into the pipe of the container(pipeline)Go inside. When demand flows foolishly in the pipeline After a while, it will be intercepted by various valves in the pipeline. For example, there are two valves in the pipeline. The first valve is called "access_allow_vavle"， In other words, when the demand flows, it will see which demand it is IP Come here, if this IP It's already dark It's on the list, sure, Kill! The second valve is called“ defaul_access_valve"It does routine checks, and if it passes, OK， Transfer the requirements to the sub containers of the current container. In this way, the requirements are transferred and flow in each container, and finally arrive at the destination. valve: It is the valve mentioned above. Tomcat There are probably such things in it, which we can simply understand tomcat It is a top-down framework, and the container contains Such a structure of a sub container.
Take another look at the overall architecture of Tomcat:
- Component oriented architecture
- JMX based
- event listeners
The component oriented architecture Tomcat code looks huge, but its structure is clear and simple. It is mainly composed of a pile of components, such as Server, Service, Connector, etc., and manages these components based on JMX. In addition, the components that implement the above interfaces also implement the interface Lifecycle representing the lifetime, so that their components perform a fixed lifetime in the process of the whole lifetime The extension is implemented through event listening Lifecycle event. The core class diagram of Tomcat is as follows:
We have described the functions of each class above, including the role of Catalina class and Server class, how Service class associates Connector and Container, and the above relationship of each Container, including how data in the Container is transferred from pipeline, and some components, such as class loader, manager and security principal (realm);
Based on JMX, Tomcat will carry out the registration process for each component and manage it through Registry, which is implemented based on JMX. Therefore, when looking at the init and start process of components, it is actually the start method to initialize MBeans and trigger MBeans. You will see a lot of examples, such as: Registry.getRegistry(null, null).invoke(mbeans, "init", false); Registry.getRegistry(null, null).invoke(mbeans, "start", false); such code is actually used to manage the behavior and life cycle of various components through JMX.
So, what is JMX?
JMX Namely Java Management Extensions(JMX standard), It's for tomcat Managed. tomcat The implementation in is commons modeler library, Catalina Use this library to write hosting Bean Work of. trusteeship Bean Is used to manage Catalina Of other objects in the Bean.
Event listening (observer mode / event driven) each component will have various behaviors in its life cycle, and these behaviors will trigger corresponding events. Tomcat aims to expand these behaviors by listening to these times. During the process of looking at the init and start of components, you will see a large number of events, such as lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null) ; such code is used to trigger a certain type of event. If you want to add your own behavior, you can only register the corresponding type of event.
Reprinted is the big man's Mona Lu Road.
In order to make your series more complete, if there is infringement, please contact me