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个线程并发访问接口,测试结果如下:

可以发现,10个并发访问总是只有6个能获取到许可,结论就是能获取到RateLimiter.create(n)中n+1个许可,总体来看Guava的RateLimiter是比较优雅的。本文就是简单的提了下RateLimiter的使用。

推荐:Java进阶视频资源

翻阅发现使用上述方式使用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();

}

}

}

测试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来动态的加到需要限流的接口上,个人认为是比较优雅的实现吧。

压测结果:

最后

我想问下大家当初选择做程序员的初衷是什么?有思考过这个问题吗?高薪?热爱?

既然入了这行就应该知道,这个行业是靠本事吃饭的,你想要拿高薪没有问题,请好好磨练自己的技术,不要抱怨。有的人通过培训可以让自己成长,有些人可以通过自律强大的自学能力成长,如果你两者都不占,还怎么拿高薪?

架构师是很多程序员的职业目标,一个好的架构师是不愁所谓的35岁高龄门槛的,到了那个时候,照样大把的企业挖他。为什么很多人想进阿里巴巴,无非不是福利待遇好以及优质的人脉资源,这对个人职业发展是有非常大帮助的。

如果你也想成为一名好的架构师,那或许这份Java核心架构笔记你需要阅读阅读,希望能够对你的职业发展有所帮助。

中高级开发必知必会:

薪?热爱?

既然入了这行就应该知道,这个行业是靠本事吃饭的,你想要拿高薪没有问题,请好好磨练自己的技术,不要抱怨。有的人通过培训可以让自己成长,有些人可以通过自律强大的自学能力成长,如果你两者都不占,还怎么拿高薪?

架构师是很多程序员的职业目标,一个好的架构师是不愁所谓的35岁高龄门槛的,到了那个时候,照样大把的企业挖他。为什么很多人想进阿里巴巴,无非不是福利待遇好以及优质的人脉资源,这对个人职业发展是有非常大帮助的。

如果你也想成为一名好的架构师,那或许这份Java核心架构笔记你需要阅读阅读,希望能够对你的职业发展有所帮助。

中高级开发必知必会:

[外链图片转存中…(img-X3RcibPP-1718808269263)]

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值