Java特定的WebClient连接超时每个请求超时

发布于 2025-02-13 09:06:48 字数 67 浏览 1 评论 0原文

我必须调用具有WebClient的不同连接超时的不同URL。但是我发现我们只能在全球范围内设置连接超时,而不是每个倒置。

I have to call different urls with different connection timeout with webclient. But i found that we can set connect timeout globally only and not per resquest .. what to do please to set this timeout on a request without creating a new weblient each time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生路 2025-02-20 09:06:48

使用 超时() 单声道

webClient.post()
         .uri(...)                    
         .retrieve()
         .bodyToMono(...) 
         .subscribeOn(Schedulers.boundedElastic())
         .timeout(Duration.ofSeconds(60))
         .block();

use timeout() of Mono

webClient.post()
         .uri(...)                    
         .retrieve()
         .bodyToMono(...) 
         .subscribeOn(Schedulers.boundedElastic())
         .timeout(Duration.ofSeconds(60))
         .block();
两相知 2025-02-20 09:06:48

您需要每个URL实例化一个WebClient。对于每个WebClient,您可以设置连接超时:

HttpClient httpClient = HttpClient.create()
  .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
WebClient client = WebClient.builder()
  .baseUrl("http://yourendpoint:8080")
  .clientConnector(new ReactorClientHttpConnector(httpClient))
  .build();

然后将此网络电量重复使用为已配置的URL的所有调用。

You need to instantiate one webclient per url. For each webclient, you can set the connection timeout :

HttpClient httpClient = HttpClient.create()
  .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
WebClient client = WebClient.builder()
  .baseUrl("http://yourendpoint:8080")
  .clientConnector(new ReactorClientHttpConnector(httpClient))
  .build();

Then reuse this webclient for all the calls to the configured url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文