无法让 Resilience4j @RateLimiter 与 Spring 引导一起工作
Can't get Resilience4j @RateLimiter to work with Spring Boot
我似乎无法让 Resilience4j @RateLimiter 与 Spring Boot 一起工作。
下面是代码
@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: 0
pom.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>
没有速率限制。无法弄清楚我错过了什么。
我没有使用 Resilience4j 的经验,但看起来您正在尝试在此处使用 spring-aop。这适用于运行时生成的代理,它包装了一个原始的 class 提供额外的功能(在这种情况下是速率限制)。
如果是这样,你不能注释class的私有方法,因为它不会被代理生成机制检测和处理。
而是考虑创建另一个 bean 并将其功能公开为 public 方法:
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();
}
}
}
我似乎无法让 Resilience4j @RateLimiter 与 Spring Boot 一起工作。
下面是代码
@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: 0
pom.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>
没有速率限制。无法弄清楚我错过了什么。
我没有使用 Resilience4j 的经验,但看起来您正在尝试在此处使用 spring-aop。这适用于运行时生成的代理,它包装了一个原始的 class 提供额外的功能(在这种情况下是速率限制)。
如果是这样,你不能注释class的私有方法,因为它不会被代理生成机制检测和处理。
而是考虑创建另一个 bean 并将其功能公开为 public 方法:
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();
}
}
}