Enabling http2.0 in spring boot

hexiaoming123 2021-02-22 23:23:20
enabling http2.0 http spring boot


HTTP2.0 characteristic

Use HTTP/2 A few points for attention

To use HTTP/2 The following points need to be noted

  1. although HTTP/2 There is no explicit requirement to use TLS, But at present, almost all browsers only support HTTP/2 Over TLS. So we need to make a certificate before using it .
  2. If your project uses tomcat or jetty, They don't directly support HTTP/2, however undertow Sure . For details, please refer to Spring Boot Documents :https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#howto-configure-http2
  3. The certificate we made is not approved by the browser , So there will be safety tips , Cannot be used in production environment .

In this example, we use undertow

Make a certificate

Use JDK Self contained keytool, The certificate type is :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"
 Enter the keystore password :
 Enter the new password again :

The certificate password will be required during execution , Here's the input 123456. After executing the command, a keystore.p12 The file of .

keytool Detailed usage : https://docs.oracle.com/javase/8/docs/technotes/tools/windows/keytool.html
Common certificate format and its description reference :http://www.cnblogs.com/xq1314/archive/2017/12/05/7987216.html

To configure Web Containers

spring boot The default is tomcat, We need to put tomcat remove , And then change to 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>

If you don't remove tomcat Dependence will always be Tomcat Start as container .

And then I'll take what I just generated keystore.p12 copy to src/main/resources Next . And then in application.yml Configure server information in .

server:
port: 8443 # port 
compression:
enabled: true
http2:
enabled: true # Enable http2
ssl:
enabled: true
key-store: classpath:keystore.p12 # Enable http2
key-store-password: 123456 # Certificate password 
key-store-type: PKCS12 # Certificate type 
protocol: TLSv1.2 # Protocol type 
key-alias: undertow

If you start the server , Only support https Of .

@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"); }); } } 

Add... Here 8080 Port monitoring , Configure the reference :

After startup, you can see the following information printed by the console :

INFO 4288 --- [ main] o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 8443 (https) 8080 (http) with context path ''

test

Add a test controller

@RestController @RequestMapping("/log") public class LogController {  private Logger logger = LoggerFactory.getLogger(LogController.class); @GetMapping("/info") public String info() {  logger.info("Info log"); return "info"; } } 

visit :https://localhost:8443/log/info, Observation console protocol by h2, Explain that this request uses HTTP/2 agreement .
 Insert picture description here

Ask again
 Insert picture description here

Look at two requests size, Once it was 236 Once it was 69, This is because HTTP/2 The head compression technology .

visit :http://localhost:8080/log/info, Looking at the console, you can see that you are still using HTTP/1.1 agreement
 Insert picture description here
And packets are better than HTTP/2 Packet size of , And no matter how many times , The size is constant .

版权声明
本文为[hexiaoming123]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/02/20210222135129534k.html

  1. Vue3元件(九)Vue + element-Plus + json = 動態渲染的表單控制元件
  2. 我的 HTTP/1.1 好慢啊!
  3. Vue為何採用非同步渲染
  4. The response status was 0. Check out the W3C XMLHttpRequest Level 2 spec for
  5. Front end monastery
  6. How to quickly understand a new front end project?
  7. webpack4.X核心工具库之tapable实例对象Hook
  8. webpack4.X核心工具库之tapable实例对象Hook
  9. C++使用libcurl进行http通讯
  10. Can be directly used in HTML special character table Unicode character set
  11. C++使用libcurl进行http通讯
  12. Java traverses list < string > and takes out the string elements in it, and splices them with ","
  13. Self taught web front end more than two months, talk about my harvest and progress, learning summary
  14. vue 中使用 css 变量
  15. 深入了解React中state和props的更新
  16. 百度分享不支持https的解决方案
  17. [practical] ABAP mail sending (HTML + attachment)
  18. [practical] ABAP mail sending (HTML + attachment)
  19. Teach you how to select products with less profit and competition in Amazon Product Development (1)
  20. 【微前端】微前端最终章-qiankun指南以及微前端整体探索
  21. 【微前端】微前端最终章-qiankun指南以及微前端整体探索
  22. Vue-Cli 创建vue3项目
  23. Go in the front of the progress of u boot v7.0 U disk boot disk production tools
  24. 使用NTLM的windows身份验证的nginx反向代理
  25. Rust教程:针对JavaScript开发人员的Rust简介
  26. 使用 Serverless Framework 部署个人博客到腾讯云
  27. #研發解決方案#易車前端監控系統
  28. Vue changes localhost to IP address and cannot access
  29. JavaScript进阶学习
  30. HTML5 from entry to proficient, realize annual salary 10W +, zero basic students must see
  31. Vue:vuex状态数据持久化插件vuex-persistedstate
  32. Vue source code analysis - start
  33. Vue -- the child component calls the method of the parent component and passes parameters --- props
  34. React-Native 获取设备当前网络状态 NetInfo
  35. 高性能 Nginx HTTPS 调优 - 如何为 HTTPS 提速 30%
  36. How to learn HTML5? How can Xiaobai start HTML5 quickly?
  37. HTML + CSS detailed tutorial, this article is enough, but also quickly save
  38. JavaScript高级:JavaScript面向对象,JavaScript内置对象,JavaScript BOM,JavaScript封装
  39. Why Vue uses asynchronous rendering
  40. JavaScript高级:JavaScript面向对象,JavaScript内置对象,JavaScript BOM,JavaScript封装
  41. vue判断elementui中el-form是否更新变化,变化就提示是否保存,没变就直接离开
  42. 算法题:两数之和——JavaScript及Java实现
  43. 高性能 Nginx HTTPS 调优
  44. Why Vue uses asynchronous rendering
  45. day 31 jQuery进阶
  46. day 30 jQuery
  47. CSS whimsy -- using background to create all kinds of wonderful backgrounds
  48. Why are more and more people learning front end?
  49. What do you do with 4K front-end development?
  50. 8 years of front-end development knowledge precipitation (do not know how many words, keep writing it...)
  51. What is the annual salary of a good web front end?
  52. Front end novice tutorial! How to get started with web front end
  53. Will the front end have a future?
  54. Is the front end hard to learn?
  55. Seven new Vue combat skills to improve efficiency in 2021!
  56. Is front end learning difficult?
  57. How about the process of Web front-end development and self-study?
  58. Front end learning route from zero basis to proficient
  59. What is the basis of learning front end?
  60. What knowledge points need to be learned for self-study front end? How long can I become a front-end Engineer?