英文:
How to avoid hardcode in every class WebClient retryWhen
问题
我想实现重试机制,我做了类似这样的事情:
public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
.onRetryExhaustedThrow(
(retryBackoffSpec, retrySignal) -> new TimeoutException(
retrySignal.failure().getMessage()));
然后在这里使用了这个方法:
public List<A> findByTimestamp(LocalDate localDate) {
return webClient.get()
.uri(bProp.getPath())
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(bData.class)
.retryWhen(fixedRetry)
.map(this::toC)
.collectList()
.block();
}
但我想创建一个通用的方法,以便在整个应用程序中都可以使用它,以免在所有类中都编写第一个方法,我应该如何更高效地做到这一点?
英文:
I want to implement retry mechanism and I did something like this:
public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
.onRetryExhaustedThrow(
(retryBackoffSpec, retrySignal) -> new TimeoutException(
retrySignal.failure().getMessage()));
And used this method here:
public List<A> findByTimestamp(LocalDate localDate) {
return webClient.get()
.uri(bProp.getPath())
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(bData.class)
.retryWhen(fixedRetry)
.map(this::toC)
.collectList()
.block();
}
But I want to create a generic one to use it across all application to not write first method in all classes, how can I do this more efficiently?
答案1
得分: 0
我会将代码部分进行翻译,只返回翻译好的结果:
我会将 webClient
注册为一个 bean 在配置类中,并在那里实现重试逻辑,然后在需要使用 webClient
的地方进行自动装配,而不是显式地创建一个新对象。我在一个类似的问题的答案中找到了一个可能有帮助的回答。
我现在没有时间去检查这段代码是否有效,但我是指以下的代码逻辑:
@Configuration
public class WebClientConfiguration {
@Bean
public WebClient retryWebClient(WebClient.Builder builder) {
return builder.baseUrl("http://localhost:8080")
.filter((request, next) -> next.exchange(request)
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(5))
.onRetryExhaustedThrow(
(retryBackoffSpec, retrySignal) -> new TimeoutException(
retrySignal.failure().getMessage()))))
.build();
}
}
然后,在任何需要的地方进行自动装配:
@Autowired
private WebClient webClient;
英文:
I'd register the webClient as a bean in a configuration class and implement the retry logic there and then autowire it where you need the webClient instead of creating a new object explicitly. I found an answer to a similiar question that might be helpful.
I don't have time to check if this code works but I mean something along these lines:
@Configuration
public class WebClientConfiguration {
@Bean
public WebClient retryWebClient(WebClient.Builder builder) {
return builder.baseUrl("http://localhost:8080")
.filter((request, next) -> next.exchange(request)
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(5))
.onRetryExhaustedThrow(
(retryBackoffSpec, retrySignal) -> new TimeoutException(
retrySignal.failure().getMessage()))))
.build();
}
}
And then just autowire it anywhere you need:
@Autowired
private WebClient webClient;
答案2
得分: 0
以下是翻译好的内容:
我决定实现这个的方式如下:
public class RetryWebClient {
public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
.onRetryExhaustedThrow(
(retryBackoffSpec, retrySignal) -> new TimeoutException(
retrySignal.failure().getMessage()));
private RetryWebClient() {}
}
然后在 retryWhen
中调用它:
.retryWhen(RetryWebClient.fixedRetry)
英文:
The way that I decided to implement this is as follows:
public class RetryWebClient {
public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
.onRetryExhaustedThrow(
(retryBackoffSpec, retrySignal) -> new TimeoutException(
retrySignal.failure().getMessage()));
private RetryWebClient() {}
}
And called it in retryWhen
:
.retryWhen(RetryWebClient.fixedRetry)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论