弹性4j springboot 2 注释@Retry,@CircuitBreaker不起作用

问题描述

我正在开发一个 springboot 应用程序,想添加弹性 4j-重试机制。 我做了以下步骤:

  1. 在 pom.xml 中添加了执行器、aop 和弹性 4j 依赖

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
    
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-aop</artifactId>
     </dependency>
    
     <dependency>
         <groupId>io.github.resilience4j</groupId>
         <artifactId>resilience4j-spring-boot2</artifactId>
     </dependency>
    
  2. 在控制器中创建了一个方法,该方法将尝试命中一个虚拟服务(预计会失败)。在我的方法添加了 @Retry 注释。

     @GetMapping("/sample-api")
     @Retry(name = "sample-api")
     private String sampleApi() {
      log.info("Sample Api call receieved");
      ResponseEntity<String> forEntity = new RestTemplate().getForEntity("http://localhost:8080/some-dummy-url",String.class);
      return forEntity.getBody();
     }
    
  3. 将配置添加到 application.properties

resilience4j.retry.instances.sample-api.maxAttempts=5

另外,我尝试了 maxRetryAttempts。但没有任何改变。

我希望它重试在 application.properties 中配置的次数。 但是,它只尝试一次。 不确定我是否遗漏了什么。 有人可以帮忙吗?

解决方法

带注释的方法必须是公共的而不是私有的。