Getting Started with HTTP2

1. Background

The previous article introduced the ProtoBuf serialization protocol, which is one of the reasons why gRPC is awesome. The second reason is the adoption of HTTP2. HTTP2 is developed from SPDY. In the past, Taobao MTOP platform implanted the SPDY protocol for performance optimization. In this way, Taobao The client can adjust the backend interface XX times faster. It should be replaced by HTTP2.0 now. It has been many years and I have almost forgotten what MTOP is, so it is still necessary to write notes and organize them frequently.

2. HTTP2 optimization of HTTP1

1. Multiplexing

  • HTTP2 implements parallel requests on a single TCP connection. One connection can carry any number of bidirectional data streams. The stream is a virtual channel in the connection, and the messages in the stream are transmitted in binary frames.
  • HTTP1.X requires multiple concurrent requests, must use multiple TCP connections, and has a limit on the number of requests for a single domain name.

2. Data encoding

  • HTTP2 splits requests and responses into smaller frames and uses binary encoding.
  • HTTP1.X request and response messages consist of a start line, header and body, with each part separated by newline characters.

3. HTTP2 also supports header compression and server push features.

3. Experimental testing

The purpose of this experiment is to test the multiplexing characteristics of HTTP2.

1. Build environment

Nginx configuration Http2 necessary conditions
Nginx version is greater than 1.9.5, my version is 1.20.1
OpenSSL version is greater than 1.0.2e, my version is OpenSSL 1.0.2k
Check whether Nginx has two modules installed --with-http_v2_module, -- with-openssl

nginx.conf configuration

Note: Configuring HTTP2 requires the same certificate as configuring HTTPS. Add [http2] to the HTTPS configuration to support it. To remove HTTPS, just remove [https].

2. Comparison of network request messages

First, I wrote an index.html with 20 pictures embedded inside.

<img src="a.jpg"/>
<img src="b.jpg"/>
......
<img src="t.jpg"/>

Configured as HTTP2, browser network request screenshot

Configured as HTTP2, TCPDUMP captures data on the server. You can see that there is only one connection (number of SYN packets)

tcpdump -ieth0 tcp port 443 and host 115.195.149.98 -w http2.pcap

Screenshot of network request configured as HTTPS browser

TCPDUMP captures packet data on the server side. You can see that 21 connections were made, one index.html, and 20 image requests.

Conclusion: From the analysis of packet capture data, we can find that HTTP2 only uses one TCP connection. In theory, it will be faster without so many TCP three-way handshakes. The actual test results are also faster, but the number of samples in this single test Too few and cannot be used as an evaluation standard. Just understand the principle. Regarding the HTTPS protocol, you can see using a packet capture tool to analyze HTTPS messages.

After I finished writing the article, I discovered that HTTP3 came out last year. I haven’t done much research on HTTP2 yet.

Guess you like

Origin blog.csdn.net/2301_76787421/article/details/132841277