Java Resilience4J Ratelimiter不限制对带注释方法的访问
我正在使用Java Resilience4J Ratelimiter不限制对带注释方法的访问,java,multithreading,spring-boot,resilience4j,Java,Multithreading,Spring Boot,Resilience4j,我正在使用resilience4j-spring-boot2库(io.github.resilience4j:resilience4j-spring-boot2,版本1.5.0)和spring-Boot版本2.3.1.发行版。 我创建了一个RateLimiter,它应该只允许一个线程每30秒执行一个特定的方法。但是,线程调用该方法似乎没有边界(Tomcat服务器中运行的已配置线程数除外) 我实现了一个简单的服务,它等待给定的时间,然后返回“Hello!”: 并按如下方式配置速率限制器 resil
resilience4j-spring-boot2
库(io.github.resilience4j:resilience4j-spring-boot2
,版本1.5.0
)和spring-Boot版本2.3.1.发行版
。
我创建了一个RateLimiter
,它应该只允许一个线程每30秒执行一个特定的方法。但是,线程调用该方法似乎没有边界(Tomcat服务器中运行的已配置线程数除外)
我实现了一个简单的服务,它等待给定的时间,然后返回“Hello!”:
并按如下方式配置速率限制器
resilience4j.ratelimiter:
instances:
hello-rl:
limitForPeriod: 1 # The number of permissions available during one limit refresh period
limitRefreshPeriod: 30s # The period of a limit refresh. After each period the rate limiter sets its permissions count back to the limitForPeriod value
timeoutDuration: 30s # The default wait time a thread waits for a permission
我用八个线程调用了该服务:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@Slf4j
public class CircuitBreakerTest {
@Autowired
TestRestTemplate testRestTemplate;
@LocalServerPort
private int port;
@Value("${server.address}")
private String serverAddress;
@Test
public void t() throws InterruptedException {
final ExecutorService executorService = Executors.newFixedThreadPool(8);
for (int i : IntStream.rangeClosed(1, 8).toArray()) {
executorService.execute(() -> log.error(callService()));
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
}
private String callService() {
return testRestTemplate.getForObject(
"http://" + serverAddress + ":" + port + "/hello/5000",
String.class
);
}
结果如下:
2020-08-07 10:41:10,095 ERROR [pool-1-thread-7] CircuitBreakerTest: Hello!
2020-08-07 10:41:10,095 ERROR [pool-1-thread-1] CircuitBreakerTest: Hello!
2020-08-07 10:41:10,095 ERROR [pool-1-thread-8] CircuitBreakerTest: Hello!
2020-08-07 10:41:10,095 ERROR [pool-1-thread-2] CircuitBreakerTest: Hello!
2020-08-07 10:41:10,095 ERROR [pool-1-thread-5] CircuitBreakerTest: Hello!
2020-08-07 10:41:15,104 ERROR [pool-1-thread-3] CircuitBreakerTest: Hello!
2020-08-07 10:41:15,121 ERROR [pool-1-thread-4] CircuitBreakerTest: Hello!
2020-08-07 10:41:15,121 ERROR [pool-1-thread-6] CircuitBreakerTest: Hello!
似乎一次只允许五个线程执行该方法,但我不知道为什么。Tomcat服务器运行的线程数超过8个。
我犯了错误吗?还是我误解了RateLimiter应该如何工作?
resilience4j-spring-boot2
在内部使用方面,因此需要将spring boot starter aop
依赖项添加到项目中,注释才能工作:
<dependency>
<!-- for rate limiting -->
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>${resilience4j.version}</version>
</dependency>
<dependency>
<!-- needed for Resilience4j -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
io.github.resilience4j
就在开头(“设置”)。。。完全忽略了这一点。如果您调试它,您将看到您的属性被忽略,默认配置也被考虑在内。但即使在我修复后,它仍然不起作用,因此没有回答:)
<dependency>
<!-- for rate limiting -->
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>${resilience4j.version}</version>
</dependency>
<dependency>
<!-- needed for Resilience4j -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>