openfeign 重试配置
时间: 2023-12-01 11:43:19 浏览: 113
在OpenFeign中,可以通过Retryer来配置重试策略。Retryer是一个接口,它有一个方法:continueOrPropagate,用于判断是否需要重试。默认情况下,OpenFeign使用的是默认的Retryer,即Retryer.NEVER_RETRY,它表示不进行重试。如果需要进行重试,可以自定义Retryer并在FeignClient中进行配置。
以下是一个自定义Retryer的例子:
```java
public class MyRetryer implements Retryer {
private final int maxAttempts;
private final long backoff;
int attempt;
public MyRetryer() {
this(100, 1000, 3);
}
public MyRetryer(long backoff, int maxAttempts) {
this(100, backoff, maxAttempts);
}
public MyRetryer(int attempt, long backoff, int maxAttempts) {
this.attempt = attempt;
this.backoff = backoff;
this.maxAttempts = maxAttempts;
}
@Override
public void continueOrPropagate(RetryableException e) {
if (attempt++ >= maxAttempts) {
throw e;
}
try {
Thread.sleep(backoff);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}
@Override
public Retryer clone() {
return new MyRetryer(attempt, backoff, maxAttempts);
}
}
```
在FeignClient中配置自定义Retryer:
```java
@FeignClient(name = "example", configuration = MyConfiguration.class)
public interface MyClient {
@GetMapping("/example")
String get();
}
@Configuration
public class MyConfiguration {
@Bean
public Retryer retryer() {
return new MyRetryer();
}
}
```
上述代码中,MyRetryer实现了Retryer接口,并重写了continueOrPropagate方法,实现了自定义的重试策略。在FeignClient中,通过configuration属性指定MyConfiguration类,并在MyConfiguration类中定义了一个名为retryer的Bean,用于配置Retryer。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)