Set connection timeout using Spring Webflux Reactive WebClient

后端 未结 2 525
暗喜
暗喜 2021-01-06 06:10

What is the correct way to set a (connection) timeout for the (default) WebClient?

Is it enough to just use Mono#timeout(Duration) method on the resulti

2条回答
  •  不要未来只要你来
    2021-01-06 06:57

    As of Reactor Netty 0.8 and Spring Framework 5.1, you can set connection, read & write timeouts like the following:

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

提交回复
热议问题