java - 在 Spring WebFlux webclient 中设置超时

标签 java spring spring-boot timeout spring-webflux

我正在使用 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/

相关文章:

java - 上传多部分文件时出现 FileNotFoundException - Spring boot

spring-boot - springboot fat jar 的 graalvm native 镜像在运行时抛出 NoSuchMethodException xxx.<init>()

java - Integers.parseint(在扫描仪输入上),我也想进行限制,以便用户只能输入 0 到 100 范围内的数字

java - 从 fat jar 启动 Dataflow 作业时暂存包时出错

java - 映射 Java ArrayList<CustomClass> 和 Flex ArrayCollection

带有 MySql 和 Maven 的 Spring Data JPA 简单 Web 示例

spring - 使用 Spring-Boot 启动 Tomcat 上下文时出错 : java. lang.ClassNotFoundException : org. apache.tomcat.dbcp.dbcp.BasicDataSource

java - java 中的 Log4j 日志级别

java - 是否可以编写测试事务代码的事务单元测试?

java - Redis中如何实现Round-Robin?