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

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

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) -&gt; new TimeoutException(
              retrySignal.failure().getMessage()));

And used this method here:

 public List&lt;A&gt; 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(&quot;http://localhost:8080&quot;)
            .filter((request, next) -&gt; next.exchange(request)
                    .retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(5))
                            .onRetryExhaustedThrow(
                                    (retryBackoffSpec, retrySignal) -&gt; 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) -&gt; new TimeoutException(
              retrySignal.failure().getMessage()));

  private RetryWebClient() {}

}

And called it in retryWhen:

.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:

确定