SpringBoot利用限速器RateLimiter實(shí)現(xiàn)單機(jī)限流的示例代碼
一. 概述
參考開(kāi)源項(xiàng)目https://github.com/xkcoding/spring-boot-demo
在系統(tǒng)運(yùn)維中, 有時(shí)候?yàn)榱吮苊庥脩舻膼阂馑⒔涌? 會(huì)加入一定規(guī)則的限流, 本Demo使用速率限制器com.xkcoding.ratelimit.guava.annotation.RateLimiter實(shí)現(xiàn)單機(jī)版的限流
二. 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 啟動(dòng)類
@SpringBootApplication
public class SpringBootDemoRatelimitGuavaApplication {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(SpringBootDemoRatelimitGuavaApplication.class, args);
? ? }
}2.4 定義一個(gè)限流注解 RateLimiter.java
注意代碼里使用了 AliasFor 設(shè)置一組屬性的別名,所以獲取注解的時(shí)候,需要通過(guò) Spring 提供的注解工具類 AnnotationUtils 獲取,不可以通過(guò) AOP 參數(shù)注入的方式獲取,否則有些屬性的值將會(huì)設(shè)置不進(jìn)去。
@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;
? ? /**
? ? ?* 超時(shí)時(shí)長(zhǎng),默認(rèn)不等待
? ? ?*/
? ? int timeout() default 0;
? ? /**
? ? ?* 超時(shí)時(shí)間單位,默認(rèn)毫秒
? ? ?*/
? ? TimeUnit timeUnit() default TimeUnit.MICROSECONDS;
}2.5 代理: RateLimiterAspect.java
@Slf4j
@Aspect
@Component
public class RateLimiterAspect {
? ? /**
? ? ?* 單機(jī)緩存
? ? ?*/
? ? 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();
? ? ? ? // 通過(guò) AnnotationUtils.findAnnotation 獲取 RateLimiter 注解
? ? ? ? RateLimiter rateLimiter = AnnotationUtils.findAnnotation(method, RateLimiter.class);
? ? ? ? if (rateLimiter != null && rateLimiter.qps() > RateLimiter.NOT_LIMITED) {
? ? ? ? ? ? double qps = rateLimiter.qps();
? ? ? ? ? ? // TODO 這個(gè)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("手速太快了,慢點(diǎn)兒吧~");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return point.proceed();
? ? }
}2.6 使用
@Slf4j
@RestController
public class TestController {
? ? /**
? ? ?* 接口每秒只能請(qǐng)求一次,不等待
? ? ?* @return
? ? ?*/
? ? @RateLimiter(value = 1.0)
? ? @GetMapping("/test1")
? ? public Dict test1() {
? ? ? ? log.info("【test1】被執(zhí)行了。。。。。");
? ? ? ? return Dict.create().set("msg", "hello,world!").set("description", "別想一直看到我,不信你快速刷新看看~");
? ? }
? ? /**
? ? ?* 接口每秒只能請(qǐng)求一次,等待一秒
? ? ?* @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實(shí)現(xiàn)單機(jī)限流的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot 單機(jī)限流 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jenkins 構(gòu)建項(xiàng)目之 pipeline基礎(chǔ)教程
​pipeline ,簡(jiǎn)單來(lái)說(shuō),就是一套運(yùn)行在 jenkins 上的工作流框架。這篇文章主要介紹了jenkins 構(gòu)建項(xiàng)目之 pipeline基礎(chǔ)教程,需要的朋友可以參考下2020-07-07
Java之字節(jié)碼以及優(yōu)勢(shì)案例講解
這篇文章主要介紹了Java之字節(jié)碼以及優(yōu)勢(shì)案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
SpringBoot Redis實(shí)現(xiàn)接口冪等性校驗(yàn)方法詳細(xì)講解
這篇文章主要介紹了SpringBoot Redis實(shí)現(xiàn)接口冪等性校驗(yàn)方法,近期一個(gè)老項(xiàng)目出現(xiàn)了接口冪等性校驗(yàn)問(wèn)題,前端加了按鈕置灰,依然被人拉著接口參數(shù)一頓輸出,還是重復(fù)調(diào)用了接口,通過(guò)復(fù)制粘貼,完成了后端接口冪等性調(diào)用校驗(yàn)2022-11-11
Java class文件格式之?dāng)?shù)據(jù)類型(二)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java class文件格式之?dāng)?shù)據(jù)類型(二)的相關(guān)資料,需要的朋友可以參考下2017-06-06
Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢
這篇文章主要介紹了Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java使用easyExcel實(shí)現(xiàn)導(dǎo)入功能
這篇文章介紹了Java使用easyExcel實(shí)現(xiàn)導(dǎo)入功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10

