跳到主要内容
版本:7.0.3

配置

Hunyuan 7b 中英对照 Configuration

创建 WebClient 的最简单方法是通过其中一个静态工厂方法:

  • WebClient.create()
  • WebClient.create(String baseUrl)

您还可以使用 WebClient.builder() 并添加更多选项:

  • uriBuilderFactory:自定义的 UriBuilderFactory,用作基础 URL。
  • defaultUriVariables:在扩展 URI 模板时使用的默认值。
  • defaultHeader:每次请求的请求头。
  • defaultCookie:每次请求的 Cookie。
  • defaultApiVersion:每次请求的 API 版本。
  • defaultRequest:用于自定义每次请求的 Consumer
  • filter:每次请求的客户端过滤器。
  • exchangeStrategies:HTTP 消息读写器的自定义设置。
  • clientConnector:HTTP 客户端库的配置。
  • apiVersionInserter:用于在请求中插入 API 版本值的组件。
  • observationRegistry:用于启用 可观测性支持 的注册表。
  • observationConvention一个可选的自定义约定,用于提取记录的观测数据的元数据

例如:

WebClient client = WebClient.builder()
.codecs(configurer -> ... )
.build();

一旦构建完成,WebClient 就是不可变的。但是,你可以按照以下方法克隆它并构建一个修改后的副本:

WebClient client1 = WebClient.builder()
.filter(filterA).filter(filterB).build();

WebClient client2 = client1.mutate()
.filter(filterC).filter(filterD).build();

// client1 has filterA, filterB

// client2 has filterA, filterB, filterC, filterD

MaxInMemorySize

为了防止应用程序出现内存问题,编解码器在内存中缓冲数据时存在限制。默认情况下,这些限制被设置为256KB。如果这个容量不够,你将会遇到以下错误:

org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer

要更改默认编解码器的限制,请使用以下方法:

WebClient webClient = WebClient.builder()
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(2 * 1024 * 1024))
.build();

Reactor Netty

要自定义 Reactor Netty 设置,请提供一个预配置的 HttpClient

HttpClient httpClient = HttpClient.create().secure(sslSpec -> ...);

WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();

资源

默认情况下,HttpClient 会参与全局 Reactor Netty 资源的分配,这些资源存储在 reactor.netty.http.HttpResources 中,包括事件循环线程和连接池。这是推荐的模式,因为对于事件循环的并发处理来说,固定、共享的资源更为理想。在这种模式下,全局资源会一直保持活跃状态,直到进程退出。

如果服务器与进程是同步运行的,通常没有必要进行显式的关闭操作。然而,如果服务器可以在进程中启动或停止(例如,作为WAR部署的Spring MVC应用程序),你可以声明一个类型为ReactorResourceFactoryglobalResources=true(默认值)的Spring管理bean,以确保当Spring的ApplicationContext被关闭时,Reactor Netty的全局资源也会被关闭,如下例所示:

@Bean
public ReactorResourceFactory reactorResourceFactory() {
return new ReactorResourceFactory();
}

你也可以选择不参与全局Reactor Netty资源的共享。然而,在这种模式下,确保所有Reactor Netty客户端和服务器实例使用共享资源的责任就落到了你身上,如下例所示:

@Bean
public ReactorResourceFactory resourceFactory() {
ReactorResourceFactory factory = new ReactorResourceFactory();
factory.setUseGlobalResources(false); 1
return factory;
}

@Bean
public WebClient webClient() {

FUNCTION<HttpClient, HttpClient> mapper = client -> {
// Further customizations...
};

clientHttpConnector connector =
new ReactorClientHttpConnector(resourceFactory(), mapper); 2

return WebClient.builder().clientConnector(connector).build(); 3
}
  • 创建独立于全局资源的资源。

  • 使用 ReactorClientHttpConnector 构造函数与资源工厂一起使用。

  • 将连接器插入到 WebClient.Builder 中。

超时

要配置连接超时:

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.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;

HttpClient httpClient = HttpClient.create()
.doOnConnected(conn -> conn
.addHandlerLast(new ReadTimeoutHandler(10))
.addHandlerLast(new WriteTimeoutHandler(10)));

// Create WebClient...

要为所有请求配置响应超时时间:

HttpClient httpClient = HttpClient.create()
.responseTimeout(Duration.ofSeconds(2));

// Create WebClient...

要为特定请求配置响应超时时间:

WebClient.create().get()
.uri("https://example.org/path")
.httpRequest(httpRequest -> {
HttpClientRequest reactorRequest = httpRequest.getNativeRequest();
reactorRequest.responseTimeout(Duration.ofSeconds(2));
})
.retrieve()
.bodyToMono(String.class);

JDK HttpClient

以下示例展示了如何自定义JDK的HttpClient

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();

Jetty

以下示例展示了如何自定义Jetty的HttpClient设置:

HttpClient httpClient = new HttpClient();
httpClient.setCookieStore(...);

WebClient webClient = WebClient.builder()
.clientConnector(new JettyClientHttpConnector(httpClient))
.build();

默认情况下,HttpClient 会创建自己的资源(ExecutorByteBufferPoolScheduler),这些资源会保持活跃状态,直到进程退出或调用 stop() 方法为止。

你可以在多个Jetty客户端(和服务器)实例之间共享资源,并通过声明一个由Spring管理的类型为JettyResourceFactory的bean来确保当Spring的ApplicationContext关闭时,这些资源也能被关闭,如下例所示:

@Bean
public JettyResourceFactory resourceFactory() {
return new JettyResourceFactory();
}

@Bean
public WebClient webClient() {

HttpClient httpClient = new HttpClient();
// Further customizations...

_ClientHttpConnector connector =
new JettyClientHttpConnector(httpClient, resourceFactory()); 1

return WebClient.builder().clientConnector(connector).build(); 2
}
  • 使用 JettyClientHttpConnector 构造函数与资源工厂(resource factory)一起使用。

  • 将该连接器(connector)插入到 WebClient.Builder 中。

HttpComponents

以下示例展示了如何自定义 Apache HttpComponents 的 HttpClient 设置:

HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
clientBuilder.setDefaultRequestConfig(...);
CloseableHttpAsyncClient client = clientBuilder.build();

ClientHttpConnector connector = new HttpComponentsClientHttpConnector(client);

WebClient webClient = WebClient.builder().clientConnector(connector).build();