我正在使用 webClient 进行其余调用,我需要的是,如果主 URL 因特定错误代码而失败,请使用辅助 URL 重试下一次。请在下面找到我正在使用的逻辑的示例代码。但是,一旦创建了客户端,我们似乎就无法更改 URL,即使我更改了 URL,它也不会生效,并且仍然会向初始 URL 发起请求。
public WebClient getWebClient(String url) {
return WebClient.builder()
.baseUrl(url)
.defaultHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.build();
}
public void callRestService(Request re){
WebClient webClient = getWebClient(baseUrl);
final RetrySpec retrySpec = Retry.max(3).doBeforeRetry(retrySignal -> {
if("408".equalsIgnoreCase((retrySignal.failure()).getRawStatusCode())) {
webClient = this.getWebClient(failSafeUrl);
}
});
return webClient.post()
.uri("/execute")
.body(Mono.just(re), Request.class)
.retrieve()
.bodyToMono(Response.class)
.defaultIfEmpty(new Response())
.retryWhen(retrySpec)
.onErrorMap(WebClientResponseException.class, EXceptionUtil::handleClientResponseError)
.block();
}