Illustrated spring: http request processing flow and mechanism [1]

Time:2020-10-31

In 2003, the veteran brother first came to ZTE to start a postgraduate internship. Spring was born that year. In March 2004, version 1.0 was released, and now it has been more than 15 years. From single layered architecture to cloud native microservice architecture, it has never been shaken at the top of Java application framework. It has brought us great value, not only can reduce the development difficulty, but also can improve the development efficiency. But time not only changed the veteran brother, but also did not let spring go. We all became more and more powerful.

Before it was released, there were 5 versions of spring and it was getting richer and smaller, and spring was becoming more and more powerful. But it’s not fair for the buddies who are new to spring. Unlike veterans, you can grow up with it. Now the technology stack is very large. Do you have a shortcut to get through this giant in a short time? Yes, just like the DOS operating system, a floppy disk was installed, with a total of only a few MB. Now, there are dozens of hundreds of GB. However, the operating system kernel is very small, and its principle mechanism is those in the textbook. One can only master these stable “one two three”, that is, the core principle mechanism (for example: IOC / AOP / ORM) Then we can achieve twice the result with half the effort.

Just like we bought a blank room, we must decorate it before we move in. Among them, the wiring of water, electricity, gas, net and other pipelines must go first. Different pipelines have different ways. Some go on the floor and some go on the wall. Only when the pipelines are laid properly can we pave the floor tiles, hang the ceiling, brush the wall stickers, etc. As the owner, if you don’t know the routing of these pipelines, when there are problems such as power failure, network disconnection or water leakage, you don’t know how to analyze, locate and repair them. In the company, veteran brother often receives help from problems. Because the small partners don’t know these principles and mechanisms, it’s quite simple to analyze and locate the problem for many days, but I haven’t got a clue. If we follow the principle mechanism, then we can do it with ease.

In fact, these underlying principles and mechanisms are not complicated. We just don’t know about them, or we lack good information. Veteran brother is going to analyze these principles and mechanisms to you by means of diagrams. This series of articles will focus on the whole process of spring processing HTTP requests, and help you to understand and master the pipeline and wiring in spring skyscraper, so that learning can get twice the result with half the effort and make the use easier. Specifically, it will include the following aspects:

  • The whole process of HTTP request processing, including browser, web server, application spring, etc;
  • The interaction interface, cooperation mechanism and configuration rules between web server and application spring;
  • Spring’s mechanism for handling HTTP requests includes dispatcher, controller, view, model, service, Dao, etc;
  • The sub processes of HTTP request processing in different application architecture scenarios, including JSP, front-end and back-end separation, etc;
  • HTTP request processing related configuration file description, including web, bootstrap, spring MVC, application context, etc;
  • HTTP request processing common problems.

For readers: development, testing, architecture, etc

1. Overview of HTTP request processing process

The cyberspace constructed by computers is a virtual world, which is the extension of our human brain power. Although the virtual world is both abstract and illusory, it is based on our experience in building the physical world. It’s not a short time to wander around the IT world. Veteran brother, I have found a way to understand the virtual world, that is, to find a prototype in the real world. The prototype is more vivid and vivid. We are also very familiar with it. Through the prototype, we can more accurately understand the mechanism behind computer technology. What prototype can we use in the real world to correspond to the process of HTTP request processing such as client (or browser), web container, web application, spring framework and so on? We can compare it to the process of delivering documents and packages by logistics express.

The computer network is like the real world traffic network, the main responsibility of the network is to carry goods. In OSI seven layer or TCP / IP four layer network protocol model, HTTP (Hyper Text Transfer Protocol), FTP (File Transfer Protocol), SMTP (Simple Mail Transfer Protocol), POP3 (post office protocol 3), IMAP4 Protocol) and other protocols belong to the application layer protocol. These protocols are just like logistics express companies that deliver different types of goods. FTP system is dedicated to transporting documents, SMTP, POP3, IMAP4 are dedicated to delivering mail, and HTTP protocol is originally used for hypertext. However, with the vigorous development of the world wide web (WWW), HTTP protocol system has been the most widely developed. Its design is very good, which brings flexibility, scalability and ease of use. It can carry the transmission of various types of information, just as the Internet logistics companies are becoming bigger and bigger, and can cover more and more business.

HTTP request or response is like a parcel, which is divided into message header and message style. The message style is the information load to be carried, and the message header is just like the express bill filled in when we usually mail things, including the address of the receiver (sender), service level requirements, description of the items to be posted, etc. For the subject of this article, we only focus on the analysis of the recipient address. The HTTP URL of a restful style service interface API is as follows:

http:// {ip} : {port} / {context-path} / {service-name} / {method-name}

For example, we need to send a file to the following address to complete user registration:

http://201.187.10.21:8080/spring-demo/user/register

The above format is for us to read. Finally, these information should be filled in the HTTP message. The real effect is as follows:

POST /spring-demo/user/register HTTP/1.1
Accept: application/json
Accept-Language: zh-cn
User-Agent: Mozilla/5.0
Host: 201.187.10.21:8080
Content-Length: 112
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=12DII6898EFY998APBE17B

name=itlaobingge&password=123456&sex=male&age=30

Next, let’s parse the HTTP URL paragraph by paragraph to see the meaning of each piece of information:

  • IP: IP address information, used for addressing different computers in a specific network.
  • Port: port information, used for addressing different web containers on the same computer. A computer can run multiple web container instances.
  • Context path: context path, used for addressing different applications in the same web container. A web container instance can deploy multiple applications.
  • Service Name: service name, used for addressing different services within the same application. There are usually multiple services within an application.
  • Method name: method name, used for addressing different methods within the same service. A service usually contains multiple methods.

The above address of the recipient is filled in by the client (or browser). The HTTP transmission system will deliver the parcel according to the address information of the recipient, find a specific computer based on the IP address or domain name information, and the computer will deliver the parcel to the specific web container instance according to the port information. Next, the web container will take over the parcel delivery task, and it will find the specific application according to the context path. If the current application is built with spring framework, then spring will take over the task of parcel delivery. It will find a service method that can handle the payload information in the parcel according to the service name and method name. The service method will process the information in the parcel in time, and then load the result information into the new parcel after the processing is completed, and then send it back to the client (or browser).

As shown in the figure below, this is the process of HTTP request traversing the network, computer, web container, web application and spring framework.
Illustrated spring: http request processing flow and mechanism [1]
The main value of this paper is to help you sort out the end-to-end whole process framework, that is, the global perspective or God perspective. With this framework, we can find the information of relevant nodes according to our own needs to study and learn, so as not to fall into details and find directions. Of course, considering the different work and study situation of each of us, and the problems we usually encounter are also different, the content of this article can not cover all the problems encountered by everyone. Welcome to leave a message and ask questions.

Today, let’s share it here. If you think it’s valuable, please move your fingerforwardTo other partners in need. In addition, veteran brother, I will share the experience of career planning, job interview, skill improvement, influence building and so on. WelcomefollowThis blog or the official account of the skew letter “It veteran 」!

Illustrated spring: http request processing flow and mechanism [1]

Other articles in this series are indexed as follows:

  • Diagram spring: http request processing flow and mechanism [2]
  • Illustrated spring: http request processing flow and mechanism [3]
  • Illustrated spring: http request processing flow and mechanism [4]

Recommended Today

Comparison and analysis of Py = > redis and python operation redis syntax

preface R: For redis cli P: Redis for Python get ready pip install redis pool = redis.ConnectionPool(host=’39.107.86.223′, port=6379, db=1) redis = redis.Redis(connection_pool=pool) Redis. All commands I have omitted all the following commands. If there are conflicts with Python built-in functions, I will add redis Global command Dbsize (number of returned keys) R: dbsize P: print(redis.dbsize()) […]