簡體   English   中英

Spring-Retry 斷路器移動以恢復代碼,在異常情況下無需任何重試嘗試

[英]Spring-Retry circuit breaker moves to recover code, without any retry attempt in case of exception

我正在為我的 spring 啟動應用程序使用 spring-retry 庫。 當使用@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;
    }

}

我期待看到重試嘗試,但只有在發生異常時才執行恢復方法。

問題是 Sambit 對你的回答,帶有 @CircuitBreaker 的帶注釋的方法 getRetryCircuitBrreakerIntService 必須與 @Recover 注釋的方法具有相同的簽名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM