很早以前,我曾写过两篇介绍如何在SpringBoot中使用Guava和redis实现接口限流的文章 。具体包括:
- 使用Guava实现单机令牌桶限流
- 使用Redis实现分布式限流
显然,我们需要定义一个通用的限流组件,将其引入到业务中,并支持通过配置文件自由切换不同的限流机制 。举例而言,当使用limit.type=redis时,启用Redis分布式限流组件,当使用limit.type=local时,启用Guava限流组件 。这种自由切换机制能够为用户提供更大的灵活性和可维护性 。
接下来,让我们开始动手实现吧!
第一步,创建通用模块cloud-limiter-starter首先在父项目下创建一个模块
文章插图
然后在pom文件中引入相关依赖
<dependencies><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId></dependency><!--SpringFramework--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><scope>provided</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><scope>provided</scope></dependency></dependencies>
小提示:通用模块命名最好遵照规则以starter命名结束,同时通用模块引入的依赖最好设置<scope>provided</scope>属性 。第二步,实现限流功能
- 创建限流接口
public interface LimiterManager {boolean tryAccess(Limiter limiter);}
- 分别实现Redis的限流功能和Guava的限流功能,这里只给出核心代码 。
@Slf4jpublic class GuavaLimiter implements LimiterManager{private final Map<String, RateLimiter> limiterMap = Maps.newConcurrentMap();@Overridepublic boolean tryAccess(Limiter limiter) {RateLimiter rateLimiter = getRateLimiter(limiter);if (rateLimiter == null) {return false;}boolean access = rateLimiter.tryAcquire(1,100, TimeUnit.MILLISECONDS);log.info("{} access :{}",limiter.getKey() , access);return access;}}
Redis限流的核心实现RedisLimiter@Slf4jpublic class RedisLimiter implements LimiterManager{private final StringRedisTemplate stringRedisTemplate;public RedisLimiter(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}@Overridepublic boolean tryAccess(Limiter limiter) {String key = limiter.getKey();if (StringUtils.isEmpty(key)) {throw new LimiterException( "redis limiter key cannot be null" );}List<String> keys = new ArrayList<>();keys.add( key );int seconds = limiter.getSeconds();int limitCount = limiter.getLimitNum();String luaScript = buildLuaScript();RedisScript<Long> redisScript = new DefaultRedisScript<>(luaScript, Long.class);Long count = stringRedisTemplate.execute( redisScript, keys, "" + limitCount, "" + seconds );log.info( "Access try count is {} for key={}", count, key );return count != null && count != 0;}}
第三步,创建配置类编写配置类根据配置文件注入限流实现类,当配置文件中属性 limit.type=local 时启用Guava限流机制,当limit.type=redis 时启用Redis限流机制 。@Configurationpublic class LimiterConfigure {@Bean@ConditionalOnProperty(name = "limit.type",havingValue = https://www.isolves.com/it/cxkf/kj/2023-03-24/"local")public LimiterManager guavaLimiter(){return new GuavaLimiter();}@Bean@ConditionalOnProperty(name = "limit.type",havingValue = "redis")public LimiterManager redisLimiter(StringRedisTemplate stringRedisTemplate){return new RedisLimiter(stringRedisTemplate);}}
第四步,创建AOP根据前面的两篇文章可知,避免限流功能污染业务逻辑的最好方式是借助Spring AOP,所以很显然还得需要创建一个AOP 。@Aspect@EnableAspectJAutoProxy(proxyTargetClass = true) //使用CGLIB代理@Conditional(LimitAspectCondition.class)public class LimitAspect {@Setter(onMethod_ = @Autowired)private LimiterManager limiterManager;@Pointcut("@annotation(com.jianzh5.limit.aop.Limit)")private void check() {}@Before("check()")public void before(JoinPoint joinPoint){MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();Limit limit = method.getAnnotation(Limit.class);if(limit != null){Limiter limiter = Limiter.builder().limitNum(limit.limitNum()).seconds(limit.seconds()).key(limit.key()).build();if(!limiterManager.tryAccess(limiter)){throw new LimiterException( "There are currently many people , please try again later!" );}}}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- SpringBoot中使用PostgreSQL数据库
- JavaScript中根据字符串中的范围规则,判断当前值是否符合条件
- SpringBoot对SpringMVC的自动配置,你知道多少?
- 李彦宏:中国基本不会再出一个OpenAI,创业公司重做ChatGPT没多大意义
- 一道题,测试你是否“睡眠障碍”,中医推荐几个方法缓解失眠
- 罗兰|曝于文红老公出轨成性!曾睡4000多名中国姑娘,每月零用钱仅5万
- 泽口靖子|昭和美人泽口靖子,皇太子的梦中情人,世界首富的追求对象
- 胡萝卜里面有什么维生素
- 小茴香能下奶吗
- 李崇霄|中国首位公开艾滋病女孩朱力亚,自曝被黑人男友传染,却仍爱他很深