Spring-Retry断路器开始恢复代码,在发生异常的情况下不进行任何重试

时间:2019-07-27 06:28:55

标签: java spring spring-boot spring-retry

我正在将spring-retry库用于我的spring boot应用程序。 使用@CircuitBreaker(include = { Exception.class }, maxAttempts = 2)
如果出现异常,则该注释将移动到恢复块,而无需任何重试。

请在下面找到代码段。 完整代码在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;
    }

}

我期望看到重试尝试,但是只有在发生异常的情况下才执行recover方法。

1 个答案:

答案 0 :(得分:0)

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