带断路器的弹簧重试

问题描述 投票:1回答:1

我试图利用弹簧重试的重试和断路器机制。我尝试在特定函数(如下所示)中使用两个注释(@Retryable和@CircuitBreaker),但是Circuit Breaker无效。

@Service
public class CommandAndRetry {

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleRetryService.class);

    @CircuitBreaker(maxAttempts = 1, openTimeout = 10000)
    @Retryable(
            value = {TypeOneException.class},
            maxAttempts = 3, backoff = @Backoff(2000))
    public void retryWhenException() throws TypeOneException {
        LOGGER.info("Retrying");
        throw new TypeOneException();
    }

    @Recover
    public void recover(Throwable t) throws Throwable {
        LOGGER.info("SampleRetryService.recover");
        throw t;
    }
}

然后我尝试将功能分成两个不同的功能,分别有@Retryable和@CircuitBreaker。在这种情况下,重试机制不起作用。请在下面找到代码段。

PS:从控制器调用exec方法(Circuit Breaker方法)。

@Service
public class CommandAndRetry {

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleRetryService.class);


    @CircuitBreaker(maxAttempts = 1, openTimeout = 10000)
    public void exec() throws TypeOneException {
        retryWhenException();
    }

    @Retryable(
            value = {TypeOneException.class},
            maxAttempts = 3, backoff = @Backoff(2000))
    public void retryWhenException() throws TypeOneException {
        LOGGER.info("Retrying");
        throw new TypeOneException();
    }

    @Recover
    public void recover(Throwable t) throws Throwable {
        LOGGER.info("SampleRetryService.recover");
        throw t;
    }
}

任何人都可以告诉它为什么会这样。

还请告知是否有更好的方法来实现重试和断路器。 PS:我既不想使用resilience4j也不想使用retryTemplate。

spring spring-retry circuit-breaker
1个回答
1
投票

如果要在断路器内重试,它们必须位于不同的bean中。如果你直接从另一个调用一个@Retryable,在同一个bean中,你将绕过拦截器。

这对我来说很好......

@SpringBootApplication
@EnableRetry
public class So52193237Application {

    public static void main(String[] args) {
        SpringApplication.run(So52193237Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(Foo foo) {
        return args -> {
            try {
                foo.exec();
            }
            catch (Exception e) {
                try {
                    foo.exec();
                }
                catch (Exception ee) {
                    Thread.sleep(11000);
                    try {
                        foo.exec();
                    }
                    catch (Exception eee) {

                    }
                }
            }
        };
    }

    @Component
    public static class Foo {

        private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);

        private final Bar bar;

        public Foo(Bar bar) {
            this.bar = bar;
        }

        @CircuitBreaker(maxAttempts = 1, openTimeout = 10000, resetTimeout=10000)
        public void exec() throws TypeOneException {
            LOGGER.info("Foo.circuit");
            this.bar.retryWhenException();
        }

        @Recover
        public void recover(Throwable t) throws Throwable {
            LOGGER.info("Foo.recover");
            throw t;
        }

    }

    @Component
    public static class Bar {

        private static final Logger LOGGER = LoggerFactory.getLogger(Bar.class);

        @Retryable(value = { TypeOneException.class }, maxAttempts = 3, backoff = @Backoff(2000))
        public void retryWhenException() throws TypeOneException {
            LOGGER.info("Retrying");
            throw new TypeOneException();
        }

        @Recover
        public void recover(Throwable t) throws Throwable {
            LOGGER.info("Bar.recover");
            throw t;
        }

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.