问题描述
我正在使用 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();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)