`resilience4j-spring-boot-2`的注解(@Retry、@CircuitBreaker等)完全被忽略。

huangapple go评论67阅读模式
英文:

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 -->

&lt;dependency&gt;
    &lt;groupId&gt;io.github.resilience4j&lt;/groupId&gt;
    &lt;artifactId&gt;resilience4j-spring-boot2&lt;/artifactId&gt;
    &lt;version&gt;${resilience4j.version}&lt;/version&gt;
&lt;/dependency&gt;

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 = &quot;get-response&quot;)
    public MyResponse getResponse(MyRequest request) {
        ...
    }
}

The program compiles, runs, however the annotations are completely ignored.

答案1

得分: 3

根据resilience4j-spring-boot2文档

> 该模块期望在运行时已经提供了spring-boot-starter-actuatorspring-boot-starter-aop

因此,整个诀窍是还需要将缺失的依赖项添加到Maven的pom.xml中:

<!-- language-all: lang-xml -->

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
&lt;/dependency&gt;
英文:

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 -->

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年10月9日 03:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64269326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定