【问题标题】:Resilience4J in Springboot is not working as expectingSpringboot 中的 Resilience4J 未按预期工作
【发布时间】:2021-09-23 17:47:43
【问题描述】:

我正在尝试将弹性 4j 添加到我的应用中以实现指数退避等。

服务

@Component
public class ResilienceService {
    private static final String BACKEND_A = "backendA";

    public ResilienceService() throws IOException {
        testRetry();
    }

    @Retry(name = BACKEND_A)
    public void testRetry() throws IOException {
        System.out.println("Hey it's working!");
        throw new IOException();
    }

}

配置

resilience4j.retry.instances.backendA.maxAttempts=3
resilience4j.retry.instances.backendA.waitDuration=10s
resilience4j.retry.instances.backendA.enableExponentialBackoff=true
resilience4j.retry.instances.backendA.exponentialBackoffMultiplier=2
resilience4j.retry.instances.backendA.retryExceptions[0]=java.io.IOException

我正在尝试查看弹性库是否会调用此函数 3 次。我应该如何考虑正确配置它并测试重试是否实际发生?我以为我可以在方法上放一个断点并看到它调用 3 次,但也许我误解了。

【问题讨论】:

  • 1.不要从构造函数中调用它,2. 这不起作用,因为它是一个内部方法调用,并且是使用代理实现的。

标签: java spring spring-boot resilience4j


【解决方案1】:

除了上面@M.Deinum 的评论之外,您可能还被resilience4j-springboot2 发现,默认情况下不依赖于spring aop。

例如你可能需要:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
</dependencies>

公平地说,the documentation does state:

将 Resilience4j 的 Spring Boot 2 Starter 添加到您的编译依赖项中。

模块期望 org.springframework.boot:spring-boot-starter-actuator 和 org.springframework.boot:spring-boot-starter-aopare 在运行时已经提供

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 2019-04-30
    • 2021-06-04
    • 2022-01-24
    • 2015-05-11
    • 2020-05-15
    • 2014-10-31
    • 2018-02-12
    • 2014-01-20
    相关资源
    最近更新 更多