如何解决弹性4j springboot 2 注释@Retry,@CircuitBreaker不起作用
我正在开发一个 springboot 应用程序,想添加弹性 4j-重试机制。 我做了以下步骤:
-
在 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>
-
在控制器中创建了一个方法,该方法将尝试命中一个虚拟服务(预计会失败)。在我的方法中添加了 @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(); }
-
将配置添加到 application.properties
resilience4j.retry.instances.sample-api.maxAttempts=5
另外,我尝试了 maxRetryAttempts。但没有任何改变。
我希望它重试在 application.properties 中配置的次数。 但是,它只尝试一次。 不确定我是否遗漏了什么。 有人可以帮忙吗?
解决方法
带注释的方法必须是公共的而不是私有的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。