问题
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
回答1:
Answer to this surely depends on what you relate throttling to.
- If you are thinking to throttle data returned by api based on role for some time, you could achieve this simply by using spring-boot cache. You can control cache evict time in springboot-app (Even if you want to externalize configuration).
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.
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.
- If you are thinking to throttle the api endpoint itself or throttle the amount of data it could serve i.e. control no. of requests it could serve in a seconds etc. Then you could use
RateLimiter
from Guava.
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.
Hope it helps!
来源:https://stackoverflow.com/questions/57573196/rate-limiting-and-throttling-in-java