在 Spring WebFlux webclient 中设置超时

2023-11-22

我正在使用 Spring Webflux WebClient 从我的 Spring boot 应用程序进行 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的ReadTimeoutHandler and WriteTimeoutHandler.

完整的示例可能如下所示:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Spring WebFlux webclient 中设置超时 的相关文章

随机推荐