【问题标题】:Spring-Retry circuit breaker moves to recover code, without any retry attempt in case of exceptionSpring-Retry 断路器移动以恢复代码,发生异常时无需任何重试尝试
【发布时间】: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


【解决方案1】:

问题是 Sambit 给你的回答,带有 @CircuitBreaker 的带注释的方法 getRetryCircuitBrreakerIntService 必须与 @Recover 注释的方法具有相同的签名。

【讨论】:

    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多