Reactor 2024.0 版本列车中的 HTTP/3 支持

工程 | Violeta Georgieva | 2024 年 11 月 26 日 | ...

HTTP/3Hypertext Transfer Protocol 的最新主要版本,其规范于 2022 年 6 月最终确定。该版本旨在提高性能、可靠性和安全性。与其前身不同,HTTP/3 使用 QUIC 而不是 TCP 作为其传输层。QUIC 是一种基于 UDP 的多路复用安全传输协议,包含内置的 TLS 1.3 加密,因此 QUIC 默认加密。

要了解有关 HTTP/3 的性能和优势的更多信息,请查看 什么是 HTTP/3

有关浏览器采用情况的信息,请查看 HTTP/3 使用情况分析,其中还提供了不同浏览器使用的 HTTP 版本的原始数据。

Reactor Netty 1.2(Reactor 2024.0 版本列车的一部分)增加了 HTTP/3 的实验性支持。通过这个新版本的 Reactor Netty,您的 Spring Boot 应用程序和 Spring Cloud Gateway 可以配置为支持 HTTP/3

让我们看看如何配置 HTTP/3 支持。

配置 Reactor BOM 版本

Spring Boot 3.4 默认包含 Reactor 2024.0 版本列车!

如果您运行的是旧版本的 Spring Boot,可以通过将 Reactor BOM 升级到 2024.0 来体验这项新功能(截至本文撰写时,2024.0.0 是最新版本)。

Maven

<properties>
    <reactor-bom.version>2024.0.0</reactor-bom.version>
</properties>

Gradle

ext['reactor-bom.version'] = '2024.0.0'

配置 Netty HTTP3 编解码器

您还需要添加 Netty HTTP3 编解码器 的运行时依赖(截至本文撰写时,0.0.28.Final 是最新版本)。

Maven

<dependencies>
    <dependency>
        <groupId>io.netty.incubator</groupId>
        <artifactId>netty-incubator-codec-http3</artifactId>
        <version>0.0.28.Final</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

Gradle

dependencies {
    runtimeOnly 'io.netty.incubator:netty-incubator-codec-http3:0.0.28.Final'
}

服务器端

配置 SSL Bundle

您首先需要提供一个包含应用程序所需配置的 SSL bundle:密钥库、密码套件等。

application.properties

spring.ssl.bundle.jks.server-http3.key.alias=http3
spring.ssl.bundle.jks.server-http3.keystore.location=...
spring.ssl.bundle.jks.server-http3.keystore.password=...
...

application.yml

spring:
  ssl:
    bundle:
      jks:
        server-http3:
          key:
            alias: http3
          keystore:
            location: ...
            password: ...
          ...

配置嵌入式服务器

Spring Boot 提供了配置嵌入式服务器的能力。Spring Cloud Gateway 使用相同的方法来完成此任务。

您可以声明一个 WebServerFactoryCustomizer 组件并获取服务器工厂的访问权限。为了启用 HTTP/3 支持,您需要

默认情况下,Reactor Netty 配置为支持 HTTP/1.1,因此您需要进行更改。

默认情况下,Reactor Netty 不提供任何设置,因为这些设置与应用程序密切相关,因此您必须配置它们:空闲超时、最大流等。

之前配置的 SSL Bundle 可以通过其名称 factory.getSslBundles().getBundle("server-http3") 从服务器工厂获取,并且您可以配置 Http3SslContextSpec

@Component
class Http3NettyWebServerCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Override
    public void customize(NettyReactiveWebServerFactory factory) {
        factory.addServerCustomizers(server -> {
            SslBundle sslBundle = factory.getSslBundles().getBundle("server-http3");
            Http3SslContextSpec sslContextSpec =
                Http3SslContextSpec.forServer(sslBundle.getManagers().getKeyManagerFactory(), sslBundle.getKey().getPassword());

            return server
                    // Configure HTTP/3 protocol
                    .protocol(HttpProtocol.HTTP3)
                    // Configure HTTP/3 SslContext
                    .secure(spec -> spec.sslContext(sslContextSpec))
                    // Configure HTTP/3 settings
                    .http3Settings(spec -> spec
                            .idleTimeout(Duration.ofSeconds(5))
                            .maxData(10_000_000)
                            .maxStreamDataBidirectionalRemote(1_000_000)
                            .maxStreamsBidirectional(100));
        });
    }
}

REST 控制器

您最后需要添加的是一个简单的 hello REST 控制器。REST 控制器不需要任何特定的 HTTP/3 配置!

@RestController
class Http3Controller {

    @GetMapping("/hello")
    String hello() {
        return "Hello HTTP/3!";
    }
}

现在您已准备好进行您的第一个 HTTP/3 请求

curl --http3 https://localhost:8443/hello --verbose
* Connected to localhost (::1) port 8443
* using HTTP/3
* [HTTP/3] [0] OPENED stream for https://localhost:8443/hello
* [HTTP/3] [0] [:method: GET]
* [HTTP/3] [0] [:scheme: https]
* [HTTP/3] [0] [:authority: localhost:8443]
* [HTTP/3] [0] [:path: /hello]
* [HTTP/3] [0] [user-agent: curl]
* [HTTP/3] [0] [accept: */*]
> GET /hello HTTP/3
> Host: localhost:8443
> User-Agent: curl
> Accept: */*
> 
* Request completely sent off
< HTTP/3 200 
< content-type: text/plain;charset=UTF-8
< content-length: 13
< 
* Connection #0 to host localhost left intact
Hello HTTP/3!

spring-webflux-http3 仓库包含完整的示例!

客户端

为客户端配置 HTTP/3 支持的方式与配置服务器端类似!

您需要

  • 指定 HTTP/3 协议。默认情况下,Reactor Netty 配置为支持 HTTP/1.1,因此您需要进行更改。
  • 指定 HTTP/3 设置。默认情况下,Reactor Netty 不提供任何设置,因为这些设置与应用程序密切相关,因此您必须配置它们:空闲超时、最大流等。
import reactor.netty.http.client.HttpClient;

HttpClient client = HttpClient.create()
        // Configure HTTP/3 protocol
        .protocol(HttpProtocol.HTTP3)
        // Configure HTTP/3 settings
        .http3Settings(spec -> spec
                .idleTimeout(Duration.ofSeconds(5))
                .maxData(10_000_000)
                .maxStreamDataBidirectionalLocal(1_000_000));

默认情况下,客户端使用 Reactor Netty 提供的标准 HTTP/3 SSLContext。但是,如果您需要更具体的配置:信任库、密码套件等,您可以像为服务器准备 SSL Bundle 一样准备它,并且可以配置 Http3SslContextSpec

SslBundle sslBundle = factory.getSslBundles().getBundle("client-http3");
Http3SslContextSpec sslContextSpec = Http3SslContextSpec.forClient()
        // Configure TrustStore etc.
        .configure(...);
HttpClient client = HttpClient.create()
        ...
        // Configure HTTP/3 SslContext
        .secure(spec -> spec.sslContext(sslContextSpec));

WebClient

您可以使用 ReactorClientHttpConnector 配置底层的 Reactor Netty HttpClient

@Bean
WebClient http3WebClient(WebClient.Builder builder) {
    HttpClient client = ...;
    return builder.clientConnector(new ReactorClientHttpConnector(client)).build();
}

REST 控制器

您可以创建一个简单的 REST 控制器,利用新的 HTTP/3 配置发起远程调用。REST 控制器不需要任何特定的 HTTP/3 配置!

@RestController
class Http3Controller {

    private final WebClient http3WebClient;

    Http3Controller(WebClient http3WebClient) {
        this.http3WebClient = http3WebClient;
    }

    @GetMapping("/remote")
    Mono<String> remote() {
        return http3WebClient
                .get()
                .uri("https://projectreactor.io/")
                .retrieve()
                .bodyToMono(String.class);
    }
}

spring-webflux-http3 仓库包含完整的示例。

RestClient

您可以使用 ReactorNettyClientRequestFactory 配置底层的 Reactor Netty HttpClient

@Bean
RestClient http3RestClient(RestClient.Builder builder) {
	HttpClient client = ...;
	return builder.requestFactory(new ReactorNettyClientRequestFactory(client)).build();
}

REST 控制器

您可以创建一个简单的 REST 控制器,利用新的 HTTP/3 配置发起远程调用。REST 控制器不需要任何特定的 HTTP/3 配置!

@RestController
class Http3Controller {

    private final RestClient http3RestClient;

    Http3Controller(RestClient http3RestClient) {
        this.http3RestClient = http3RestClient;
    }

    @GetMapping("/remote")
    String remote() {
        return http3RestClient
                .get()
                .uri("https://projectreactor.io/")
                .retrieve()
                .body(String.class);
    }
}

spring-webmvc-http3 仓库包含完整的示例。

现在您已准备好进行您的第一个 HTTP/3 远程调用

curl --http3 https://localhost:8443/remote --verbose
* Connected to localhost (::1) port 8443
* using HTTP/3
* [HTTP/3] [0] OPENED stream for https://localhost:8443/remote
* [HTTP/3] [0] [:method: GET]
* [HTTP/3] [0] [:scheme: https]
* [HTTP/3] [0] [:authority: localhost:8443]
* [HTTP/3] [0] [:path: /remote]
* [HTTP/3] [0] [user-agent: curl/8]
* [HTTP/3] [0] [accept: */*]
> GET /remote HTTP/3
> Host: localhost:8443
> User-Agent: curl/8
> Accept: */*
> 
* Request completely sent off
< HTTP/3 200 
< content-type: text/plain;charset=UTF-8
< content-length: 17138
...

Spring Cloud Gateway

您可以使用 Spring Cloud Gateway 中的 HttpClientCustomizer 配置底层的 Reactor Netty HttpClient。要使用此自定义器,您需要将其注册到 Spring Cloud Gateway 配置中。

@Configuration
class GatewayConfiguration {

    @Bean
    HttpClientCustomizer http3HttpClientCustomizer() {
        return httpClient ->
                httpClient
                        // Configure HTTP/3 protocol
                        .protocol(HttpProtocol.HTTP3)
                        // Configure HTTP/3 settings
                        .http3Settings(spec -> spec.idleTimeout(Duration.ofSeconds(5))
                                .maxData(10_000_000)
                                .maxStreamDataBidirectionalLocal(1_000_000));
    }
}

spring-cloud-gateway-http3 仓库包含完整的示例。

我们希望您会喜欢我们与 HTTP/3 集成的简洁性。请在我们的 GitHub/Twitter 上提供反馈!

获取 Spring 时事通讯

订阅 Spring 时事通讯保持联系

订阅

领先一步

VMware 提供培训和认证,助您快速提升。

了解更多

获取支持

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件,只需一个简单的订阅即可获得。

了解更多

即将举行的活动

查看 Spring 社区所有即将举行的活动。

查看全部