【发布时间】:2020-10-01 09:44:15
【问题描述】:
我正在使用 webclient 进行其余调用,我需要的是,如果主 URL 第 n 次失败,请在 Secondary URL 上重试。请在下面找到我正在使用的逻辑的示例代码。但是,一旦创建了client,我们似乎就无法更改 URL,即使我更改了 URL,它也不会生效,并且仍然会向初始 URL 发起请求。
ClientHttpConnector connector;//initiate
WebClient webClient = WebClient.builder().clientConnector(connector).build();
WebClient.RequestBodyUriSpec client = webClient.post();
client.uri("http://primaryUrl/").body(BodyInserters.fromObject("hi")).retrieve().bodyToMono(String.class).retryWhen(Retry.anyOf(Exception.class)
.exponentialBackoff(Duration.ofSeconds(2), Duration.ofSeconds(10)).doOnRetry(x ->
{
if (x.iteration() == 2) {
client.uri("http://fail_over_url/");//this does not work
}
})
.retryMax(2)).subscribe(WebClientTest::logCompletion, WebClientTest::handleError);
有什么方法可以在重试周期的中间更改 URL 吗?
【问题讨论】:
标签: java spring spring-boot spring-webflux