HTTP2: springboot启用http2

springboot http2应用条件

  • 使用servlet 4.0
  • jdk 9+
  • tomcat 9+

springboot 开启h2

创建证书

创建脚本:

keytool -genkey -keyalg RSA -alias wisely -keystore keystore.jks -storepass pass1234 -validity 4000 -keysize 2048

添加springboot配置
server:
  port: 8080

  http2:
    enabled: true #启用http2

  ssl: #添加证书
    key-store: classpath:keystore.jks
    key-store-password: pass1234
    key-store-type: JKS
    key-alias: wisely
    key-password: pass1234
验证

浏览器验证
通过浏览器访问接口,然后查看使用的protocol。

springboot 开启h2c (无ssl)

Springboot默认自带的tomcat开启http2必须使用ssl,所以需要重新启一个tomcat,并修改其配置,使其支持HTTP2.0 (Springboot支持多connector)。如果只考虑支持h2c,可以让springboot自带的tomcat不启动。

新启的tomcat可以不需要上面的配置,把它注释掉:

server:
#  http2:
#    enabled: true #启用http2
#
#  ssl: #添加证书
#    key-store: classpath:keystore.jks
#    key-store-password: pass1234
#    key-store-type: JKS
#    key-alias: wisely
#    key-password: pass1234

添加TomcatServletWebServerFactory 的Configuration类:

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http2.Http2Protocol;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class H2cConfig {

    private int port = 8081;

    @Bean
    public ServletWebServerFactory servletContainer(){
        TomcatServletWebServerFactory servletWebServerFactory = new TomcatServletWebServerFactory();
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.addUpgradeProtocol(new Http2Protocol());
        connector.setPort(port);
        servletWebServerFactory.addAdditionalTomcatConnectors(connector);
        return servletWebServerFactory;
    }
}

验证

使用okhttp进行验证。
代码如下:

import okhttp3.ConnectionPool;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;


public class OkHttpClient {

    public static String get(String url) throws IOException {
        List<Protocol> protocols = new ArrayList<>();
//        protocols.add(Protocol.HTTP_2);
        protocols.add(Protocol.H2_PRIOR_KNOWLEDGE);
        okhttp3.OkHttpClient.Builder builder = new okhttp3.OkHttpClient.Builder()
                .readTimeout(Duration.ofSeconds(10))
                .connectTimeout(Duration.ofSeconds(10))
                .writeTimeout(Duration.ofSeconds(10))
                .connectionPool(new ConnectionPool())
                .protocols(protocols);
        okhttp3.OkHttpClient httpClient = builder.build();
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Response response = httpClient.newCall(request).execute();
        return response.body().string();
    }


    public static void main(String[] args) throws IOException {
        String body = OkHttpClient.get("http://localhost:8081/test/get?q=fd");
        System.out.println("http2 response:" + body);
    }
}

启动springboot应用。
再过行OkHttpClient.main()方法,运行结果:
HTTP2: springboot启用http2_第1张图片

参考

与华为联调过程中,HTTP/2.0 协议踩坑
Spring Boot 2.x实战50 - Spring Web MVC 22 - Servlet容器(配置SSL与HTTP/2)
Spring Boot实现Undertow服务器支持HTTP2协议
Spring Boot 2.x实战50 - Spring Web MVC 22 - Servlet容器(配置SSL与HTTP/2)
springboot http2

你可能感兴趣的:(spring,boot)