当前位置: 首页 > news >正文

一对一视频app开发,基于Redis+Lua实现分布式限流 - 云豹科技

一对一视频app开发,基于Redis+Lua实现分布式限流

一、新建一个Mavne项目,取名为rate_limiter,并引入Lombok和guava的依赖。

<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
</dependency>
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>29.0-jre</version>
</dependency>

二、在rate_limiter项目下新建一个名为ratelimiter_annotation的子模块,在该模块的pom文件中添加redis的依赖。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

三、在ratelimiter_annotation模块的src/main/java目录下创建service包,在service包下创建一个名为AccessLimiter的类。

@Service
@Slf4j
public class AccessLimiter {@Autowiredprivate StringRedisTemplate redisTemplate;/*** DefaultRedisScript类用来加载脚本的,并设置相应的数据类型来接收lua脚本返回的数据,* 这个泛型类在使用时设置泛型是什么类型,脚本返回的结果就是用什么类型接收。* 该类只接收4种类型的返回类型(Long, Boolean, List, or deserialized value type)*/@Autowiredprivate DefaultRedisScript<Boolean> rateLimiterLua;public void limitAccess(String key,Integer limit){//执行lua脚本boolean acquire=redisTemplate.execute(rateLimiterLua,Lists.newArrayList(key),limit.toString());if (!acquire){log.error("your access is blocked,key={}",key);throw new RuntimeException("your access is blocked");}}
}

四、新建config包并创建名为RedisConfiguration的配置类

@Configuration
public class RedisConfiguration {@Beanpublic RedisTemplate<String,String> redisTemplate(RedisConnectionFactory factory){return new StringRedisTemplate(factory);}@Beanpublic DefaultRedisScript loadRedisScript(){DefaultRedisScript redisScript=new DefaultRedisScript();//设置lua脚本redisScript.setLocation(new ClassPathResource("ratelimiter.lua"));//设置返回类型redisScript.setResultType(java.lang.Boolean.class);return redisScript;}
}

五、在resources目录下新建lua脚本文件ratelimiter.lua。

--
-- Created by IntelliJ IDEA.
-- User: wanglei
--
-- 在lua脚本中,有两个全局的变量,是用来接收redis应用端传递的键值和其它参数的,
-- 分别为KEYS、ARGV。-- 在应用端传递给KEYS时是一个数组列表,在lua脚本中通过索引方式获取数组内的值。-- 在应用端,传递给ARGV的参数比较灵活,可以是多个独立的参数,但对应到Lua脚本中是,
-- 统一用ARGV这个数组接收,获取方式也是通过数组下标获取。-- 通过KEYS获取方法签名特征
local methodKey = KEYS[1]
redis.log(redis.LOG_DEBUG, 'key is', methodKey)-- 通过ARGV传入限流大小
local limit = tonumber(ARGV[1])-- 获取当前流量大小
local count = tonumber(redis.call('get', methodKey) or "0")-- 是否超出限流阈值
if count + 1 > limit then-- 拒绝服务访问return false
else-- 没有超过阈值-- 设置当前访问的数量+1redis.call("INCRBY", methodKey, 1)-- 设置过期时间redis.call("EXPIRE", methodKey, 1)-- 放行return true
end

六、在rate_limiter项目中再新建一个ratelimiter_test的子模块用于测试我们前面的脚本。在ratelimiter_test中引入以下依赖。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency><groupId>${project.groupId}</groupId><artifactId>ratelimiter_annotation</artifactId><version>${project.version}</version>
</dependency>

七、在ratelimiter_test的src/main/java下新建controller包,并在controller包下创建一个TestController的类。

@RestController
@Slf4j
public class TestController {@Autowiredprivate AccessLimiter accessLimiter;@GetMapping("test")public String test(){accessLimiter.limitAccess("ratelimiter-test",1);return "success";}
}

八、在application.properties中添加redis的配置

spring.redis.database=0
spring.redis.host=localhsot
spring.redis.port=6379
spring.redis.password=root

九、创建一个启动类并启动项目,在postman中测试一下查看限流的结果。

@SpringBootApplication
public class RatelimiterTestApplication {public static void main(String[] args) {SpringApplication.run(RatelimiterTestApplication.class, args);}}

十、通过以上的几个步骤,已经实现了基于Redis+Lua的限流,但是代码还不够完美,现在我们将项目改造一下,通过自定义的注解在项目的任何位置都可以实现限流。

先在ratelimiter_annotation模块中引入aop的依赖。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>

然后在ratelimiter_annotation模块中新建一个annotation的包,并在annotation包下创建一个名为AccessLimiter的注解。

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AccessLimiter {int limit();String methodKey() default "";
}

再创建一个aspect的包,并创建一个名为AccessLimiterAspect的类

@Slf4j
@Aspect
@Component
public class AccessLimiterAspect {@Autowiredprivate StringRedisTemplate redisTemplate;@Autowiredprivate DefaultRedisScript<Boolean> rateLimiterLua;@Pointcut("@annotation(com.wl.annotation.AccessLimiter)")public void cut(){log.info("cut");}@Before("cut()")public void before(JoinPoint joinPoint){//1、获得方法签名,作为method keyMethodSignature methodSignature= (MethodSignature) joinPoint.getSignature();Method method=methodSignature.getMethod();AccessLimiter annotation=method.getAnnotation(AccessLimiter.class);if (annotation==null){return;}String key=annotation.methodKey();Integer limit=annotation.limit();//如果没有设置methodKey,从调用方法签名自动生成一个if (StringUtils.isEmpty(key)){Class[] type=method.getParameterTypes();key=method.getName();if (type!=null){String paramTypes= Arrays.stream(type).map(Class::getName).collect(Collectors.joining(","));log.info("param types: "+paramTypes);key+="#"+paramTypes;}}//2、调用redisboolean acquire=redisTemplate.execute(rateLimiterLua,Lists.newArrayList(key),limit.toString());if (!acquire){log.error("your access is blocked,key={}",key);throw new RuntimeException("your access is blocked");}}
}

现在我们九可以使用我们自定义的注解了,我们在TestController新增一个方法

@GetMapping("test-annotation")
@com.wl.annotation.AccessLimiter(limit = 1)
public String testAnnotation(){return "success";
}

通过启动类再次启动我们的项目并测试一下testAnnotation接口。

以上就是一对一视频app开发,基于Redis+Lua实现分布式限流, 更多内容欢迎关注之后的文章

http://www.jsqmd.com/news/40914/

相关文章:

  • Nginx配置proxy转发其他域名502 - Commissar
  • 2025年高温抗氧化试验炉制造企业权威推荐榜单:回转抗渣炉/中温试验炉/全自动重烧试验炉设备源头厂家精选
  • 2025 最新推荐!绝缘监测系统厂家权威榜单:变频设备 / 高压电机专用监测方案深度解析
  • 2025年制氮机生产厂家权威推荐榜单:PSA制氮机/制氮机组/PSA变压吸附制氮机源头厂家精选
  • 2025年11月牛初乳品牌口碑榜:五强实测评价与排行指南
  • 2025年11月牛初乳品牌推荐榜:免疫营养指标与认证资质全对比
  • 完整教程:Python - PEP 738 – 将 Android 添加为支持平台
  • 2025年11月AI智能客服机器人品牌推荐:性能评价榜全维度盘点
  • 如何禁用XFCE的屏幕锁
  • 2025年11月上海遗产继承律师评测榜:五强名单与选择策略
  • 05_杂记 学习方法很重要!
  • 2025年11月上海遗产继承律师评价榜:五强机构数据全解析
  • 2025年中小学生AI学习机怎么选?这品牌凭技术封神,全学段都适配!
  • 2025年11月北京物流公司推荐榜:热门企业对比与口碑排行
  • 2025年11月消音室厂家推荐榜:五强对比与性能全解析
  • 2025年11月消音室厂家推荐排名榜:五强性能数据与认证资质一览
  • 2025年11月北京物流公司优选榜:五强对比与权威排行
  • 2025年11月隔音室厂家榜单:五强口碑与实测数据综合排行
  • 2025年11月隔音室厂家推荐榜:一站式选购指南与厂家排名榜
  • 2025年11月超声波清洗机厂家推荐排行:五家主流品牌深度对比评价
  • 2025年隔音室厂家联系方式推荐:精选推荐与使用指南
  • 2025年11月隔音室厂家排名榜:五强品牌数据化选购参考
  • 2025年11月超声波清洗机厂家推荐榜:五强对比看技术实力与服务差异
  • New Inventory System Plugin
  • 14-无监督学习:讲解无需标注资料的数据分析和模式发现技巧
  • Go日志之日志库讲解
  • Why are Germans superior to Latins
  • Why are monarchies so good
  • 管桁架拼装工厂排行榜、锥形管厂家排名、变径管源头厂家,相贯线工厂怎么选、网架源头厂家、冷弯生产厂家、光伏桁架公司
  • docker: 报错:Error response from daemon: Conflict. The container name is already in use by container