SpringBoot利用限速器RateLimiter實現(xiàn)單機限流的示例代碼
一. 概述
參考開源項目https://github.com/xkcoding/spring-boot-demo
在系統(tǒng)運維中, 有時候為了避免用戶的惡意刷接口, 會加入一定規(guī)則的限流, 本Demo使用速率限制器com.xkcoding.ratelimit.guava.annotation.RateLimiter實現(xiàn)單機版的限流
二. SpringBootDemo
2.1 依賴
? ? <dependency> ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? </dependency> ? ? <dependency> ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? <artifactId>spring-boot-starter-aop</artifactId> ? ? </dependency> ? ? <dependency> ? ? ? <groupId>cn.hutool</groupId> ? ? ? <artifactId>hutool-all</artifactId> ? ? </dependency> ? ? <dependency> ? ? ? <groupId>com.google.guava</groupId> ? ? ? <artifactId>guava</artifactId> ? ? </dependency>
2.2 application.yml
server: ? port: 8080 ? servlet: ? ? context-path: /demo
2.3 啟動類
@SpringBootApplication public class SpringBootDemoRatelimitGuavaApplication { ? ? public static void main(String[] args) { ? ? ? ? SpringApplication.run(SpringBootDemoRatelimitGuavaApplication.class, args); ? ? } }
2.4 定義一個限流注解 RateLimiter.java
注意代碼里使用了 AliasFor 設(shè)置一組屬性的別名,所以獲取注解的時候,需要通過 Spring 提供的注解工具類 AnnotationUtils 獲取,不可以通過 AOP 參數(shù)注入的方式獲取,否則有些屬性的值將會設(shè)置不進去。
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RateLimiter { ? ? int NOT_LIMITED = 0; ? ? /** ? ? ?* qps (每秒并發(fā)量) ? ? ?*/ ? ? @AliasFor("qps") double value() default NOT_LIMITED; ? ? /** ? ? ?* qps (每秒并發(fā)量) ? ? ?*/ ? ? @AliasFor("value") double qps() default NOT_LIMITED; ? ? /** ? ? ?* 超時時長,默認不等待 ? ? ?*/ ? ? int timeout() default 0; ? ? /** ? ? ?* 超時時間單位,默認毫秒 ? ? ?*/ ? ? TimeUnit timeUnit() default TimeUnit.MICROSECONDS; }
2.5 代理: RateLimiterAspect.java
@Slf4j @Aspect @Component public class RateLimiterAspect { ? ? /** ? ? ?* 單機緩存 ? ? ?*/ ? ? private static final ConcurrentMap<String, com.google.common.util.concurrent.RateLimiter> RATE_LIMITER_CACHE = new ConcurrentHashMap<>(); ? ? @Pointcut("@annotation(com.xkcoding.ratelimit.guava.annotation.RateLimiter)") ? ? public void rateLimit() { ? ? } ? ? @Around("rateLimit()") ? ? public Object pointcut(ProceedingJoinPoint point) throws Throwable { ? ? ? ? MethodSignature signature = (MethodSignature) point.getSignature(); ? ? ? ? Method method = signature.getMethod(); ? ? ? ? // 通過 AnnotationUtils.findAnnotation 獲取 RateLimiter 注解 ? ? ? ? RateLimiter rateLimiter = AnnotationUtils.findAnnotation(method, RateLimiter.class); ? ? ? ? if (rateLimiter != null && rateLimiter.qps() > RateLimiter.NOT_LIMITED) { ? ? ? ? ? ? double qps = rateLimiter.qps(); ? ? ? ? ? ? // TODO 這個key可以根據(jù)具體需求配置,例如根據(jù)ip限制,或用戶 ? ? ? ? ? ? String key = method.getDeclaringClass().getName() + StrUtil.DOT + method.getName(); ? ? ? ? ? ? if (RATE_LIMITER_CACHE.get(key) == null) { ? ? ? ? ? ? ? ? // 初始化 QPS ? ? ? ? ? ? ? ? RATE_LIMITER_CACHE.put(key, com.google.common.util.concurrent.RateLimiter.create(qps)); ? ? ? ? ? ? } ? ? ? ? ? ? // 嘗試獲取令牌 ? ? ? ? ? ? if (RATE_LIMITER_CACHE.get(key) != null && !RATE_LIMITER_CACHE.get(key).tryAcquire(rateLimiter.timeout(), rateLimiter.timeUnit())) { ? ? ? ? ? ? ? ? throw new RuntimeException("手速太快了,慢點兒吧~"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return point.proceed(); ? ? } }
2.6 使用
@Slf4j @RestController public class TestController { ? ? /** ? ? ?* 接口每秒只能請求一次,不等待 ? ? ?* @return ? ? ?*/ ? ? @RateLimiter(value = 1.0) ? ? @GetMapping("/test1") ? ? public Dict test1() { ? ? ? ? log.info("【test1】被執(zhí)行了。。。。。"); ? ? ? ? return Dict.create().set("msg", "hello,world!").set("description", "別想一直看到我,不信你快速刷新看看~"); ? ? } ? ? /** ? ? ?* 接口每秒只能請求一次,等待一秒 ? ? ?* @return ? ? ?*/ ? ? @RateLimiter(value = 1.0, timeout = 1,timeUnit = TimeUnit.SECONDS) ? ? @GetMapping("/test3") ? ? public Dict test3() { ? ? ? ? log.info("【test3】被執(zhí)行了。。。。。"); ? ? ? ? return Dict.create().set("msg", "hello,world!").set("description", "別想一直看到我,不信你快速刷新看看~"); ? ? } }
到此這篇關(guān)于SpringBoot利用限速器RateLimiter實現(xiàn)單機限流的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot 單機限流 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jenkins 構(gòu)建項目之 pipeline基礎(chǔ)教程
​pipeline ,簡單來說,就是一套運行在 jenkins 上的工作流框架。這篇文章主要介紹了jenkins 構(gòu)建項目之 pipeline基礎(chǔ)教程,需要的朋友可以參考下2020-07-07SpringBoot Redis實現(xiàn)接口冪等性校驗方法詳細講解
這篇文章主要介紹了SpringBoot Redis實現(xiàn)接口冪等性校驗方法,近期一個老項目出現(xiàn)了接口冪等性校驗問題,前端加了按鈕置灰,依然被人拉著接口參數(shù)一頓輸出,還是重復調(diào)用了接口,通過復制粘貼,完成了后端接口冪等性調(diào)用校驗2022-11-11Java class文件格式之數(shù)據(jù)類型(二)_動力節(jié)點Java學院整理
這篇文章主要介紹了Java class文件格式之數(shù)據(jù)類型(二)的相關(guān)資料,需要的朋友可以參考下2017-06-06Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢
這篇文章主要介紹了Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11