Guava RateLimiter 實(shí)現(xiàn) API 限流,這才是正確的姿勢(shì)!
Guava提供的RateLimiter可以限制物理或邏輯資源的被訪問速率,咋一聽有點(diǎn)像java并發(fā)包下的Samephore,但是又不相同,RateLimiter控制的是速率,Samephore控制的是并發(fā)量。
<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個(gè)
for (int i = 1; i <= 10; i++) {
limiter.acquire();// 請(qǐng)求RateLimiter, 超過permits會(huì)被阻塞
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);
}

另外,Java 多線程系列面試題和答案全部整理好了,微信搜索互聯(lián)網(wǎng)架構(gòu)師,在后臺(tái)發(fā)送:2T獲取。
實(shí)際項(xiàng)目中使用
@Service
public class GuavaRateLimiterService {
/*每秒控制5個(gè)許可*/
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,"未獲取到許可");
}

import java.lang.annotation.*;
/**
* 自定義注解可以不包含屬性,成為一個(gè)標(biāo)識(shí)注解
*/
@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); //比如說,我這里設(shè)置"并發(fā)數(shù)"為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();
}
}
}
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限流測(cè)試(基于 注解+切面 方式)
* 創(chuàng)建人:simonsfan
*/
@Controller
public class TestController {
@ResponseBody
@RateLimitAspect //可以非常方便的通過這個(gè)注解來實(shí)現(xiàn)限流
@RequestMapping("/test")
public String test(){
return ResultUtil.success1(1001, "success").toString();
}
壓測(cè)結(jié)果:

作者:飯一碗
來源:https://blog.csdn.net/fanrenxiang/article/details/80949079
正文結(jié)束
1.不認(rèn)命,從10年流水線工人,到谷歌上班的程序媛,一位湖南妹子的勵(lì)志故事
3.從零開始搭建創(chuàng)業(yè)公司后臺(tái)技術(shù)棧
5.37歲程序員被裁,120天沒找到工作,無奈去小公司,結(jié)果懵了...
6.IntelliJ IDEA 2019.3 首個(gè)最新訪問版本發(fā)布,新特性搶先看
一個(gè)人學(xué)習(xí)、工作很迷茫?
點(diǎn)擊「閱讀原文」加入我們的小圈子!

評(píng)論
圖片
表情
