Guava RateLimiter 實現 API 限流,這纔是正確的姿勢!

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左右。那麼在實際的項目中是如何使用的呢??

實際項目中使用

@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的使用。

翻閱發現使用上述方式使用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/categories/Spring-Boot/

測試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來動態的加到需要限流的接口上,個人認爲是比較優雅的實現吧。

壓測結果:

可以看到,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開發手冊(嵩山版)》最新發布,速速下載!

覺得不錯,別忘了隨手點贊+轉發哦!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章