springboo2开启http2

HTTP2.0特性
通俗易懂篇:https://www.cnblogs.com/yingsmirk/p/5248506.html
深入了解篇:https://www.jianshu.com/p/67c541a421f9
使用HTTP/2的几点注意事项
要使用HTTP/2需要注意以下几点

虽然HTTP/2没有明确要求必须使用TLS,但当前几乎所有浏览器均只支持 HTTP/2 Over TLS。所以在使用之前我们需要先制作一张证书。
如果您的项目中用的是tomcat或jetty,它们并不能直接支持HTTP/2,但是undertow可以。具体可以参考Spring Boot的文档:https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#howto-configure-http2
我们制作的证书是不被浏览器认可的,所以会有安全提示,不能用于生产环境。
在本文的例子中使用的是undertow

制作证书
使用JDK自带的keytool,证书类型为:PKCS12


keytool -genkey -alias undertow -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -dname "CN=localhost, OU=localhost, O=localhost, L=Zhengzhou, ST=Henan, C=CN"

输入密钥库口令:
再次输入新口令:

执行时会要求输入证书口令,这里输入的是123456。执行完命令后会在执行的文件夹生成一个keystore.p12的文件。

keytool的详细用法: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/keytool.html
常见的证书格式及其说明参考:http://www.cnblogs.com/xq1314/archive/2017/12/05/7987216.html

配置Web容器
spring boot默认使用的是tomcat,我们需要先将tomcat移除,然后换成undertow。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

如果不移除tomcat依赖会一直以Tomcat作为容器启动。

然后将刚才生成的keystore.p12拷贝到src/main/resources下。然后在application.yml中配置服务器信息。

server:
  port: 8443 # 端口
  compression:
    enabled: true
  http2:
    enabled: true # 启用http2
  ssl:
    enabled: true
    key-store: classpath:keystore.p12 # 启用http2
    key-store-password: 123456 # 证书密码
    key-store-type: PKCS12 # 证书类型
    protocol: TLSv1.2 # 协议类型
    key-alias: undertow

这时如果启动服务器,是只支持https的。

@SpringBootApplication
public class RabbitmqProducerApplication implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {

    public static void main(String[] args) {
        SpringApplication.run(RabbitmqProducerApplication.class, args);
    }

    @Override
    public void customize(UndertowServletWebServerFactory factory) {
        factory.addBuilderCustomizers((UndertowBuilderCustomizer) builder -> {
            builder.addHttpListener(8080, "0.0.0.0");
        });
    }
}
这里增加8080端口的监听,配置参考:

https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#howto-configure-webserver
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#howto-enable-multiple-listeners-in-undertow

启动后可以看到控制台打印出如下信息:

INFO 4288 — [ main] o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 8443 (https) 8080 (http) with context path ‘’
1
测试
增加一个测试控制器

@RestController
@RequestMapping("/log")
public class LogController {

    private Logger logger = LoggerFactory.getLogger(LogController.class);

    @GetMapping("/info")
    public String info() {
        logger.info("Info log");
        return "info";
    }
}

用google查看 http2链接 url:chrome://net-internals/#http2

访问:https://localhost:8443/log/info,观察控制台protocol为h2,说明这次请求使用的是HTTP/2协议。

再次请求

观察两次请求的size,一次是236一次是69,这是因为HTTP/2的头部压缩技术。

访问:http://localhost:8080/log/info,观察控制台可以看到使用的仍然是HTTP/1.1协议

而且数据包要比HTTP/2的数据包大,并且无论刷新多少次,大小是不变的。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值