How to set a timeout in Spring 5 WebFlux WebClient

前端 未结 7 2001
暗喜
暗喜 2020-12-01 06:30

I\'m trying to set timeout on my WebClient, here is the current code :

SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManag         


        
7条回答
  •  感动是毒
    2020-12-01 07:09

    Based on the above comment if you want to add a Socket Timeout just add it as another option in the same timeoutClient.

    TcpClient timeoutClient = TcpClient.create()
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, SECONDS*10) //Connect Timeout
        .option(ChannelOption.SO_TIMEOUT,1000) // Socket Timeout
        .doOnConnected(
            c -> c.addHandlerLast(new ReadTimeoutHandler(SECONDS))
                  .addHandlerLast(new WriteTimeoutHandler(SECONDS)));
    return webClientBuilder.baseUrl(YOUR_URL)
           .clientConnector(new ReactorClientHttpConnector(HttpClient.from(timeoutClient)))
           .build();
    

提交回复
热议问题