移花宫资源网 Design By www.nbddc.com
创建限流组件项目
pom.xml文件中引入相关依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> </dependencies>
在resources目录下创建lua脚本 ratelimiter.lua
-- -- Created by IntelliJ IDEA. -- User: 寒夜 -- -- 获取方法签名特征 local methodKey = KEYS[1] redis.log(redis.LOG_DEBUG, 'key is', methodKey) -- 调用脚本传入的限流大小 local limit = tonumber(ARGV[1]) -- 获取当前流量大小 local count = tonumber(redis.call('get', methodKey) or "0") -- 是否超出限流阈值 if count + 1 > limit then -- 拒绝服务访问 return false else -- 没有超过阈值 -- 设置当前访问的数量+1 redis.call("INCRBY", methodKey, 1) -- 设置过期时间 redis.call("EXPIRE", methodKey, 1) -- 放行 return true end
创建RedisConfiguration 类
package com.imooc.springcloud; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.script.DefaultRedisScript; /** * @author 寒夜 */ @Configuration public class RedisConfiguration { @Bean public RedisTemplate<String, String> redisTemplate( RedisConnectionFactory factory) { return new StringRedisTemplate(factory); } @Bean public DefaultRedisScript loadRedisScript() { DefaultRedisScript redisScript = new DefaultRedisScript(); redisScript.setLocation(new ClassPathResource("ratelimiter.lua")); redisScript.setResultType(java.lang.Boolean.class); return redisScript; } }
创建一个自定义注解
package com.hy.annotation; import java.lang.annotation.*; /** * @author 寒夜 */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface AccessLimiter { int limit(); String methodKey() default ""; }
创建一个切入点
package com.hy.annotation; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.script.RedisScript; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.lang.reflect.Method; import java.util.Arrays; import java.util.stream.Collectors; /** * @author 寒夜 */ @Slf4j @Aspect @Component public class AccessLimiterAspect { private final StringRedisTemplate stringRedisTemplate; private final RedisScript<Boolean> rateLimitLua; public AccessLimiterAspect(StringRedisTemplate stringRedisTemplate, RedisScript<Boolean> rateLimitLua) { this.stringRedisTemplate = stringRedisTemplate; this.rateLimitLua = rateLimitLua; } @Pointcut(value = "@annotation(com.hy.annotation.AccessLimiter)") public void cut() { log.info("cut"); } @Before("cut()") public void before(JoinPoint joinPoint) { // 1. 获得方法签名,作为method Key MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); AccessLimiter annotation = method.getAnnotation(AccessLimiter.class); if (annotation == null) { return; } String key = annotation.methodKey(); int limit = annotation.limit(); // 如果没设置methodkey, 从调用方法签名生成自动一个key if (StringUtils.isEmpty(key)) { Class[] type = method.getParameterTypes(); key = method.getClass() + method.getName(); if (type != null) { String paramTypes = Arrays.stream(type) .map(Class::getName) .collect(Collectors.joining(",")); log.info("param types: " + paramTypes); key += "#" + paramTypes; } } // 2. 调用Redis boolean acquired = stringRedisTemplate.execute( rateLimitLua, // Lua script的真身 Lists.newArrayList(key), // Lua脚本中的Key列表 Integer.toString(limit) // Lua脚本Value列表 ); if (!acquired) { log.error("your access is blocked, key={}", key); throw new RuntimeException("Your access is blocked"); } } }
创建测试项目
pom.xml中引入组件
application.yml配置
spring: redis: host: 192.168.0.218 port: 6379 password: 123456 database: 0 application: name: ratelimiter-test server: port: 10087
创建controller
package com.hy; import com.hy.annotation.AccessLimiter; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 寒夜 */ @RestController @Slf4j public class Controller { private final com.hy.AccessLimiter accessLimiter; public Controller(com.hy.AccessLimiter accessLimiter) { this.accessLimiter = accessLimiter; } @GetMapping("test") public String test() { accessLimiter.limitAccess("ratelimiter-test", 3); return "success"; } // 提醒! 注意配置扫包路径(com.hy路径不同) @GetMapping("test-annotation") @AccessLimiter(limit = 1) public String testAnnotation() { return "success"; } }
开始测试,快速点击结果如下
移花宫资源网 Design By www.nbddc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
移花宫资源网 Design By www.nbddc.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年06月01日
2024年06月01日
- PUCK《天蓝色的彼岸》[FLAC/分轨][74.02MB]
- Edward Chan《Blueprint of Memories》[320K/MP3][48.3MB]
- 张敬轩.2007-MY1STCOLLECTION【环球】2CD【WAV+CUE】
- 著名歌手绝美音色经典老歌《超级音效王》DTS-WAV
- 流行靓声-群星《情网HQ》DTS[WAV]
- 群星《经典情歌对唱迟来的爱HQ》DTS[WAV]
- 群星2024《传承(太平洋影音四十五周年纪念专辑)4CD》[FLAC]
- 太平洋唱片中国轻音乐1-10全集[竹锦+影视篇+民歌篇+创作篇等]10CD[APE分轨+CUE]
- 群星《2024第一季度百度排行100首》[FLAC/分轨][2.3G]
- 证声音乐图书馆《聆听·心流·爵士 Flow Jazz》[320K/MP3][77.28MB]
- 证声音乐图书馆《聆听·心流·爵士 Flow Jazz》[FLAC/分轨][132.74MB]
- dnf手游为什么不上线了
- dnf手游韩服怎么重新转职
- 雷婷《婷婷醉歌》发烧版K2HD[正版CD低速原抓WAV+CUE]
- 西卿.1997-西北风(布袋戏精选)【金瓜石】【WAV+CUE】