如何避免在每个类中硬编码 WebClient retryWhen。

huangapple go评论91阅读模式
英文:

How to avoid hardcode in every class WebClient retryWhen

问题

我想实现重试机制,我做了类似这样的事情:

  1. public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
  2. .onRetryExhaustedThrow(
  3. (retryBackoffSpec, retrySignal) -> new TimeoutException(
  4. retrySignal.failure().getMessage()));

然后在这里使用了这个方法:

  1. public List<A> findByTimestamp(LocalDate localDate) {
  2. return webClient.get()
  3. .uri(bProp.getPath())
  4. .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
  5. .retrieve()
  6. .bodyToFlux(bData.class)
  7. .retryWhen(fixedRetry)
  8. .map(this::toC)
  9. .collectList()
  10. .block();
  11. }

但我想创建一个通用的方法,以便在整个应用程序中都可以使用它,以免在所有类中都编写第一个方法,我应该如何更高效地做到这一点?

英文:

I want to implement retry mechanism and I did something like this:

  1. public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
  2. .onRetryExhaustedThrow(
  3. (retryBackoffSpec, retrySignal) -&gt; new TimeoutException(
  4. retrySignal.failure().getMessage()));

And used this method here:

  1. public List&lt;A&gt; findByTimestamp(LocalDate localDate) {
  2. return webClient.get()
  3. .uri(bProp.getPath())
  4. .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
  5. .retrieve()
  6. .bodyToFlux(bData.class)
  7. .retryWhen(fixedRetry)
  8. .map(this::toC)
  9. .collectList()
  10. .block();
  11. }

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 的地方进行自动装配,而不是显式地创建一个新对象。我在一个类似的问题的答案中找到了一个可能有帮助的回答。

我现在没有时间去检查这段代码是否有效,但我是指以下的代码逻辑:

  1. @Configuration
  2. public class WebClientConfiguration {
  3. @Bean
  4. public WebClient retryWebClient(WebClient.Builder builder) {
  5. return builder.baseUrl("http://localhost:8080")
  6. .filter((request, next) -> next.exchange(request)
  7. .retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(5))
  8. .onRetryExhaustedThrow(
  9. (retryBackoffSpec, retrySignal) -> new TimeoutException(
  10. retrySignal.failure().getMessage()))))
  11. .build();
  12. }
  13. }

然后,在任何需要的地方进行自动装配:

  1. @Autowired
  2. 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:

  1. @Configuration
  2. public class WebClientConfiguration {
  3. @Bean
  4. public WebClient retryWebClient(WebClient.Builder builder) {
  5. return builder.baseUrl(&quot;http://localhost:8080&quot;)
  6. .filter((request, next) -&gt; next.exchange(request)
  7. .retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(5))
  8. .onRetryExhaustedThrow(
  9. (retryBackoffSpec, retrySignal) -&gt; new TimeoutException(
  10. retrySignal.failure().getMessage()))))
  11. .build();
  12. }
  13. }

And then just autowire it anywhere you need:

  1. @Autowired
  2. private WebClient webClient;

答案2

得分: 0

以下是翻译好的内容:

我决定实现这个的方式如下:

  1. public class RetryWebClient {
  2. public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
  3. .onRetryExhaustedThrow(
  4. (retryBackoffSpec, retrySignal) -> new TimeoutException(
  5. retrySignal.failure().getMessage()));
  6. private RetryWebClient() {}
  7. }

然后在 retryWhen 中调用它:

  1. .retryWhen(RetryWebClient.fixedRetry)
英文:

The way that I decided to implement this is as follows:

  1. public class RetryWebClient {
  2. public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
  3. .onRetryExhaustedThrow(
  4. (retryBackoffSpec, retrySignal) -&gt; new TimeoutException(
  5. retrySignal.failure().getMessage()));
  6. private RetryWebClient() {}
  7. }

And called it in retryWhen:

  1. .retryWhen(RetryWebClient.fixedRetry)

huangapple
  • 本文由 发表于 2020年9月24日 14:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64040627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定