其他分享
首页 > 其他分享> > 使用Spring Webflux Reactive WebClient设置连接超时

使用Spring Webflux Reactive WebClient设置连接超时

作者:互联网

为(默认)WebClient设置(连接)超时的正确方法是什么?

仅仅对生成的Mono(或Flux)使用Mono #timetime(Duration)方法是否足够?或者这是否会导致内存/连接泄漏?

提前致谢!

(Spring 5 webflux how to set a timeout on Webclient的答案不起作用!)

解决方法:

从Reactor Netty 0.8和Spring Framework 5.1开始,您可以设置连接,读取和读取.写下超时如下:

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();

标签:spring-webflux,spring
来源: https://codeday.me/bug/20191008/1871918.html