Guava RateLimiter 实现 API 限流,这才是正确的姿势!

Guava提供的RateLimiter可以限制物理或逻辑资源的被访问速率,咋一听有点像java并发包下的Samephore,但是又不相同,RateLimiter控制的是速率,Samephore控制的是并发量。
RateLimiter的原理类似于令牌桶,它主要由许可发出的速率来定义,如果没有额外的配置,许可证将按每秒许可证规定的固定速度分配,许可将被平滑地分发,若请求超过permitsPerSecond则RateLimiter按照每秒 1/permitsPerSecond 的速率释放许可。

com.google.guava guava 23.0 public static void main(String[] args) { String start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); RateLimiter limiter = RateLimiter.create(1.0); // 这里的1表示每秒允许处理的量为1个 for (int i = 1; i <= 10; i++) { limiter.acquire(); // 请求RateLimiter, 超过permits会被阻塞 System.out.println("call execute.." + i); } String end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); System.out.println("start time:" + start); System.out.println("end time:" + end); }

Guava RateLimiter 实现 API 限流,这才是正确的姿势!
文章图片

可以看到,我假定了每秒处理请求的速率为1个,现在我有10个任务要处理,那么RateLimiter就很好的实现了控制速率,总共10个任务,需要9次获取许可,所以最后10个任务的消耗时间为9s左右。那么在实际的项目中是如何使用的呢??
实际项目中使用
@Service public class GuavaRateLimiterService { /*每秒控制5个许可*/ RateLimiter rateLimiter = RateLimiter.create(5.0); /** * 获取令牌 * * @return */ public boolean tryAcquire() { return rateLimiter.tryAcquire(); }} @Autowired private GuavaRateLimiterService rateLimiterService; @ResponseBody @RequestMapping("/ratelimiter") public Result testRateLimiter(){ if(rateLimiterService.tryAcquire()){ return ResultUtil.success1(1001,"成功获取许可"); } return ResultUtil.success1(1002,"未获取到许可"); }

jmeter起10个线程并发访问接口,测试结果如下:
Guava RateLimiter 实现 API 限流,这才是正确的姿势!
文章图片

可以发现,10个并发访问总是只有6个能获取到许可,结论就是能获取到RateLimiter.create(n)中n+1个许可,总体来看Guava的RateLimiter是比较优雅的。本文就是简单的提了下RateLimiter的使用。
翻阅发现使用上述方式使用RateLimiter的方式不够优雅,尽管我们可以把RateLimiter的逻辑包在service里面,controller直接调用即可,但是如果我们换成:自定义注解+切面 的方式实现的话,会优雅的多,详细见下面代码:
自定义注解类
import java.lang.annotation.*; /** * 自定义注解可以不包含属性,成为一个标识注解 */ @Inherited @Documented @Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface RateLimitAspect {}

自定义切面类
import com.google.common.util.concurrent.RateLimiter; import com.simons.cn.springbootdemo.util.ResultUtil; import net.sf.json.JSONObject; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component @Scope @Aspect public class RateLimitAop { @Autowired private HttpServletResponse response; private RateLimiter rateLimiter = RateLimiter.create(5.0); //比如说,我这里设置"并发数"为5 @Pointcut("@annotation(com.simons.cn.springbootdemo.aspect.RateLimitAspect)") public void serviceLimit() { } @Around("serviceLimit()") public Object around(ProceedingJoinPoint joinPoint) { Boolean flag = rateLimiter.tryAcquire(); Object obj = null; try { if (flag) { obj = joinPoint.proceed(); }else{ String result = JSONObject.fromObject(ResultUtil.success1(100, "failure")).toString(); output(response, result); } } catch (Throwable e) { e.printStackTrace(); } System.out.println("flag=" + flag + ",obj=" + obj); return obj; }public void output(HttpServletResponse response, String msg) throws IOException { response.setContentType("application/json; charset=UTF-8"); ServletOutputStream outputStream = null; try { outputStream = response.getOutputStream(); outputStream.write(msg.getBytes("UTF-8")); } catch (IOException e) { e.printStackTrace(); } finally { outputStream.flush(); outputStream.close(); } } }

推荐一个 Spring Boot 基础教程及实战示例:
https://www.javastack.cn/cate...
测试controller类
import com.simons.cn.springbootdemo.aspect.RateLimitAspect; import com.simons.cn.springbootdemo.util.ResultUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * 类描述:RateLimit限流测试(基于 注解+切面 方式) * 创建人:simonsfan */ @Controller public class TestController { @ResponseBody @RateLimitAspect//可以非常方便的通过这个注解来实现限流 @RequestMapping("/test") public String test(){ return ResultUtil.success1(1001, "success").toString(); }

这样通过自定义注解@RateLimiterAspect来动态的加到需要限流的接口上,个人认为是比较优雅的实现吧。
压测结果:
Guava RateLimiter 实现 API 限流,这才是正确的姿势!
文章图片

可以看到,10个线程中无论压测多少次,并发数总是限制在6,也就实现了限流。
作者:饭一碗 \
来源:blog.csdn.net/fanrenxiang/*article/details/80949079
近期热文推荐:
1.1,000+ 道 Java面试题及答案整理(2021最新版)
2.别在再满屏的 if/ else 了,试试策略模式,真香!!
3.卧槽!Java 中的 xx ≠ null 是什么新语法?
4.Spring Boot 2.5 重磅发布,黑暗模式太炸了!
5.《Java开发手册(嵩山版)》最新发布,速速下载!
【Guava RateLimiter 实现 API 限流,这才是正确的姿势!】觉得不错,别忘了随手点赞+转发哦!

    推荐阅读