首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法让Resilience4j @RateLimiter使用Spring

无法让Resilience4j @RateLimiter使用Spring
EN

Stack Overflow用户
提问于 2021-11-28 05:55:31
回答 1查看 690关注 0票数 0

我似乎无法让Resilience4j @RateLimiter使用Spring。

下面是代码

代码语言:javascript
复制
@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文件

代码语言:javascript
复制
resilience4j.ratelimiter:
  instances:
    backendA:
      limitForPeriod: 1
      limitRefreshPeriod: 10s
      timeoutDuration: 0

pom.xml

代码语言:javascript
复制
<!-- 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>

没有利率限制。不知道我错过了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-28 06:38:05

我没有使用Resilience4j的经验,但是看起来您在这里尝试使用spring。这适用于包含原始类的运行时生成的代理,该代理提供了额外的功能(在本例中是限制速率的)。

如果是这样的话,您就不能注释类的私有方法,因为代理生成机制不会检测和处理它。

相反,请考虑创建另一个bean,并将其功能公开为公共方法:

代码语言:javascript
复制
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();
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70141022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档