Initial articles Tomcat: Tomcat State of the weighing

A. The purpose of writing

Why study tomcat container Le, bloggers recently as summarized in knowledge, writes architecture articles when carried out with nginx load of tomcat container application, this time bloggers think, can not do without the concurrent application, interview, work, pressure measurement , performance optimization, and so can not be separated tomcat, tomcat works in any doubt, and it is how to handle concurrency? With server memory, cpu association? You can support a number of concurrent tomcat? Tomcat how to integrate multiple applications to improve concurrency, improve customer satisfaction? Where concurrent bottlenecks? Clearly aware of this knowledge point in the future, after doing architecture, writing specific functions, and will not at a loss, I can handle this in the end how much concurrency, how much concurrency customer requirements, how can I provide concurrency, why do I interface Crimping Crimping will collapse and so on, these technologies should have their own long ago should know, at first not so many pieces, or do a long time to develop, review their knowledge, think it necessary to go into some. So began our tomcat research trip.

II. Overview and principle of operation

Tomcat simply means that a web server running JAVA underlying Socket is a program, it is also a container of JSP and Serlvet.
If you learn html, css, you will know that you can only write a page you visited, others can not remotely access the page you write, Tomcat is to provide a program that allows people access to write their own pages.
Here Insert Picture DescriptionTomcat topmost containers are Server, it represents the entire server can be seen from the diagram above, a-Service Server may comprise at least one, for a particular service.

Service consists mainly of two parts: Connector and Container. As well as other components.

  1. Only a Tomcat Server, a Server can have multiple Service, a Service can have multiple Connector and a Container;
  2. Server refers to the entire tomcat container, responsible for launching and managing each Service, while listening 8005 port sent me the shutdown command to shut down the entire container, in charge of life and death throughout the Tomcat;
  3. Service is to provide external component-based external services;
  4. Connector for processing connection-related things, and provide Socket associated with the conversion of the Request and Response; is connected with external events, listening fixed port receiving an external request. Passes the request to the Container. Container and the processing result back to the outside.
  5. Servlet Container for packaging and management, as well as specific process request Request; Servlet lifecycle management, call Servlet related methods, business logic.
  6. Jasper is the jsp parsing engine. Major topics jsp files into java file. And compiled into .class files.
  7. Naming is the naming service, connect names and objects, so that we can use the name to access the object.
  8. Session is responsible for creating and managing Session and Session persistence, you can customize. Also supports Session clusters. Tomcat server open at the bottom of both memory space. Some information can be stored temporarily.
  9. Logging is responsible for records related to the log. It includes access error information, and operational information.
  10. Jmx javase defined some technical specification. Implanting a frame management function is an application, device, system. By Jmx can remotely monitor tomcat

Three core components .Tomcat

Connector: receiving client is connected, processing client requests.

Connector for receiving requests and packaged into Request and Response, and then to the Container processing, after completion of processing to the Connector Container returned to the client.

Therefore, we can understand Connector is divided into four areas:

How to (1) Connector to accept the request?
(2) How to request encapsulated into the Request and Response?
(3) after the completion of the package Request and Response Container how to be treated?
(4) Container after processing How to Connector and returned to the client?

First, look at the structure (Figure B) Connector, as follows:
Here Insert Picture DescriptionConnector ProtocolHandler is used to process the request, different types of connection ProtocolHandler representatives, such as: Http11Protocol Socket by using a common connection, Http11NioProtocol using NioSocket to connect.

Which contains three components from the ProtocolHandler: Endpoint, Processor, Adapter.

(. 1) Endpoint Socket for processing of the underlying network connection, Processor for Socket Endpoint received encapsulated Request, Adapter for Container Request to perform specific processing.
(2) Endpoint process because it is the underlying network Socket connection, so Endpoint is used to achieve TCP / IP protocol, and the HTTP protocol to implement Processor, Adapter request to the Servlet container adapted for specific processing.
(3) Endpoint abstract implementation AbstractEndpoint inside AsyncTimeout Acceptor and two internal classes and interfaces defined in a Handler. Acceptor to listen request, AsyncTimeout Request for checking asynchronous timeout, Handler for processing the received Socket, Processor calls are processed internally.

At this point, we should be very easy to answer (1) (2) (3) of the question, but (4) still do not know, then we look at how the Container is processed and after the processing is how processed the results are returned to the Connector?

Container: parent interface, Chain of Responsibility design pattern all child containers.
Here Insert Picture Description

  1. Engine: engine, used to manage multiple sites, a Service can only have a maximum of Engine;
  2. Host: represents a site, it can also be called a virtual host, by configuring the Host can add sites;
  3. Context: represents an application program corresponding to a normal development, or a WEB-INF directory file and the following web.xml
  4. Wrapper: each encloses a Wrapper the Servlet;

Here Insert Picture DescriptionHost is the difference between Context and Context represents an application, each file in the Tomcat webapps under our default configuration directory is a folder Context, ROOT directory in which to store the main application, other sub-directory for the application, and the entire webapps is a Host site.

When we access the application Context, if it is using the domain name directly under the ROOT can access, such as: www.qi-bb.club, if other applications are under Host (webapps), you can use http: // www. qi-bb.club/docs visit, of course, the default specified application root (rOOT) can be set only by default under the Main Host site is used under the rOOT directory.

Http request processing four .Server

Here Insert Picture Description

Request from the client is assumed as: http: // localhost: 8080 / test / index.jsp request is sent to the local port 8080

  1. Users click on the page content, the request is sent to the local port 8080, was there listening Coyote HTTP / 1.1 Connector obtained.
  2. Connector to the request to it in the Engine Service to process, Engine and wait for a response.
  3. Engine obtaining request localhost / test / index.jsp, matching all virtual hosts Host.
  4. Engine Host name matched to the localhost (also not match even if the request to process the Host, Host is defined as the default host for the Engine), the Host is localhost obtaining request /test/index.jsp, matching it holds all of Context. Host matched to the path / test of Context (if not match the request to put the path named "" Context to the process).
  5. path = "/ test" Context request to obtain the /index.jsp, find out the corresponding Servlet in its mapping table. Context URLPATTERN is matched to the * .jsp Servlet, JspServlet corresponding class.
  6. HttpServletRequest object and HttpServletResponse objects constructed as a parameter JspServlet doGet () or doPost () call. Execute business logic, data storage and other programs.
  7. Context The HttpServletResponse object after executing the return to the Host.
  8. Host the HttpServletResponse object back to the Engine.
  9. Engine The HttpServletResponse object returned Connector.
  10. Connector HttpServletResponse object back to the client Browser.

Resources:
https://blog.csdn.net/jsj13263690918/article/details/80368757
https://www.cnblogs.com/small-boy/p/8042860.html

Published 215 original articles · won praise 135 · Views 1.14 million +

Guess you like

Origin blog.csdn.net/weinichendian/article/details/103826703