首页 > 其他分享 >SpringBoot中如何编写一个优雅的限流组件?

SpringBoot中如何编写一个优雅的限流组件?

时间:2023-04-06 13:55:50浏览次数:50  
标签:SpringBoot 限流 limit key limiter 组件 type public

很早以前,我曾写过两篇介绍如何在SpringBoot中使用Guava和Redis实现接口限流的文章。具体包括:

  1. 使用Guava实现单机令牌桶限流
  2. 使用Redis实现分布式限流

现在,一个问题摆在我们面前:如何将这两种限流机制整合到同一个组件中,以便用户随时切换呢?

显然,我们需要定义一个通用的限流组件,将其引入到业务中,并支持通过配置文件自由切换不同的限流机制。举例而言,当使用limit.type=redis时,启用Redis分布式限流组件,当使用limit.type=local时,启用Guava限流组件。这种自由切换机制能够为用户提供更大的灵活性和可维护性。

接下来,让我们开始动手实现吧!

第一步,创建通用模块cloud-limiter-starter

首先在父项目下创建一个模块

image-20230317173226318

然后在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>属性。

第二步,实现限流功能

  1. 创建限流接口

既然有两种限流机制,按照套路肯定得先创建一个限流接口,就叫LimiterManager吧。

public interface LimiterManager {
    boolean tryAccess(Limiter limiter);
}
  1. 分别实现Redis的限流功能和Guava的限流功能,这里只给出核心代码。

Guava限流的核心实现GuavaLimiter

@Slf4j
public class GuavaLimiter implements LimiterManager{
    private final Map<String, RateLimiter> limiterMap = Maps.newConcurrentMap();

    @Override
    public 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

@Slf4j
public class RedisLimiter implements LimiterManager{

    private final StringRedisTemplate stringRedisTemplate;

    public RedisLimiter(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public 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限流机制。

@Configuration
public class LimiterConfigure {

    @Bean
    @ConditionalOnProperty(name = "limit.type",havingValue = "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!" );
            }
        }
    }
}

注意到类上我加了一行@Conditional(LimitAspectCondition.class),使用了自定义条件选择器,意思是只有当配置类中出现了limit.type属性时才会加载这个AOP。

public class LimitAspectCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //检查配置文件是否包含limit.type属性
        return conditionContext.getEnvironment().containsProperty(ConfigConstant.LIMIT_TYPE);
    }
}

第四步,创建spring.factories文件,引导SpringBoot加载配置类

## AutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.jianzh5.limit.config.LimiterConfigure,\
  com.jianzh5.limit.aop.LimitAspect

完整目录结构如下:

image-20230318223223264

第五步,在项目中引用限流组件

  1. 引入依赖
<dependency>
    <groupId>com.jianzh5</groupId>
    <artifactId>cloud-limit-starter</artifactId>
</dependency>
  1. 在application.properties中设置加载的限流组件
limit.type = redis

如果不配置此属性则不加载对应限流功能。

  1. 在需要限流的接口上加上注解
@Limit(key = "Limiter:test",limitNum = 3,seconds = 1)

小结

通过上述步骤,我们已经成功实现了一个通用限流组件。在实际应用中,只需要根据场景需求选择对应的限流机制,即可非常方便的进行限流操作。这种灵活性和便捷性,也是SpringBoot中定义Starter的一般套路。

如果你想详细了解这两种限流机制的原理,可以参考之前的文章中所介绍的内容。

老鸟系列源码已经上传至GitHub,需要的在公号【JAVA日知录】回复关键字 0923 获取源码地址。

标签:SpringBoot,限流,limit,key,limiter,组件,type,public
From: https://www.cnblogs.com/jianzh5/p/17292532.html

相关文章

  • Kubernetes 集群监控 controller-manager & scheduler 组件
    一、问题描述在上篇Kubernetes集群监控kube-prometheus部署我们实现kube-prometheus的安装,我们可以看到监控指标大部分的配置都是正常的,只有两个没有管理到对应的监控目标,比如kube-controller-manager和kube-scheduler这两个系统组件。没有监控目标❝备注:controller-man......
  • springboot项目图片不显示的问题
    首先确认你的图片路径是对的那么大概率就是浏览器缓存的原因,因为页面直接用的是缓存的旧数据,所以显示不出来。再不修改浏览器设置的情况下,最简单的办法就是直接项目在pom.xml文件里引入devtools如下:<dependency><groupId>org.springframework.boot</groupId><artifactId>sp......
  • SpringBoot如何进行限流,老鸟们还可以这样玩!
    大家好,我是飘渺。在SpringBoot如何进行限流,老鸟们都这么玩的!一文中我们详细介绍了为什么需要对接口进行限流,也介绍了常见的限流算法,最后还基于Guava工具类实现了接口限流。但是这种方式有个问题,无法实现分布式限流。那今天我们来利用Redis+Lua来实现分布式限流。Lua脚本......
  • Java SpringBoot Bean InitializingBean
    Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean。工厂Bean跟普通Bean不同,其返回的对象不是指定类的一个实例,其返回的是该工厂Bean的getObject方法所返回的对象。Spring初始化bean有两种方式:实现InitializingBean接口,实现afterPropertiesSet方法。(比通过......
  • 解决在创建springboot项目中遇到:Error:(3, 32) java: 无法访问org.springframework.bo
    解决在创建springboot项目中遇到:Error:(3,32)java:无法访问org.springframework.boot.SpringApplication File--->ProjectStructure    选择对应版本        File--->Settings--->Build,Execution,Deployment    修改对应版本 ......
  • 用python的pywinauto组件控制微信Windows版
    使用pywinauto组件可以比较容易的操纵微信Windows版进行信息发送和接受 前提如下1、已经安装有关python组件。2、微信已经打开和登录,下面的代码不负责登录操作。3、微信版本3.9.2.23具体实现有两个类1、帮助类HelperimportdatetimeclassHelper:"""帮助类......
  • SpringBoot启动流程
    启动类@SpringBootApplicationpublicclassApp{publicstaticvoidmain(String[]args){ SpringApplication.run(App.class,args);}}@SpringBootApplication对于一个SpringBoot程序的启动首先需要一个加了@SpringBootApplication注解的启动类。@Spr......
  • Springboot整合Seata实现分布式事务
    前言Seata是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata将为用户提供了AT、TCC、SAGA和XA事务模式,为用户打造一站式的分布式解决方案。Seata配置非常灵活,支持多种注册中心、配置来源(配置中心)和持久化方式。本文选择eureka作注册中......
  • Springboot整合TX-LCN实现分布式事务
    前言TX-LCN是一款国产分布式事务协调框架,框架其本身并不操作事务,而是基于对事务的协调从而达到事务一致性的效果。本文讲解如何使用Springboot作为基础,来配置使用TX-LCN。需要MySQL和Redis。名词解释TM(Tx-Manager/TransactionManager)事务协调者TC(Tx-Client......
  • Springboot整合Apollo配置中心
    前言参考这一篇在Linux部署Apollo配置中心可以搭建出一套Apollo配置中心服务,我们在这里重点看看Springboot如何整合Apollo,将配置交给配置中心管理,并在修改后及时生效到服务上。我们模拟工作中的开发(development,DEV)和生产(production,PRO)两套环境,在下面例子中会实现不同环境下配......