Guava提供的RateLimiter能够限度物理或逻辑资源的被拜访速率,咋一听有点像java并发包下的Samephore,然而又不雷同,RateLimiter管制的是速率,Samephore管制的是并发量。
RateLimiter的原理相似于令牌桶,它次要由许可收回的速率来定义,如果没有额定的配置,许可证将按每秒许可证规定的固定速度调配,许可将被平滑地散发,若申请超过permitsPerSecond则RateLimiter依照每秒 1/permitsPerSecond 的速率开释许可。
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version></dependency>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);}
能够看到,我假设了每秒解决申请的速率为1个,当初我有10个工作要解决,那么RateLimiter就很好的实现了管制速率,总共10个工作,须要9次获取许可,所以最初10个工作的耗费工夫为9s左右。那么在理论的我的项目中是如何应用的呢??
理论我的项目中应用
@Servicepublic 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个线程并发拜访接口,测试后果如下:
能够发现,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@Aspectpublic 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 */@Controllerpublic class TestController { @ResponseBody @RateLimitAspect //能够十分不便的通过这个注解来实现限流 @RequestMapping("/test") public String test(){ return ResultUtil.success1(1001, "success").toString(); }
这样通过自定义注解@RateLimiterAspect来动静的加到须要限流的接口上,集体认为是比拟优雅的实现吧。
压测后果:
能够看到,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开发手册(嵩山版)》最新公布,速速下载!
感觉不错,别忘了顺手点赞+转发哦!