配置
创建WebClient
最简单的方式是通过其中一个静态工厂方法:
-
WebClient.create()
-
WebClient.create(String baseUrl)
您也可以使用WebClient.builder()
来进一步设置选项:
-
uriBuilderFactory
: 自定义UriBuilderFactory
作为基本URL。 -
defaultUriVariables
: 在扩展URI模板时使用的默认值。 -
defaultHeader
: 每个请求的标头。 -
defaultCookie
: 每个请求的Cookie。 -
defaultRequest
: 自定义每个请求的Consumer
。 -
filter
: 每个请求的客户端过滤器。 -
exchangeStrategies
: HTTP消息读取器/写入器的自定义。 -
clientConnector
: HTTP客户端库设置。 -
observationRegistry
: 用于启用可观测性支持的注册表。 -
observationConvention
: 用于提取记录观测数据的可选自定义约定。
例如:
-
Java
-
Kotlin
WebClient client = WebClient.builder()
.codecs(configurer -> ... )
.build();
val webClient = WebClient.builder()
.codecs { configurer -> ... }
.build()
构建完成后,WebClient
是不可变的。但是,您可以克隆它并构建修改后的副本,如下所示:
-
Java
-
Kotlin
WebClient client1 = WebClient.builder()
.filter(filterA).filter(filterB).build();
WebClient client2 = client1.mutate()
.filter(filterC).filter(filterD).build();
// client1有filterA, filterB
// client2有filterA, filterB, filterC, filterD
val client1 = WebClient.builder()
.filter(filterA).filter(filterB).build()
val client2 = client1.mutate()
.filter(filterC).filter(filterD).build()
// client1有filterA, filterB
// client2有filterA, filterB, filterC, filterD
MaxInMemorySize
编解码器在内存中缓冲数据时具有限制,以避免应用程序内存问题。默认情况下,这些限制设置为256KB。如果这不够,您将收到以下错误:
org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer
要更改默认编解码器的限制,请使用以下方法:
-
Java
-
Kotlin
WebClient webClient = WebClient.builder()
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(2 * 1024 * 1024))
.build();
val webClient = WebClient.builder()
.codecs { configurer -> configurer.defaultCodecs().maxInMemorySize(2 * 1024 * 1024) }
.build()
Reactor Netty
要自定义 Reactor Netty 设置,请提供预配置的 HttpClient
:
-
Java
-
Kotlin
HttpClient httpClient = HttpClient.create().secure(sslSpec -> ...);
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
val httpClient = HttpClient.create().secure { ... }
val webClient = WebClient.builder()
.clientConnector(ReactorClientHttpConnector(httpClient))
.build()
资源
默认情况下,HttpClient
参与全局 Reactor Netty 资源,包括事件循环线程和连接池。这是推荐的模式,因为固定的、共享的资源更适合事件循环并发。在此模式下,全局资源保持活动状态直到进程退出。
如果服务器与进程同步,通常不需要显式关闭。但是,如果服务器可以在进程内启动或停止(例如,作为 WAR 部署的 Spring MVC 应用程序),您可以声明一个类型为 ReactorResourceFactory
的 Spring 管理的 bean,其中 globalResources=true
(默认值)以确保在 Spring ApplicationContext
关闭时关闭 Reactor Netty 全局资源,如下例所示:
-
Java
-
Kotlin
@Bean
public ReactorResourceFactory reactorResourceFactory() {
return new ReactorResourceFactory();
}
@Bean
fun reactorResourceFactory() = ReactorResourceFactory()
您还可以选择不参与全局 Reactor Netty 资源。但是,在此模式下,您需要确保所有 Reactor Netty 客户端和服务器实例使用共享资源,如下例所示:
-
Java
-
Kotlin
@Bean
public ReactorResourceFactory resourceFactory() {
ReactorResourceFactory factory = new ReactorResourceFactory();
factory.setUseGlobalResources(false); (1)
return factory;
}
@Bean
public WebClient webClient() {
Function<HttpClient, HttpClient> mapper = client -> {
// 进一步的自定义...
};
ClientHttpConnector connector =
new ReactorClientHttpConnector(resourceFactory(), mapper); (2)
return WebClient.builder().clientConnector(connector).build(); (3)
}
1 | 创建独立于全局资源的资源。 |
2 | 使用带有资源工厂的 ReactorClientHttpConnector 构造函数。 |
3 | 将连接器插入 WebClient.Builder 中。 |
@Bean
fun resourceFactory() = ReactorResourceFactory().apply {
isUseGlobalResources = false (1)
}
@Bean
fun webClient(): WebClient {
val mapper: (HttpClient) -> HttpClient = {
// 进一步的自定义...
}
val connector = ReactorClientHttpConnector(resourceFactory(), mapper) (2)
return WebClient.builder().clientConnector(connector).build() (3)
}
1 | 创建独立于全局资源的资源。 |
2 | 使用带有资源工厂的 ReactorClientHttpConnector 构造函数。 |
3 | 将连接器插入 WebClient.Builder 中。 |
超时
要配置连接超时:
-
Java
-
Kotlin
import io.netty.channel.ChannelOption;
HttpClient httpClient = HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
import io.netty.channel.ChannelOption
val httpClient = HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
val webClient = WebClient.builder()
.clientConnector(ReactorClientHttpConnector(httpClient))
.build();
要配置读取或写入超时:
-
Java
-
Kotlin
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
HttpClient httpClient = HttpClient.create()
.doOnConnected(conn -> conn
.addHandlerLast(new ReadTimeoutHandler(10))
.addHandlerLast(new WriteTimeoutHandler(10)));
// 创建 WebClient...
import io.netty.handler.timeout.ReadTimeoutHandler
import io.netty.handler.timeout.WriteTimeoutHandler
val httpClient = HttpClient.create()
.doOnConnected { conn -> conn
.addHandlerLast(ReadTimeoutHandler(10))
.addHandlerLast(WriteTimeoutHandler(10))
}
// 创建 WebClient...
要为所有请求配置响应超时:
-
Java
-
Kotlin
HttpClient httpClient = HttpClient.create()
.responseTimeout(Duration.ofSeconds(2));
// 创建 WebClient...
val httpClient = HttpClient.create()
.responseTimeout(Duration.ofSeconds(2));
// 创建 WebClient...
要为特定请求配置响应超时:
-
Java
-
Kotlin
WebClient.create().get()
.uri("https://example.org/path")
.httpRequest(httpRequest -> {
HttpClientRequest reactorRequest = httpRequest.getNativeRequest();
reactorRequest.responseTimeout(Duration.ofSeconds(2));
})
.retrieve()
.bodyToMono(String.class);
WebClient.create().get()
.uri("https://example.org/path")
.httpRequest { httpRequest: ClientHttpRequest ->
val reactorRequest = httpRequest.getNativeRequest<HttpClientRequest>()
reactorRequest.responseTimeout(Duration.ofSeconds(2))
}
.retrieve()
.bodyToMono(String::class.java)
JDK HttpClient
以下示例展示了如何自定义JDK HttpClient
:
-
Java
-
Kotlin
HttpClient httpClient = HttpClient.newBuilder()
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.build();
ClientHttpConnector connector =
new JdkClientHttpConnector(httpClient, new DefaultDataBufferFactory());
WebClient webClient = WebClient.builder().clientConnector(connector).build();
val httpClient = HttpClient.newBuilder()
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.build()
val connector = JdkClientHttpConnector(httpClient, DefaultDataBufferFactory())
val webClient = WebClient.builder().clientConnector(connector).build()
Jetty
以下示例展示了如何自定义Jetty HttpClient
设置:
-
Java
-
Kotlin
HttpClient httpClient = new HttpClient();
httpClient.setCookieStore(...);
WebClient webClient = WebClient.builder()
.clientConnector(new JettyClientHttpConnector(httpClient))
.build();
val httpClient = HttpClient()
httpClient.cookieStore = ...
val webClient = WebClient.builder()
.clientConnector(JettyClientHttpConnector(httpClient))
.build();
默认情况下,HttpClient
会创建自己的资源(Executor
、ByteBufferPool
、Scheduler
),这些资源会保持活动状态直到进程退出或调用stop()
方法。
您可以在多个Jetty客户端(和服务器)实例之间共享资源,并通过声明一个Spring管理的JettyResourceFactory
类型的bean来确保在Spring ApplicationContext
关闭时关闭资源,如下例所示:
-
Java
-
Kotlin
@Bean
public JettyResourceFactory resourceFactory() {
return new JettyResourceFactory();
}
@Bean
public WebClient webClient() {
HttpClient httpClient = new HttpClient();
// 进一步定制...
ClientHttpConnector connector =
new JettyClientHttpConnector(httpClient, resourceFactory()); (1)
return WebClient.builder().clientConnector(connector).build(); (2)
}
1 | 使用带有资源工厂的JettyClientHttpConnector 构造函数。 |
2 | 将连接器插入WebClient.Builder 中。 |
@Bean
fun resourceFactory() = JettyResourceFactory()
@Bean
fun webClient(): WebClient {
val httpClient = HttpClient()
// 进一步定制...
val connector = JettyClientHttpConnector(httpClient, resourceFactory()) (1)
return WebClient.builder().clientConnector(connector).build() (2)
}
1 | 使用带有资源工厂的JettyClientHttpConnector 构造函数。 |
2 | 将连接器插入WebClient.Builder 中。 |
HttpComponents
以下示例展示了如何自定义Apache HttpComponents HttpClient
设置:
-
Java
-
Kotlin
HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
clientBuilder.setDefaultRequestConfig(...);
CloseableHttpAsyncClient client = clientBuilder.build();
ClientHttpConnector connector = new HttpComponentsClientHttpConnector(client);
WebClient webClient = WebClient.builder().clientConnector(connector).build();
val client = HttpAsyncClients.custom().apply {
setDefaultRequestConfig(...)
}.build()
val connector = HttpComponentsClientHttpConnector(client)
val webClient = WebClient.builder().clientConnector(connector).build()