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 設置一組屬性的別名,所以獲取注解的時候,需要通過 Spring 提供的注解工具類 AnnotationUtils 獲取,不可以通過 AOP 參數(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)文章
Java中shiro框架和security框架的區(qū)別
這篇文章主要介紹了Java中shiro框架和security框架的區(qū)別,shiro和security作為兩款流行的功能強大的且易于使用的java安全認證框架,在近些年中的項目開發(fā)過程中使用廣泛,今天我們就來一起了解一下兩者的區(qū)別2023-08-08
springboot啟動報錯:application?startup?failed問題
這篇文章主要介紹了springboot啟動報錯:application?startup?failed問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot+nacos+gateway實現(xiàn)灰度發(fā)布的實例詳解
灰度發(fā)布是一種在軟件部署過程中用于平滑過渡的技術(shù),通過引入灰度發(fā)布SDK和配置網(wǎng)關(guān)策略實現(xiàn),本文就來介紹一下,感興趣的可以了解一下2022-03-03
mybatis返回map類型數(shù)據(jù)空值字段不顯示的解決方案
這篇文章主要介紹了mybatis返回map類型數(shù)據(jù)空值字段不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

