How to set a timeout in Spring 5 WebFlux WebClient

前端 未结 7 1970
暗喜
暗喜 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:15

    The WebFlux WebClient doesn't use Apache Commons HTTP Client. Although you might be able to implement one solution via custom ClientHttpConnector. The existing ReactorClientHttpConnector is based on the Netty. So, consider to use Netty options to configure the client, e.g.:

    ReactorClientHttpConnector connector =
                new ReactorClientHttpConnector(options ->
                        options.option(ChannelOption.SO_TIMEOUT, this.applicationConfig.getHttpClientConnectTimeout()));
    

    or

    .onChannelInit(channel -> channel.config().setConnectTimeoutMillis(this.applicationConfig.getHttpClientConnectTimeout()))
    

    UPDATE

    We also can use ReadTimeoutHandler:

    .onChannelInit(channel -> 
            channel.pipeline()
               .addLast(new ReadTimeoutHandler(this.applicationConfig.getHttpClientConnectTimeout())))
    

提交回复
热议问题