gpt4 book ai didi

java - 在 Spring WebFlux webclient 中设置超时

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:06:46 25 4
gpt4 key购买 nike

我正在使用 Spring Webflux WebClient 从我的 Spring 引导应用程序进行 REST 调用。并且每次都在 30 秒内超时。

这是我尝试在 Spring webfulx 的 WebClient 中设置套接字超时的一些代码。

 - ReactorClientHttpConnector connector = new ReactorClientHttpConnector(options -> options
.option(ChannelOption.SO_TIMEOUT, 600000).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 600000));
- ReactorClientHttpConnector connector = new ReactorClientHttpConnector(
options -> options.afterChannelInit(chan -> {
chan.pipeline().addLast(new ReadTimeoutHandler(600000));
}));
- ReactorClientHttpConnector connector1 = new ReactorClientHttpConnector(options -> options
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 600000).afterNettyContextInit(ctx -> {
ctx.addHandlerLast(new ReadTimeoutHandler(600000, TimeUnit.MILLISECONDS));
}));

并尝试使用“clientConnector”方法在“WebClient”中添加上述连接器设置。

并且还尝试如下设置超时:

webClient.get().uri(builder -> builder.path("/result/{name}/sets")
.queryParam("q", "kind:RECORDS")
.queryParam("offset", offset)
.queryParam("limit", RECORD_COUNT_LIMIT)
.build(name))
.header(HttpHeaders.AUTHORIZATION, accessToken)
.exchange().timeout(Duration.ofMillis(600000))
.flatMap(response -> handleResponse(response, name, offset));

以上选项都不适合我。

我正在使用 org.springframework.boot:spring-boot-gradle-plugin:2.0.0.M7,它内部依赖于 org.springframework:spring-webflux:5.0.2.RELEASE。

请在这里提出建议,如果我在这里做错了什么,请告诉我。

最佳答案

我已尝试重现该问题,但无法重现。使用 reactor-netty 0.7.5.RELEASE。

我不确定你说的是哪个超时。

可以使用 ChannelOption.CONNECT_TIMEOUT_MILLIS 配置连接超时。我在“正在连接”日志消息和实际错误之间有 10 秒的时间:

WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(options -> options
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)))
.build();

webClient.get().uri("http://10.0.0.1/resource").exchange()
.doOnSubscribe(subscription -> logger.info("connecting"))
.then()
.doOnError(err -> logger.severe(err.getMessage()))
.block();

如果你在谈论读/写超时,那么你可以看看 Netty 的 ReadTimeoutHandlerWriteTimeoutHandler

一个完整的例子可能是这样的:

ReactorClientHttpConnector connector = new ReactorClientHttpConnector(options ->
options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10)
.onChannelInit(channel -> {
channel.pipeline().addLast(new ReadTimeoutHandler(10))
.addLast(new WriteTimeoutHandler(10));
return true;
}).build());

从 Reactor Netty 0.8 和 Spring Framework 5.1 开始,配置现在看起来像这样:

TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(10))
.addHandlerLast(new WriteTimeoutHandler(10)));
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.build();

也许将以下内容添加到您的 application.properties 将提供有关在 HTTP 级别发生的事情的更多信息:

logging.level.reactor.ipc.netty.channel.ContextHandler=debug
logging.level.reactor.ipc.netty.http.client.HttpClient=debug

关于java - 在 Spring WebFlux webclient 中设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48992992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com