resilience4j-spring-boot-2 注释(@Retry、@CircuitBreaker...)被完全忽略

resilience4j-spring-boot-2 annotations (@Retry, @CircuitBreaker...) are completely ignored

我花了一整天的时间试图找出为什么这不起作用,所以我认为如果我分享问题和答案可能会有用。

Resilience4j library 提供了来自 Spring Boot 2 的优雅的基于注解的解决方案。您需要做的只是使用其中一个注解方法(或 class)提供注释,例如 @CircuitBreaker@Retry@RateLimiter@Bulkhead@Thread 并自动添加适当的弹性模式。

我向 Maven 添加了预期的依赖项 pom.xml:

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
    <version>${resilience4j.version}</version>
</dependency>

现在编译器很满意,所以我可以添加注释:

...
import org.springframework.stereotype.Service;
import io.github.resilience4j.retry.annotation.Retry;
...

@Service
public class MyService {
    ...
    @Retry(name = "get-response")
    public MyResponse getResponse(MyRequest request) {
        ...
    }
}

程序编译、运行,但是注解被完全忽略

根据 resilience4j-spring-boot2 documentation:

The module expects that spring-boot-starter-actuator and spring-boot-starter-aop are already provided at runtime.

所以整个技巧是添加 缺少的依赖项到 Maven pom.xml:

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