Spring 5 webflux如何在Webclient上设置超时
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 5 webflux如何在Webclient上设置超时相关的知识,希望对你有一定的参考价值。
我正在尝试在我的WebClient上设置超时,这是当前代码:
SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(opt ->
opt.sslContext(sslContext);
HttpClientOptions option = HttpClientOptions.builder().build();
opt.from(option);
);
return WebClient.builder().clientConnector(httpConnector).defaultHeader("Authorization", xxxx)
.baseUrl(this.opusConfig.getBaseURL()).build();
我需要添加超时和汇集策略,我想的是这样的:
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(this.applicationConfig.getHttpClientMaxPoolSize());
cm.setDefaultMaxPerRoute(this.applicationConfig.getHttpClientMaxPoolSize());
cm.closeIdleConnections(this.applicationConfig.getServerIdleTimeout(), TimeUnit.MILLISECONDS);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.applicationConfig.getHttpClientSocketTimeout())
.setConnectTimeout(this.applicationConfig.getHttpClientConnectTimeout())
.setConnectionRequestTimeout(this.applicationConfig.getHttpClientRequestTimeout()).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm).build();
但我无法弄清楚如何在我的webclient中设置httpClient
WebFlux WebClient
不使用Apache Commons HTTP Client。虽然您可以通过自定义ClientHttpConnector
实现一个解决方案。现有的ReactorClientHttpConnector
基于Netty。因此,请考虑使用Netty选项来配置客户端,例如:
ReactorClientHttpConnector connector =
new ReactorClientHttpConnector(options ->
options.option(ChannelOption.SO_TIMEOUT, this.applicationConfig.getHttpClientConnectTimeout()));
要么
.onChannelInit(channel -> channel.config().setConnectTimeoutMillis(this.applicationConfig.getHttpClientConnectTimeout()))
UPDATE
我们也可以使用ReadTimeoutHandler
:
.onChannelInit(channel ->
channel.pipeline()
.addLast(new ReadTimeoutHandler(this.applicationConfig.getHttpClientConnectTimeout())))
要设置读取和连接超时,我使用下面的方法,因为SO_TIMEOUT选项不适用于使用NIO的通道(并提供警告Unknown channel option 'SO_TIMEOUT' for channel '[id: 0xa716fcb2]'
)
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(
options -> options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000)
.compression(true)
.afterNettyContextInit(ctx ->
ctx.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS));
));
return WebClient.builder()
.clientConnector(connector)
.build();
ReactorClientHttpConnector API在版本Spring WebFlux 5.1中已更改。
所以我做了以下(Kotlin语法,基于@joshiste示例):
val tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000)
.doOnConnected connection ->
connection.addHandlerLast(ReadTimeoutHandler(10))
.addHandlerLast(WriteTimeoutHandler(10))
val myWebClient = WebClient.builder()
.clientConnector(ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.baseUrl(myEndPoint)
.build()
这是我的表现(感谢@Artem)
SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(options ->
options.sslContext(sslContext);
options.option(ChannelOption.SO_TIMEOUT, this.applicationConfig.getHttpClientRequestTimeout());
options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.applicationConfig.getHttpClientConnectTimeout());
options.poolResources(PoolResources.fixed("myPool", this.applicationConfig.getHttpClientMaxPoolSize()));
);
return WebClient.builder().clientConnector(httpConnector).defaultHeader("Authorization", "xxxx")
.baseUrl(this.config.getBaseURL()).build();
随着Spring Webflux的更新,这里有一个适用于Java的解决方案(基于kazlin的answer):
TcpClient timeoutClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, SECONDS*1000)
.doOnConnected(
c -> c.addHandlerLast(new ReadTimeoutHandler(SECONDS))
.addHandlerLast(new WriteTimeoutHandler(SECONDS)));
return webClientBuilder.baseUrl(YOUR_URL)
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(timeoutClient)))
.build();
基于上述注释,如果要添加套接字超时,只需将其添加为同一timeoutClient中的另一个选项。
TcpClient timeoutClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, SECONDS*10) //Connect Timeout
.option(ChannelOption.SO_TIMEOUT,1000) // Socket Timeout
.doOnConnected(
c -> c.addHandlerLast(new ReadTimeoutHandler(SECONDS))
.addHandlerLast(new WriteTimeoutHandler(SECONDS)));
return webClientBuilder.baseUrl(YOUR_URL)
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(timeoutClient)))
.build();
以上是关于Spring 5 webflux如何在Webclient上设置超时的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring webflux 应用程序中使用 Spring WebSessionIdResolver 和 Spring Security 5?
如何在 Spring 5 webflux websocket 客户端上更改帧/缓冲区大小
如何使用 Spring Boot 对 WebFlux 进行异常处理?
在8102年的今天,你清楚Spring 5.0的WebFlux吗?