我似乎无法让Resilience4j @RateLimiter使用Spring。
下面是代码
@Log4j2
@Component
class Resilience4jDemo implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
for (int i = 0; i < 100; i++) {
callBackendA();
}
}
@RateLimiter(name = "backendA")
private void callBackendA() {
log.info("Calling ");
}
}application.yaml文件
resilience4j.ratelimiter:
instances:
backendA:
limitForPeriod: 1
limitRefreshPeriod: 10s
timeoutDuration: 0pom.xml
<!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-spring-boot2 -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.6.0</version>
</dependency>没有利率限制。不知道我错过了什么。
发布于 2021-11-28 06:38:05
我没有使用Resilience4j的经验,但是看起来您在这里尝试使用spring。这适用于包含原始类的运行时生成的代理,该代理提供了额外的功能(在本例中是限制速率的)。
如果是这样的话,您就不能注释类的私有方法,因为代理生成机制不会检测和处理它。
相反,请考虑创建另一个bean,并将其功能公开为公共方法:
public interface Backend {
void callBackendA();
}
@Component // this is a spring bean!
@Log4j2
public class LoggingBackendImpl implements Backend {
@RateLimiter(name = "backendA")
public void callBackendA() {
log.info("calling backend");
}
}
@Component
class Resilience4jDemo implements CommandLineRunner {
@Autowired
Backend backend; // this has to be managed by spring
@Override
public void run(String... args) throws Exception {
for (int i = 0; i < 100; i++) {
backend.callBackendA();
}
}
}https://stackoverflow.com/questions/70141022
复制相似问题