【发布时间】:2019-12-05 08:54:52
【问题描述】:
我正在为我的 Spring Boot 应用程序使用 spring-retry 库。
使用@CircuitBreaker(include = { Exception.class }, maxAttempts = 2)时
注释在异常情况下方法移动到恢复块,没有任何重试尝试。
请在下面找到代码 sn-p。 完整代码在 GitHub 上,下面是链接。 https://github.com/konduruvijaykumar/spring-retry-hystrix/tree/master/spring-retry-hystrix-service-1
@SpringBootApplication
@EnableCircuitBreaker
@EnableRetry
public class SpringRetryHystrixService1Application {
public static void main(String[] args) {
SpringApplication.run(SpringRetryHystrixService1Application.class, args);
}
}
@RestController
public class SpringRetryHystrixService1SimpleController {
private final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
SpringRetryHystrixService1SimpleService springRetryHystrixService1SimpleService;
@GetMapping("/simple-retry-cb-getint")
public int retryCircuitBreakerGetInt() throws Exception {
return springRetryHystrixService1SimpleService.getRetryCircuitBrreakerIntService();
}
}
@Service
public class SpringRetryHystrixService1SimpleService {
private final Logger log = LoggerFactory.getLogger(getClass());
@Recover
private int recoverFromException(Exception exception) {
log.info(" ## recoverFromException(Exception exception) ##");
return 0;
}
@CircuitBreaker(include = { Exception.class }, maxAttempts = 2)
public int getRetryCircuitBrreakerIntService() throws Exception {
log.info(" ## getRetryCircuitBrreakerIntService() ##");
if (Math.random() > 0.5) {
Thread.sleep(1000 * 3);
throw new RuntimeException("Service failed when executing getRetryIntService() method");
}
// One is success and Zero is failure
return 1;
}
}
我期待看到重试尝试,但只有在出现异常时才执行恢复方法。
【问题讨论】:
-
CircuitBreaker 和 Retry 两种实现方式不同。
-
@Sambit 我不明白。我在这里只使用 CircuitBreaker 实现。这具有重试 maxAttempts 的属性(最大尝试次数(包括第一次失败),默认为 3)。 CircuitBreaker 本身带有 Retryable(stateful = true) 注释
-
我是说,如果您使用的是 CircuitBreaker,则不需要 EnableRetry 注释。
标签: java spring spring-boot spring-retry