@CircuitBreaker已經將@Retry實現爲stateful = true,這就是他知道有多少次調用失敗的原因。
我認爲最好的辦法在這裏,將使用RetryTemplate你的方法裏面:
@CircuitBreaker(maxAttempts = 2, openTimeout = 5000l, resetTimeout = 10000l)
void call() {
retryTemplate.execute(new RetryCallback<Void, RuntimeException>() {
@Override
public Void doWithRetry(RetryContext context) {
myService.templateRetryService();
}
});
}
聲明的RetryTemplate:
@Configuration
public class AppConfig {
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(2000l);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
}
項目啓用春重試:
@Configuration
@EnableRetry
public class AppConfig { ... }