領先一步
VMware 提供培訓和認證,助您加速進步。
瞭解更多HTTP/3,超文字傳輸協議的最新主要版本,其規範於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 支援。
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 編解碼器的執行時依賴(截至本文撰寫時,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 捆綁包。
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 捆綁包可以透過其名稱 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));
});
}
}
您需要新增的最後一件事是一個簡單的 hello REST 控制器。REST 控制器不需要任何特定的 HTTP/3 配置!
@RestController
class Http3Controller {
@GetMapping("/hello")
String hello() {
return "Hello HTTP/3!";
}
}
現在您可以進行您的第一個 HTTP/3 請求了。
curl --http3 https://:8443/hello --verbose
* Connected to localhost (::1) port 8443
* using HTTP/3
* [HTTP/3] [0] OPENED stream for https://: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/1.1,因此您需要更改它。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 捆綁包,並配置 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));
您可以使用 ReactorClientHttpConnector 配置底層的 Reactor Netty HttpClient。
@Bean
WebClient http3WebClient(WebClient.Builder builder) {
HttpClient client = ...;
return builder.clientConnector(new ReactorClientHttpConnector(client)).build();
}
您可以建立一個簡單的 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 倉庫包含完整的示例。
您可以使用 ReactorNettyClientRequestFactory 配置底層的 Reactor Netty HttpClient。
@Bean
RestClient http3RestClient(RestClient.Builder builder) {
HttpClient client = ...;
return builder.requestFactory(new ReactorNettyClientRequestFactory(client)).build();
}
您可以建立一個簡單的 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://:8443/remote --verbose
* Connected to localhost (::1) port 8443
* using HTTP/3
* [HTTP/3] [0] OPENED stream for https://: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 中使用 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 倉庫包含完整的示例。