JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼
基本步驟:
1.自定義注解 @RateLimiter,添加屬性:key(redis中key前綴)、time(redis中key過期時(shí)間,單位秒)、count(time中定義時(shí)間段內(nèi)可以訪問的次數(shù)),limitType(限流類型:1.限制接口;2.限制ip)
2.創(chuàng)建AOP,定義前置通知,獲取@RateLimiter注解上的各項(xiàng)值,客戶端每訪問一次接口,redis中存儲(chǔ)的current(當(dāng)前時(shí)間段內(nèi)已訪問的次數(shù))值+1,若current值大于count規(guī)定的值,拋出異常,提示到達(dá)允許訪問的次數(shù);
具體代碼如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- redis json序列化 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.9</version> </dependency>
1.創(chuàng)建枚舉類,定義限流類型
public enum LimitType { /** * 默認(rèn)限流策略,針對(duì)某一個(gè)接口進(jìn)行限流 */ DEFAULT, /** * 根據(jù)IP地址進(jìn)行限流 */ IP; }
2.自定義注解@RateLimiter
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface RateLimiter { /** * 定義redis中限流key前綴 rate_limit:com.xxx.controller.HelloController-hello //HelloController中的hello方法 */ String key() default "rate_limit:"; /** * 限流時(shí)間,單位秒 * @return */ int time() default 60; /** * 限流時(shí)間內(nèi)限流次數(shù) * @return */ int count() default 100; /** * 限流類型:1.限制接口訪問次數(shù) 2.限制ip訪問次數(shù) * @return */ LimitType limitType() default LimitType.DEFAULT; }
3.創(chuàng)建lua腳本,保證redis中操作原子性(在resources/lua目錄下創(chuàng)建rateLimit.lua文件)
---定義變量:redis中key值,redis中過期時(shí)間,規(guī)定的時(shí)間段內(nèi)訪問次數(shù),當(dāng)前訪問次數(shù) local key = KEYS[1] local time = tonumber(ARGV[1]) local count = tonumber(ARGV[2]) local current = redis.call('get',key) if current and tonumber(current) > count then ---如果current不為空當(dāng)前訪問次數(shù)大于規(guī)定時(shí)間段內(nèi)的訪問次數(shù) return tonumber(current) ---返回當(dāng)前訪問的次數(shù) end current = redis.call('incr',key) --- 若沒超過限制次數(shù),當(dāng)前次數(shù)+1(自增) if tonumber(current)==1 then redis.call('expire',key,time) --- 如果當(dāng)前訪問次數(shù)=1,表示第一次訪問,設(shè)置當(dāng)前key的過期時(shí)間 end return tonumber(current) --返回tonumber(current)
4.創(chuàng)建配置類,解決redis序列化與讀取lua腳本
@Configuration public class RedisConfig { //Redis序列化 @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); //首先解決key的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); //解決value的序列化方式 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); //序列化時(shí)將類的數(shù)據(jù)類型存入json,以便反序列化的時(shí)候轉(zhuǎn)換成正確的類型 ObjectMapper objectMapper = new ObjectMapper(); //objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); // 解決jackson2無法反序列化LocalDateTime的問題 objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.registerModule(new JavaTimeModule()); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); return redisTemplate; } /** * 定義lua腳本 */ @Bean public DefaultRedisScript<Long> limitScript(){ DefaultRedisScript<Long> script = new DefaultRedisScript<>(); script.setResultType(Long.class);//設(shè)置lua腳本返回值類型 需要同lua腳本中返回值一致 script.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua\\rateLimit.lua")));//讀取lua文件 return script; } }
5.自定義限流異常與全局異常
//自定義限流異常 public class RateLimitException extends RuntimeException{ ? ? private Integer code; ? ? private String msg; ? ? public RateLimitException(Integer code, String msg) { ? ? ? ? this.code = code; ? ? ? ? this.msg = msg; ? ? } ? ? public Integer getCode() { ? ? ? ? return code; ? ? } ? ? public void setCode(Integer code) { ? ? ? ? this.code = code; ? ? } ? ? public String getMsg() { ? ? ? ? return msg; ? ? } ? ? public void setMsg(String msg) { ? ? ? ? this.msg = msg; ? ? } } //全局異常 @RestControllerAdvice public class GlobalException { ? ? @ExceptionHandler(RateLimitException.class) ? ? public Map rateLimit(RateLimitException e){ ? ? ? ? Map<String, Object> map = new HashMap<>(); ? ? ? ? map.put("code",e.getCode()); ? ? ? ? map.put("msg",e.getMsg()); ? ? ? ? return map; ? ? } }
6.創(chuàng)建切面,進(jìn)行限流操作
@Aspect @Component public class RateLimitAspect { private static final Logger logger= LoggerFactory.getLogger(RateLimitAspect.class); @Autowired RedisTemplate<String,Object> redisTemplate; @Autowired RedisScript<Long> redisScript; //實(shí)現(xiàn)類為DefaultRedisScript<Long> limitScript() @Pointcut("@annotation(com.xxx.annotation.RateLimiter)") public void pointCut(){} @Before("pointCut()") public void beforeRateLimit(JoinPoint jp){ //獲取RateLimiter注解上的值 MethodSignature methodSignature = (MethodSignature) jp.getSignature(); RateLimiter rateLimiter = AnnotationUtils.findAnnotation(methodSignature.getMethod(), RateLimiter.class); int time = rateLimiter.time(); int count = rateLimiter.count(); //構(gòu)建redis中的key值 String rateKey=getRateLimitKey(rateLimiter,methodSignature); System.out.println("redis中key值:"+rateKey); try { Long current = redisTemplate.execute(redisScript, Collections.singletonList(rateKey), time, count); if(current==null||current.intValue()>count){ logger.info("當(dāng)前接口達(dá)到最大限流次數(shù)"); throw new RateLimitException(500,"當(dāng)前接口達(dá)到最大限流次數(shù)"); } logger.info("一段時(shí)間內(nèi)允許的請(qǐng)求次數(shù):{},當(dāng)前請(qǐng)求次數(shù):{},緩存的key為:{}",count,current,rateKey); } catch (Exception e) { throw e; } } /** * redis中key兩種類型格式為: * 1. rate_limit:com.xxx.controller.HelloController-hello * 2. rate_limit:127.0.0.1-com.xxx.controller.HelloController-hello * @param rateLimiter * @param methodSignature * @return */ private String getRateLimitKey(RateLimiter rateLimiter, MethodSignature methodSignature) { StringBuffer key = new StringBuffer(rateLimiter.key()); if(rateLimiter.limitType()== LimitType.IP){//如果參數(shù)類型為IP //獲取客戶端ip String clientIP = ServletUtil.getClientIP(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()); key.append(clientIP+"-"); } Method method = methodSignature.getMethod(); //獲取全類名 String className = method.getDeclaringClass().getName(); //獲取方法名 String methodName = method.getName(); key.append(className) .append("-") .append(methodName); System.out.println("全類名+方法名 "+className+"-"+methodName); return key.toString(); } }
7.創(chuàng)建接口,進(jìn)行驗(yàn)證
@RestController public class HelloController { @GetMapping("/hello") @RateLimiter(time = 10,count = 3,limitType = LimitType.DEFAULT) //10秒內(nèi)允許訪問三次 public String hello(){ return "hello"; } }
gitee開源地址:
RateLimiter: JAVA自定義注解實(shí)現(xiàn)接口/ip限流
到此這篇關(guān)于JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼的文章就介紹到這了,更多相關(guān)JAVA 接口/ip限流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot JSON全局日期格式轉(zhuǎn)換器實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot JSON全局日期格式轉(zhuǎn)換器,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04java實(shí)現(xiàn)把兩個(gè)有序數(shù)組合并到一個(gè)數(shù)組的實(shí)例
今天小編就為大家分享一篇java實(shí)現(xiàn)把兩個(gè)有序數(shù)組合并到一個(gè)數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的真正理解
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11詳談spring中bean注入無效和new創(chuàng)建對(duì)象的區(qū)別
這篇文章主要介紹了spring中bean注入無效和new創(chuàng)建對(duì)象的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java實(shí)現(xiàn)5種負(fù)載均衡算法(小結(jié))
負(fù)載均衡是將客戶端請(qǐng)求訪問,通過提前約定好的規(guī)則轉(zhuǎn)發(fā)給各個(gè)server,本文主要介紹了Java實(shí)現(xiàn)5種負(fù)載均衡算法,具有一定的參考價(jià)值,感興趣的可以了解一下2022-06-06SpringAop自定義切面注解、自定義過濾器及ThreadLocal詳解
這篇文章主要介紹了SpringAop自定義切面注解、自定義過濾器及ThreadLocal詳解,Aspect(切面)通常是一個(gè)類,里面可以定義切入點(diǎn)和通知(切面 = 切點(diǎn)+通知),execution()是最常用的切點(diǎn)函數(shù),需要的朋友可以參考下2024-01-01Java如何接收XML格式參數(shù)并轉(zhuǎn)換為JSON
在 Java 應(yīng)用程序中,處理 XML 數(shù)據(jù)并將其轉(zhuǎn)換為 JSON 格式是很常見的任務(wù),這篇文章為大家整理了一下具體的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助2025-03-03