英文:
resilience4j-spring-boot-2 annotations (@Retry, @CircuitBreaker...) are completely ignored
问题
I spent a whole day trying to find why this does not work so I think it might be useful if I share the question and the answer.
Resilience4j库提供了一个优雅的基于注解的解决方案,适用于Spring Boot 2。你只需要用提供的注解之一来注解一个方法(或一个类),比如@CircuitBreaker
、@Retry
、@RateLimiter
、@Bulkhead
、@Thread
,适当的弹性模式会被自动添加。
我已经在Maven的pom.xml中添加了所需的依赖:
<!-- language-all: lang-xml -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>${resilience4j.version}</version>
</dependency>
现在编译器是正常的,所以我可以添加注解:
<!-- language-all: lang-java -->
...
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) {
...
}
}
程序可以编译和运行,但是注解被完全忽略了。
英文:
I spent a whole day trying to find why this does not work so I think it might be useful if I share the question and the answer.
The Resilience4j library provides an elegant annotation-based solution from Spring Boot 2. All you need to do is just annotate a method (or a class) with one of the provided annotations, such as @CircuitBreaker
, @Retry
, @RateLimiter
, @Bulkhead
, @Thread
and the appropriate resilience pattern is automagically added.
I added the expected dependency to the Maven pom.xml:
<!-- language-all: lang-xml -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>${resilience4j.version}</version>
</dependency>
Now the compiler is happy, so I can add the annotations:
<!-- language-all: lang-java -->
...
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) {
...
}
}
The program compiles, runs, however the annotations are completely ignored.
答案1
得分: 3
根据resilience4j-spring-boot2文档:
> 该模块期望在运行时已经提供了spring-boot-starter-actuator
和spring-boot-starter-aop
。
因此,整个诀窍是还需要将缺失的依赖项添加到Maven的pom.xml
中:
<!-- language-all: lang-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>
英文:
According to the resilience4j-spring-boot2 documentation:
> The module expects that spring-boot-starter-actuator
and spring-boot-starter-aop
are already provided at runtime.
So the whole trick is to add also the missing dependencies to the Maven pom.xml
:
<!-- language-all: lang-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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论