Java中的速率限制和限制 [英] rate limiting and throttling in java
问题描述
我需要在我的一个微服务中实现ratelimiter/throttling. 例如,我有一个User微服务,该服务基于Admin或普通用户等角色处理登录并获取用户数据,该角色是使用 JWT令牌和注解@Secured,所以,我的要求是基于这些被调用的api进行节流,而且我也应该能够在运行时修改节流限制. 我不想重新发明轮子,所以,有什么想法吗? 技术堆栈:-Java,春季启动
I need to implement ratelimiter/throttling in one of my microservice. For example I have one User microservice that handles login and get user data based on role like Admin or normal user that is implemented using JWT token and annotation @Secured, So, My ask is to throttle based on these what api is being called.And, I should be able to modify the throttle limit at runtime too. I don't want to re-invent the wheel, so, any ideas please? technology stack:- java, spring boot
推荐答案
对此的答案肯定取决于您与节流有关.
Answer to this surely depends on what you relate throttling to.
- 如果您想在一段时间内根据角色限制api返回的数据,则可以通过使用spring-boot缓存来实现.您可以在springboot-app中控制缓存逐出时间(即使您想外部化配置).
请查看 https://spring.io/guides/gs/caching/.另外,请查看 https://www.youtube.com/watch?v=nfZxXGjXVfc 演示(如果需要).
Please have a look at https://spring.io/guides/gs/caching/. Also, have a look at https://www.youtube.com/watch?v=nfZxXGjXVfc demonstration if required.
由于Springboot文档中对此进行了很好的解释,因此我没有详细介绍如何进行缓存.因此,可能必须根据您的需要对其进行调整,但这是控制节流的第一个答案.
I am not putting details of how caching is done as it's very well explained in springboot docs. So, might have to tune it according to your need but this is first answer to your controlled throttling.
- 如果您想限制api端点本身或限制它可以服务的数据量,即控制号.它可以在几秒钟内满足请求等.然后您可以使用Guava中的
RateLimiter
.
此外,如果您使用的是springboot,我设法找到了另一个可能更相关的选项.它是 weddini/spring-boot-throttling
Also, I managed to find another one probably more relevant if you are using springboot. It's weddini/spring-boot-throttling
似乎第二种方法更适合您的需求.
Seems like 2nd approach fits more into what you need.
希望有帮助!
这篇关于Java中的速率限制和限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!