如何解决Springboot 中的 Resilience4J 未按预期工作
我正在尝试在我的应用中添加弹性 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 次,但也许我误解了。
解决方法
除了上面@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 已经在运行时提供
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。