最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中啟用和測(cè)試HTTP/2的幾種方法

 更新時(shí)間:2025年10月22日 09:16:15   作者:學(xué)亮編程手記  
HTTP/2即超文本傳輸協(xié)議第二版,使用于萬(wàn)維網(wǎng),HTTP/2主要基于SPDY協(xié)議,通過(guò)對(duì)HTTP頭字段進(jìn)行數(shù)據(jù)壓縮、對(duì)數(shù)據(jù)傳輸采用多路復(fù)用和增加服務(wù)端推送等舉措,本文給大家介紹了SpringBoot中啟用和測(cè)試HTTP/2的幾種方法,需要的朋友可以參考下

前置條件

在開(kāi)始之前,需要注意:

  • JDK 版本:需要 JDK 9+(推薦 JDK 11 或 17)
  • SSL 證書(shū):HTTP/2 在瀏覽器中需要 HTTPS,所以需要 SSL 證書(shū)

方法一:使用內(nèi)置 Undertow 服務(wù)器(推薦)

Undertow 原生支持 HTTP/2,配置簡(jiǎn)單,性能優(yōu)秀。

1. 添加依賴(lài)

<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <!-- 排除默認(rèn)的 Tomcat -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <!-- 使用 Undertow 作為服務(wù)器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
</dependencies>

2. 生成 SSL 證書(shū)

首先創(chuàng)建一個(gè)自簽名證書(shū)(用于測(cè)試):

# 在項(xiàng)目根目錄執(zhí)行
keytool -genkey -alias spring-boot-http2 -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650

# 輸入信息(測(cè)試時(shí)都可以用默認(rèn)值):
# 密碼:password
# 姓名:localhost

3. 配置 application.yml

# application.yml
server:
  port: 8443
  ssl:
    key-store: classpath:keystore.p12
    key-store-password: password
    key-store-type: PKCS12
    key-alias: spring-boot-http2
  # HTTP/2 配置
  http2:
    enabled: true
  # Undertow 特定配置
  undertow:
    threads:
      worker: 64
      io: 4

4. 創(chuàng)建測(cè)試 Controller

// Http2DemoController.java
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.stream.LongStream;

@RestController
@RequestMapping("/api")
public class Http2DemoController {
    
    // 普通接口
    @GetMapping("/hello")
    public Map<String, String> hello() {
        return Map.of(
            "message", "Hello HTTP/2!",
            "timestamp", new Date().toString(),
            "protocol", "HTTP/2 with Undertow"
        );
    }
    
    // 模擬大量數(shù)據(jù)返回,展示 HTTP/2 的多路復(fù)用優(yōu)勢(shì)
    @GetMapping("/large-data")
    public Map<String, Object> getLargeData() {
        List<Map<String, Object>> data = new ArrayList<>();
        
        for (int i = 0; i < 1000; i++) {
            data.add(Map.of(
                "id", i,
                "name", "Item " + i,
                "value", Math.random() * 1000,
                "timestamp", System.currentTimeMillis() + i
            ));
        }
        
        return Map.of(
            "items", data,
            "total", data.size(),
            "server", "Undertow with HTTP/2"
        );
    }
    
    // 計(jì)算密集型任務(wù)
    @GetMapping("/calculate")
    public Map<String, Object> calculate() {
        long startTime = System.currentTimeMillis();
        
        // 模擬計(jì)算任務(wù)
        long sum = LongStream.range(0, 10_000_000)
                .parallel()
                .sum();
        
        long endTime = System.currentTimeMillis();
        
        return Map.of(
            "result", sum,
            "calculationTime", endTime - startTime + "ms",
            "protocol", "HTTP/2"
        );
    }
}

5. 創(chuàng)建主應(yīng)用類(lèi)

// SpringBootHttp2Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import io.undertow.UndertowOptions;

@SpringBootApplication
public class SpringBootHttp2Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootHttp2Application.class, args);
    }
    
    @Bean
    public UndertowServletWebServerFactory undertowServletWebServerFactory() {
        UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
        
        factory.addBuilderCustomizers(builder -> {
            // 啟用 HTTP/2
            builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
            // 其他優(yōu)化配置
            builder.setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, false);
            builder.setServerOption(UndertowOptions.ALWAYS_SET_DATE, true);
        });
        
        return factory;
    }
}

方法二:使用 Tomcat(需要 ALPN)

Tomcat 9+ 也支持 HTTP/2,但配置稍復(fù)雜。

1. 依賴(lài)配置(使用 Tomcat)

<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- 對(duì)于 JDK 9+,需要包含 tomcat-embed-core -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
    </dependency>
</dependencies>

2. Tomcat 配置

# application.yml
server:
  port: 8443
  ssl:
    key-store: classpath:keystore.p12
    key-store-password: password
    key-store-type: PKCS12
    key-alias: spring-boot-http2
  http2:
    enabled: true
  # Tomcat 特定配置
  tomcat:
    max-threads: 200
    min-spare-threads: 10

測(cè)試 HTTP/2

1. 啟動(dòng)應(yīng)用

啟動(dòng) Spring Boot 應(yīng)用后,你應(yīng)該看到類(lèi)似日志:

Tomcat started on port(s): 8443 (https) with context path ''
// 或
Undertow started on port(s): 8443 (https) with context path ''

2. 使用 curl 測(cè)試

# 測(cè)試 HTTP/2
curl -k -I --http2 https://localhost:8443/api/hello

# 正常請(qǐng)求
curl -k --http2 https://localhost:8443/api/hello

3. 瀏覽器測(cè)試

  1. 訪(fǎng)問(wèn) https://localhost:8443/api/hello
  2. 由于是自簽名證書(shū),瀏覽器會(huì)顯示不安全警告,選擇"繼續(xù)前往"
  3. 打開(kāi)開(kāi)發(fā)者工具 → Network 標(biāo)簽
  4. 刷新頁(yè)面,在 Protocol 列應(yīng)該看到 h2(HTTP/2)

4. 創(chuàng)建測(cè)試頁(yè)面展示多路復(fù)用優(yōu)勢(shì)

// 添加這個(gè) Controller 來(lái)演示多請(qǐng)求并發(fā)
@Controller
class DemoPageController {
    
    @GetMapping("/")
    public String index() {
        return "index";
    }
}

在 src/main/resources/templates/index.html

<!DOCTYPE html>
<html>
<head>
    <title>HTTP/2 Demo</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <h1>Spring Boot HTTP/2 演示</h1>
    
    <button onclick="testSequential()">順序請(qǐng)求測(cè)試 (模擬 HTTP/1.1)</button>
    <button onclick="testParallel()">并行請(qǐng)求測(cè)試 (HTTP/2 多路復(fù)用)</button>
    
    <div id="result"></div>
    
    <script>
        async function testSequential() {
            const start = Date.now();
            const result = $('#result');
            result.html('測(cè)試中...');
            
            // 順序請(qǐng)求
            for (let i = 0; i < 10; i++) {
                await fetch('/api/calculate')
                    .then(r => r.json());
            }
            
            const duration = Date.now() - start;
            result.html(`順序請(qǐng)求完成: ${duration}ms`);
        }
        
        function testParallel() {
            const start = Date.now();
            const result = $('#result');
            result.html('測(cè)試中...');
            
            // 并行請(qǐng)求
            const promises = [];
            for (let i = 0; i < 10; i++) {
                promises.push(fetch('/api/calculate').then(r => r.json()));
            }
            
            Promise.all(promises).then(() => {
                const duration = Date.now() - start;
                result.html(`并行請(qǐng)求完成: ${duration}ms (HTTP/2 多路復(fù)用優(yōu)勢(shì))`);
            });
        }
    </script>
</body>
</html>

完整配置示例(生產(chǎn)建議)

對(duì)于生產(chǎn)環(huán)境,建議使用如下配置:

# application-prod.yml
server:
  port: 443
  ssl:
    key-store: /etc/ssl/certs/keystore.p12
    key-store-password: ${KEYSTORE_PASSWORD}
    key-store-type: PKCS12
    key-alias: my-production-cert
  http2:
    enabled: true
  compression:
    enabled: true
    mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
    min-response-size: 1024
  servlet:
    session:
      timeout: 30m

# Undertow 生產(chǎn)配置
undertow:
  threads:
    worker: 100  # 根據(jù) CPU 核心數(shù)調(diào)整
    io: 8        # 通常為核心數(shù)

# 日志配置
logging:
  level:
    org.springframework: INFO
    io.undertow: WARN

總結(jié)

  1. 推薦使用 Undertow:配置簡(jiǎn)單,性能優(yōu)秀,原生支持 HTTP/2
  2. 必須配置 SSL:HTTP/2 在瀏覽器中需要 HTTPS
  3. 多路復(fù)用優(yōu)勢(shì):在需要大量并發(fā)請(qǐng)求的場(chǎng)景下,HTTP/2 性能提升明顯
  4. 向后兼容:HTTP/2 不改變 HTTP 語(yǔ)義,現(xiàn)有代碼無(wú)需修改

這個(gè)示例展示了如何在 Spring Boot 中啟用和測(cè)試 HTTP/2,你可以直接運(yùn)行來(lái)體驗(yàn) HTTP/2 的性能優(yōu)勢(shì)。

以上就是SpringBoot中啟用和測(cè)試HTTP/2代碼示例的幾種方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot啟用和測(cè)試HTTP/2代碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Dubbo3和Spring?Boot整合過(guò)程源碼解析

    Dubbo3和Spring?Boot整合過(guò)程源碼解析

    Dubbo首先是提供了一個(gè)單獨(dú)的模塊來(lái)和Spring Boot做整合,利用 Spring Boot自動(dòng)裝配的功能,配置了一堆自動(dòng)裝配的組件,本文介紹Dubbo3和Spring?Boot整合過(guò)程,需要的朋友一起看看吧
    2023-08-08
  • JDK多版本管理工具安裝和使用詳細(xì)教程

    JDK多版本管理工具安裝和使用詳細(xì)教程

    隨著軟件開(kāi)發(fā)環(huán)境的日益復(fù)雜,多版本Java開(kāi)發(fā)工具包(JDK)管理變得尤為重要,下面這篇文章主要介紹了JDK多版本管理工具安裝和使用的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-09-09
  • SpringBoot?注解?@AutoConfiguration?在?2.7?版本中被新增的使用方法詳解

    SpringBoot?注解?@AutoConfiguration?在?2.7?版本中被新增的使用方法詳解

    這篇文章主要介紹了SpringBoot?注解?@AutoConfiguration?在?2.7?版本中被新增(使用方法),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-09-09
  • 在Spring Boot中如何使用Cookies詳析

    在Spring Boot中如何使用Cookies詳析

    這篇文章主要給大家介紹了關(guān)于在Spring Boot中如何使用Cookies的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • SpringBoot重寫(xiě)addResourceHandlers映射文件路徑方式

    SpringBoot重寫(xiě)addResourceHandlers映射文件路徑方式

    這篇文章主要介紹了SpringBoot重寫(xiě)addResourceHandlers映射文件路徑方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java中常用的類(lèi)型轉(zhuǎn)換(推薦)

    Java中常用的類(lèi)型轉(zhuǎn)換(推薦)

    這篇文章主要介紹了Java中常用的類(lèi)型轉(zhuǎn)換(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • 解決Spring?Boot應(yīng)用打包后文件訪(fǎng)問(wèn)問(wèn)題

    解決Spring?Boot應(yīng)用打包后文件訪(fǎng)問(wèn)問(wèn)題

    在Spring Boot項(xiàng)目的開(kāi)發(fā)過(guò)程中,一個(gè)常見(jiàn)的挑戰(zhàn)是如何有效地訪(fǎng)問(wèn)和操作資源文件,本文就來(lái)介紹一下解決Spring?Boot應(yīng)用打包后文件訪(fǎng)問(wèn)問(wèn)題,感興趣的可以了解一下
    2024-01-01
  • javamail 發(fā)送郵件的實(shí)例代碼分享

    javamail 發(fā)送郵件的實(shí)例代碼分享

    今天學(xué)習(xí)了一下JavaMail,javamail發(fā)送郵件確實(shí)是一個(gè)比較麻煩的問(wèn)題。為了以后使用方便,自己寫(xiě)了段代碼,打成jar包,以方便以后使用
    2013-08-08
  • Spring整合Mybatis使用<context:property-placeholder>時(shí)的坑

    Spring整合Mybatis使用<context:property-placeholder>時(shí)的坑

    這篇文章主要介紹了Spring整合Mybatis使用<context:property-placeholder>時(shí)的坑 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Mybatisplus主鍵生成策略算法解析

    Mybatisplus主鍵生成策略算法解析

    這篇文章主要介紹了Mybatisplus主鍵生成策略算法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論

甘洛县| 大兴区| 徐水县| 丹棱县| 当阳市| 农安县| 濮阳县| 沧源| 祁门县| 雷波县| 大埔县| 喀什市| 盘山县| 乌拉特中旗| 武强县| 凯里市| 饶河县| 五峰| 图片| 安福县| 闽侯县| 泰来县| 佳木斯市| 乌拉特后旗| 河西区| 大田县| 阳江市| 上犹县| 原平市| 景谷| 应城市| 家居| 平塘县| 肥西县| 荣成市| 西贡区| 宁乡县| 大宁县| 普兰店市| 富阳市| 连城县|