HTTP1.1 upgrade to HTTP2.0

HTTP1.1 upgrade to HTTP2.0

1. Introduction
1. Why should we upgrade http2.0?

HTTP2.0 has the following advantages compared to HTTP1.x:

  1. Binary framing: HTTP2.0 divides all transmitted information into smaller messages and frames, and encodes them in binary format, which can better compress and decompress data and improve transmission efficiency.

  2. Multiplexing: HTTP2.0 can transmit multiple requests and responses simultaneously on one connection, while HTTP1.x requires multiple connections to achieve concurrent transmission, which can reduce the number of TCP connections and improve performance.

  3. Header compression: HTTP2.0 uses the HPACK algorithm to compress the header, reducing the size of the header and improving transmission efficiency.

  4. Server push: HTTP2.0 supports the server to actively push resources to the client, reducing the number of client requests and improving performance.

Therefore, upgrading to HTTP2.0 can improve website performance and user experience, reduce page loading time, and increase response speed.

2.Safety performance comparison

HTTP/2.0 has great improvements in security and performance compared to HTTP/1.1. In terms of security, HTTP/2.0 forces the use of encrypted transmission, and even non-sensitive data will be encrypted, which can prevent man-in-the-middle attacks and eavesdropping. In terms of performance, HTTP/2.0 introduces multiplexing technology, which can transmit multiple requests and responses simultaneously on the same connection, avoiding the head-of-line blocking problem in HTTP/1.1, thus improving page loading speed. In addition, HTTP/2.0 also uses header compression technology to reduce the amount of data transmitted and further improve performance.

Here is an example demonstrating the performance comparison of HTTP/1.1 and HTTP/2.0:

Let's say you have a website that contains 10 small images and 1 large image for a total size of 1MB. We used the Chrome browser's developer tools to simulate loading this website and record the loading time.

In HTTP/1.1, the browser will establish 6 connections to load these resources, 1 connection is used to load HTML, and the other 5 connections are used to load images. Due to the head-of-line blocking issue in HTTP/1.1, only one request and response can be transmitted simultaneously per connection, resulting in longer load times.

In HTTP/2.0, the browser only needs to establish a single connection to load all resources, using multiplexing technology to transmit multiple requests and responses simultaneously, so load times are shorter.

According to test results, loading this website using HTTP/2.0 is about 40% faster than loading using HTTP/1.1

3. Version requirements

HTTP/2.0 is a network protocol, and its implementation is related to the JDK version and Tomcat version. Here are some common requests:

For JDK version:

  • JDK 9 and higher support the HTTP/2.0 protocol.

  • JDK 8 does not directly support HTTP/2.0, but support can be achieved by using third-party libraries such as Jetty or Netty.

For Tomcat version:

  • Tomcat 9.x supports HTTP/2.0.

  • Tomcat 8.x does not directly support HTTP/2.0, but support can be achieved using appropriate configuration and plug-ins.

2. Practice
1.Command

This command is executed under resources

keytool -genkey -alias tomcat -keyalg RSA -keystore keystore.jks -validity 365 -keysize 2048

This file will be generated and placed in the resources directory.

2.yml

yml file configuration writing

3. Configuration class

This configuration class is used to solve the code change-free caused by the upgrade.

/**
 * http force jump to https
 */
@Configuration
public class HttpProtocolConversionConfig {
    @Value("${server.port}")
    private Integer httpsPort;
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                // If you want to force the use of https, please release the following comment
                // SecurityConstraint constraint = new SecurityConstraint();
                // constraint.setUserConstraint("CONFIDENTIAL");
                // SecurityCollection collection = new SecurityCollection();
                // collection.addPattern("/*");
                // constraint.addCollection(collection);
                // context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
        return tomcat;
    }
    // Configure http
    private Connector createStandardConnector() {
        //The default protocol is org.apache.coyote.http11.Http11NioProtocol
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setSecure(false);
        connector.setScheme("http");
        // I don’t need to explain this.
        connector.setPort(80);
        connector.setRedirectPort(httpsPort); // https port number when http is redirected to https
        return connector;
    }

Guess you like

Origin blog.csdn.net/qq_49841284/article/details/134502282