Author: Lin Guanhong/The Ghost at my Fingertips

The Denver nuggets: juejin. Cn/user / 178526…

Blog: www.cnblogs.com/linguanh/

Making: github.com/af913337456…

Tencent cloud column: cloud.tencent.com/developer/u…

Worm hole block chain column: www.chongdongshequ.com/article/153…


The hypertext Transfer Protocol (HTTP) version 2.0 was released in 2015. Compare this to the previous HTTP 1.1 version. It adds three major new features.

  • After the connection is established, yesmultiplexing
  • After a connection is established, a request and a response are consideredflow
  • Data transmission is divided intoBinary framefragment

HTTP 2.0 is a huge improvement over HTTP latency. The link below is an intuitive web site that shows the same 300 + images loading time under HTTP 1.1 and HTTP 2.0 protocols. HTTP 2.0 is almost six times faster than HTTP 1.1.

demo


So let’s visualize them a little bit.

The first isBinary frame

In TCP, the unit of data transmission is the datagram. The data is divided into two parts. Header and actual data section.

In HTTP 2.0, it divides the two parts of a datagram into header frames and data frames. That is, header frames and data body frames. The transmission of frames is finally carried out in the stream. The frame, header frame and data frame in the stream can be divided into multiple fragment frames. For example, data frame can be data = data_1 + data_2 +… + data_n.

In addition, if the partition is based on the binary frame as a whole, except the frame classification of the packet. There are other auxiliary frame types, such as SETTINGS, PING, GOWAY, WINDOW_UPDATE, etc., mentioned in the comments.

The second isflow

A flow represents a complete request-response data interaction. It has the following characteristics:

  1. Bidirectional: Data can be sent and received simultaneously within the same stream.
  2. Orderliness: The data being transferred in the stream isBinary frame. Frames are sent and received sequentially on a stream.
  3. Parallelism: in the flowBinary frameIt’s all being transferred in parallel, there’s no need to wait in sequence. But there is no data clutter because each frame has a sequential label. They will eventually be merged in order.
  4. Creation of streams: Streams can be created, consumed, or shared unilaterally by clients or servers.
  5. Stream closing: A stream can also be closed by either party.

Below comes from: https://blog.csdn.net/zqjflash/article/details/50179235. It demonstrates the orderliness and parallelism of frames in a stream. For example, the data frame of datagram 1 is not transmitted together, but divided into two.

Summarize the relationship between a stream and a frame

A frame is a unit of data in a stream. Header frames of a datagram can be divided into multiple header frames, and data frames into multiple Data frames.

multiplexing

HTTP 2.0 multiplexing is an updated version of HTTP 1.1’s long links.

In HTTP 1.1, after a successful link, as long as the link is still open, the client can make multiple requests in such a link in order and obtain the corresponding response data for each request. The disadvantage is that one request-response interaction must wait for the previous request interaction to complete, otherwise the subsequent interaction must wait, which is called header blocking. Here’s an example:

Request A and request B. A is initiated first, and the server receives the request and is processing it. At the same time, request B came in. But request A has not been returned, so request B has to wait.

In HTTP 2.0, after a connection is successful, the client can concurrently initiate multiple requests within a link as long as the connection is still open. Each request and its response do not need to wait for other requests. A request task takes too much time and does not affect the normal execution of other connections.

Here’s a look at it:

Because of these three features, HTTP 2.0 is a great optimization for data transfer latency.

So how do we specify the HTTP 2.0 protocol in our code?

The simplest method is to specify the protocol version in the HTTP request header when constructing the request parameters, such as the following example:

The HTTP 1.1

GET /index HTTP/1.1  
Host: www.xxx.com 
Copy the code

This paper refers to:

  • Blog.csdn.net/yangguosb/a…
  • www.cnblogs.com/ghj1976/p/4…

After the