From 37db08a2dc2a2b0ceb6c15ee8caedd291d4ff223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Sat, 11 Mar 2023 22:27:15 +0800 Subject: [PATCH] =?UTF-8?q?redis=E9=99=90=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis限流/pom.xml | 138 ++++++++++++++++++ .../feign/TLogFeignConsumerRunner.java | 19 +++ .../tlog/example/feign/aop/RedisLimit.java | 30 ++++ .../tlog/example/feign/aop/RedisLimitAop.java | 117 +++++++++++++++ .../feign/configuration/Log4j2Rule.java | 41 ++++++ .../feign/configuration/RedisConfig.java | 33 +++++ .../controller/LimitRedisController.java | 29 ++++ .../exception/GlobalExceptionHandler.java | 29 ++++ .../feign/exception/RedisLimitException.java | 12 ++ .../tlog/example/feign/vo/ResultInfo.java | 28 ++++ .../src/main/resources/application.properties | 19 +++ .../src/main/resources/rateLimiter.lua | 16 ++ .../core/config/plugins/Log4j2Plugins.dat | Bin 0 -> 111 bytes .../target/classes/application.properties | 19 +++ .../feign/TLogFeignConsumerRunner.class | Bin 0 -> 880 bytes .../tlog/example/feign/aop/RedisLimit.class | Bin 0 -> 658 bytes .../example/feign/aop/RedisLimitAop.class | Bin 0 -> 4418 bytes .../feign/configuration/Log4j2Rule.class | Bin 0 -> 1210 bytes .../feign/configuration/RedisConfig.class | Bin 0 -> 1951 bytes .../controller/LimitRedisController.class | Bin 0 -> 1239 bytes .../exception/GlobalExceptionHandler.class | Bin 0 -> 1389 bytes .../feign/exception/RedisLimitException.class | Bin 0 -> 434 bytes .../tlog/example/feign/vo/ResultInfo.class | Bin 0 -> 1675 bytes redis限流/target/classes/rateLimiter.lua | 16 ++ .../tlog-example-logback-feign-consumer.iml | 136 +++++++++++++++++ 25 files changed, 682 insertions(+) create mode 100644 redis限流/pom.xml create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/aop/RedisLimit.java create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/aop/RedisLimitAop.java create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/configuration/Log4j2Rule.java create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/configuration/RedisConfig.java create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/controller/LimitRedisController.java create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/exception/GlobalExceptionHandler.java create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/exception/RedisLimitException.java create mode 100644 redis限流/src/main/java/com/yomahub/tlog/example/feign/vo/ResultInfo.java create mode 100644 redis限流/src/main/resources/application.properties create mode 100644 redis限流/src/main/resources/rateLimiter.lua create mode 100644 redis限流/target/classes/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat create mode 100644 redis限流/target/classes/application.properties create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.class create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/aop/RedisLimit.class create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/aop/RedisLimitAop.class create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/configuration/Log4j2Rule.class create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/configuration/RedisConfig.class create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/controller/LimitRedisController.class create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/exception/GlobalExceptionHandler.class create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/exception/RedisLimitException.class create mode 100644 redis限流/target/classes/com/yomahub/tlog/example/feign/vo/ResultInfo.class create mode 100644 redis限流/target/classes/rateLimiter.lua create mode 100644 redis限流/tlog-example-logback-feign-consumer.iml diff --git a/redis限流/pom.xml b/redis限流/pom.xml new file mode 100644 index 0000000..f74f39b --- /dev/null +++ b/redis限流/pom.xml @@ -0,0 +1,138 @@ + + + + + + 8 + 8 + 2.1.8.RELEASE + Greenwich.SR3 + 2.1.0.RELEASE + 1.8 + 1.7.25 + 2.12.0 + 1.2.17 + 1.6.1 + 2.7.6 + 2.8.4 + 4.0.1 + 3.4.6 + 0.1 + 1.0.0 + 1.5.0 + + + 4.0.0 + com.yomahub.tlog.example + tlog-example-logback-feign-consumer + 1.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-logging + + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + com.google.guava + guava + 30.1-jre + + + + org.springframework.boot + spring-boot-starter-aop + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + + + + com.yomahub + tlog-feign-spring-boot-starter + ${tlog.version} + + + com.yomahub + tlog-web-spring-boot-starter + ${tlog.version} + + + org.projectlombok + lombok + + + junit + junit + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + + log4j + log4j + ${log4j.version} + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + + + diff --git a/redis限流/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java b/redis限流/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java new file mode 100644 index 0000000..b44c806 --- /dev/null +++ b/redis限流/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java @@ -0,0 +1,19 @@ +package com.yomahub.tlog.example.feign; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.scheduling.annotation.EnableAsync; + +@SpringBootApplication +@EnableFeignClients +//@EnableDiscoveryClient +@EnableAsync +public class TLogFeignConsumerRunner { + + public static void main(String[] args) { + SpringApplication.run(TLogFeignConsumerRunner.class, args); + + } +} diff --git a/redis限流/src/main/java/com/yomahub/tlog/example/feign/aop/RedisLimit.java b/redis限流/src/main/java/com/yomahub/tlog/example/feign/aop/RedisLimit.java new file mode 100644 index 0000000..aa83e6a --- /dev/null +++ b/redis限流/src/main/java/com/yomahub/tlog/example/feign/aop/RedisLimit.java @@ -0,0 +1,30 @@ +package com.yomahub.tlog.example.feign.aop; + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +@Documented +public @interface RedisLimit { + /** + * 资源的key,唯一 + * 作用:不同的接口,不同的流量控制 + */ + String key() default ""; + + /** + * 最多的访问限制次数 + */ + long permitsPerSecond() default 2; + + /** + * 过期时间也可以理解为单位时间,单位秒,默认60 + */ + long expire() default 60; + + + /** + * 得不到令牌的提示语 + */ + String msg() default "系统繁忙,请稍后再试."; +} diff --git a/redis限流/src/main/java/com/yomahub/tlog/example/feign/aop/RedisLimitAop.java b/redis限流/src/main/java/com/yomahub/tlog/example/feign/aop/RedisLimitAop.java new file mode 100644 index 0000000..a739dba --- /dev/null +++ b/redis限流/src/main/java/com/yomahub/tlog/example/feign/aop/RedisLimitAop.java @@ -0,0 +1,117 @@ +package com.yomahub.tlog.example.feign.aop; + +import com.yomahub.tlog.example.feign.exception.RedisLimitException; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; +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.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.script.DefaultRedisScript; +import org.springframework.data.redis.core.script.RedisScript; +import org.springframework.scripting.support.ResourceScriptSource; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +/** + * Limit AOP + */ +@Slf4j +@Aspect +@Component +public class RedisLimitAop { + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + + @Pointcut("@annotation(com.yomahub.tlog.example.feign.aop.RedisLimit)") + private void check() { + + } + + private DefaultRedisScript redisScript; + + @PostConstruct + public void init() { + redisScript = new DefaultRedisScript<>(); + redisScript.setResultType(Long.class); + redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("rateLimiter.lua"))); + } + + + @Before("check()") + public void before(JoinPoint joinPoint) { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + + //拿到RedisLimit注解,如果存在则说明需要限流 + RedisLimit redisLimit = method.getAnnotation(RedisLimit.class); + + if (redisLimit != null) { + //获取redis的key + String key = redisLimit.key(); + String className = method.getDeclaringClass().getName(); + String name = method.getName(); + + String limitKey = key + className + method.getName(); + + log.info(limitKey); + + if (StringUtils.isEmpty(key)) { + throw new RedisLimitException("key cannot be null"); + } + + long limit = redisLimit.permitsPerSecond(); + + long expire = redisLimit.expire(); + + List keys = new ArrayList<>(); + keys.add(key); + + Long count = stringRedisTemplate.execute(redisScript, keys, String.valueOf(limit), String.valueOf(expire)); + + log.info("Access try count is {} for key={}", count, key); + + if (count != null && count == 0) { + log.debug("获取key失败,key为{}", key); + throw new RedisLimitException(redisLimit.msg()); + } + } + + } + +// /** +// * 构建redis lua脚本 +// * +// * @return +// */ +// private String buildLuaScript() { +// StringBuilder luaString = new StringBuilder(); +// luaString.append("local key = KEYS[1]"); +// //获取ARGV内参数Limit +// luaString.append("\nlocal limit = tonumber(ARGV[1])"); +// //获取key的次数 +// luaString.append("\nlocal curentLimit = tonumber(redis.call('get', key) or '0')"); +// luaString.append("\nif curentLimit + 1 > limit then"); +// luaString.append("\nreturn 0"); +// luaString.append("\nelse"); +// //自增长 1 +// luaString.append("\n redis.call('INCRBY', key, 1)"); +// //设置过期时间 +// luaString.append("\nredis.call('EXPIRE', key, ARGV[2])"); +// luaString.append("\nend"); +// return luaString.toString(); +// } +// + + +} \ No newline at end of file diff --git a/redis限流/src/main/java/com/yomahub/tlog/example/feign/configuration/Log4j2Rule.java b/redis限流/src/main/java/com/yomahub/tlog/example/feign/configuration/Log4j2Rule.java new file mode 100644 index 0000000..701b441 --- /dev/null +++ b/redis限流/src/main/java/com/yomahub/tlog/example/feign/configuration/Log4j2Rule.java @@ -0,0 +1,41 @@ +package com.yomahub.tlog.example.feign.configuration; + + +import java.util.HashMap; +import java.util.Map; + +/** + * 现在拦截加密的日志有三类: + * 1,身份证 + * 2,姓名 + * 3,身份证号 + * 加密的规则后续可以优化在配置文件中 + **/ +public class Log4j2Rule { + + /** + * 正则匹配 关键词 类别 + */ + public static Map regularMap = new HashMap<>(); + /** + * TODO 可配置 + * 此项可以后期放在配置项中 + */ + public static final String USER_NAME_STR = "Name,name,联系人,姓名"; + public static final String USER_IDCARD_STR = "empCard,idCard,身份证,证件号"; + public static final String USER_PHONE_STR = "mobile,Phone,phone,电话,手机"; + + /** + * 正则匹配,自己根据业务要求自定义 + */ + private static String IDCARD_REGEXP = "(\\d{17}[0-9Xx]|\\d{14}[0-9Xx])"; + private static String USERNAME_REGEXP = "[\\u4e00-\\u9fa5]{2,4}"; + private static String PHONE_REGEXP = "(? redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(factory); + // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class); + template.setValueSerializer(serializer); + // 使用StringRedisSerializer来序列化和反序列化redis的key值 + template.setKeySerializer(new StringRedisSerializer()); + template.afterPropertiesSet(); + + return template; + } + +} + diff --git a/redis限流/src/main/java/com/yomahub/tlog/example/feign/controller/LimitRedisController.java b/redis限流/src/main/java/com/yomahub/tlog/example/feign/controller/LimitRedisController.java new file mode 100644 index 0000000..530750d --- /dev/null +++ b/redis限流/src/main/java/com/yomahub/tlog/example/feign/controller/LimitRedisController.java @@ -0,0 +1,29 @@ +package com.yomahub.tlog.example.feign.controller; + + +import com.yomahub.tlog.example.feign.aop.RedisLimit; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * SpringBoot如何用redis限流? + * 可以这样玩! + */ +@Slf4j +@RestController +@RequestMapping("/limit/redis") +public class LimitRedisController { + + /** + * 基于Redis AOP限流 + */ + @GetMapping("/test") + @RedisLimit(key = "redis-limit:test", permitsPerSecond = 2, expire = 1, msg = "当前排队人数较多,请稍后再试!") + public String test() { + log.info("限流成功。。。"); + return "ok"; + } + +} \ No newline at end of file diff --git a/redis限流/src/main/java/com/yomahub/tlog/example/feign/exception/GlobalExceptionHandler.java b/redis限流/src/main/java/com/yomahub/tlog/example/feign/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..865c935 --- /dev/null +++ b/redis限流/src/main/java/com/yomahub/tlog/example/feign/exception/GlobalExceptionHandler.java @@ -0,0 +1,29 @@ +package com.yomahub.tlog.example.feign.exception; +import com.yomahub.tlog.example.feign.vo.ResultInfo; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice// 声明这个类为全局异常处理器 +public class GlobalExceptionHandler { + + + @ExceptionHandler(RedisLimitException.class) // 声明当前方法要处理的异常类型 + public ResultInfo handlerCustomException(RedisLimitException e) { + //1. 打印日志 +// e.printStackTrace(); + + //2. 给前端提示 + return ResultInfo.error(e.getMessage()); + } + + + //非预期异常 对于他们,我们直接捕获,捕获完了,记录日志, 给前端一个假提示 + @ExceptionHandler(Exception.class) + public ResultInfo handlerException(Exception e) { + //1. 打印日志 + e.printStackTrace(); + + //2. 给前端提示 + return ResultInfo.error("当前系统异常"); + } +} diff --git a/redis限流/src/main/java/com/yomahub/tlog/example/feign/exception/RedisLimitException.java b/redis限流/src/main/java/com/yomahub/tlog/example/feign/exception/RedisLimitException.java new file mode 100644 index 0000000..8189b0c --- /dev/null +++ b/redis限流/src/main/java/com/yomahub/tlog/example/feign/exception/RedisLimitException.java @@ -0,0 +1,12 @@ +package com.yomahub.tlog.example.feign.exception; + + +/** + * Redis限流自定义异常 + * @date 2023/3/10 21:43 + */ +public class RedisLimitException extends RuntimeException{ + public RedisLimitException(String msg) { + super( msg ); + } +} \ No newline at end of file diff --git a/redis限流/src/main/java/com/yomahub/tlog/example/feign/vo/ResultInfo.java b/redis限流/src/main/java/com/yomahub/tlog/example/feign/vo/ResultInfo.java new file mode 100644 index 0000000..45bc816 --- /dev/null +++ b/redis限流/src/main/java/com/yomahub/tlog/example/feign/vo/ResultInfo.java @@ -0,0 +1,28 @@ +package com.yomahub.tlog.example.feign.vo; + + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ResultInfo { + + private String message; + private String code; + private T data; + + + public ResultInfo(String message, String code, T data) { + this.message = message; + this.code = code; + this.data = data; + } + + public static ResultInfo error(String message) { + return new ResultInfo(message,"502",null); + } + + + +} diff --git a/redis限流/src/main/resources/application.properties b/redis限流/src/main/resources/application.properties new file mode 100644 index 0000000..acdf5bd --- /dev/null +++ b/redis限流/src/main/resources/application.properties @@ -0,0 +1,19 @@ +spring.application.name=tlog-logback-feign-consumer +server.port=3111 +#eureka.client.service-url.defaultZone=http://127.0.0.1:1111/eureka/ + +tlog.pattern=[$currIp][$spanId][$traceId] +#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 + +http.connectTimeOutMillis=8000 +http.readTimeOutMillis=8000 + +spring.task.execution.pool.core-size=5 +spring.task.execution.pool.max-size=50 +spring.task.execution.pool.queue-capacity=200 +spring.task.execution.thread-name-prefix=woniugege- + + +spring.redis.host=192.168.56.10 +spring.redis.password=123456 +spring.redis.port=6379 diff --git a/redis限流/src/main/resources/rateLimiter.lua b/redis限流/src/main/resources/rateLimiter.lua new file mode 100644 index 0000000..362e0cc --- /dev/null +++ b/redis限流/src/main/resources/rateLimiter.lua @@ -0,0 +1,16 @@ +--获取KEY +local key = KEYS[1] + +local limit = tonumber(ARGV[1]) + +local curentLimit = tonumber(redis.call('get', key) or "0") + +if curentLimit + 1 > limit +then return 0 +else + -- 自增长 1 + redis.call('INCRBY', key, 1) + -- 设置过期时间 + redis.call('EXPIRE', key, ARGV[2]) + return curentLimit + 1 +end \ No newline at end of file diff --git a/redis限流/target/classes/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat b/redis限流/target/classes/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat new file mode 100644 index 0000000000000000000000000000000000000000..738e01103305f5866f77f027670611e50f6e56c1 GIT binary patch literal 111 zcmXBMK?;B%5CFgxq7U?seM8W-LtofrH9=jm74r8{-sUs_U|_OR89ptx-72Y&D%TvT rOH){_Bfgc5Ph4ZO=?nFQYaJ(Qg3Mv>;;udMwLQDPZmg|3I|SWXX9+Kcdhjr+MmS< z#DO2ck3!7ah~|)9u;iJYc{^Wk_Sf$pKLNbJvj83JckwX5BOG+`m|^pk(#oANw1>wt zhR(#yCBsgnw0v9Tv9uQ=&d4)}Od_(Gu*%ziogKGSCBtcyn4DjmTr8`YyUe6qUWvTO zBwt9CYJL%!^xU5&MweAC?fXh=X+x@)3#A$MhaaO$@mcUp=#)>LRXPppeuPb_!rmH~ zA()!VCh}bQ1`ls3^4#ZTIG~bF<#MJ_lEX&@uf^0!{}Bvn)z2|r)Og; z+Fv|eS>GfRWf`v7*zwv~iC!nK9H+NY)9guFDM4-zhd4ruUa$XJV19a%U9vc7MtWoP z4eV%YH#19*XohbJBuJvuP z?8@r)p*)hlmL~KYE>R|YDKP!Y80(}{)>P?0X4(lrU@E30QZC)2q(Ol-ZI)SC-72wo zKE?#fbR4UM`p6~|qBIm(?ESdvU7huQe(wJHzIJnQ**ib&e*MxtIlZ~~wjnUluvrq& zYo$qGvQBN4ZYzFjBTr6XzMdJUB6_b<)zUP`E){q-RDk@SwqGYl9AgcaQ#z75W8vjn zsC!ncpu+)Peg9qa?M{soA05(^Nl1>F&qv?aG)4S;vlA1O$~(2@o879w(r9+o23bz0 zHj~!_8~Gg@$&XpfciM(N9m^=zj;R#gVjP)7z`TNZ5Q z7|#N`RxAn>QQ}jXPlXg71LL^U2evT5z};cs9s`rZAyce2o&Nyb#|*C(R-R>n2ZV=s Qgt>hC0fI1(1w01+0Rxq_g#Z8m literal 0 HcmV?d00001 diff --git a/redis限流/target/classes/com/yomahub/tlog/example/feign/aop/RedisLimitAop.class b/redis限流/target/classes/com/yomahub/tlog/example/feign/aop/RedisLimitAop.class new file mode 100644 index 0000000000000000000000000000000000000000..a7ef5fca17a589ed74258e2c27c7f0bf68e55e3c GIT binary patch literal 4418 zcmbtYYj_mZ8Ga|(&FpR_0U{R{5fep70%WLQl?2cbpe+Q51Y@JUIN6*elkCo{GqaFL z=}mgE1?|P!YA@CnYwsh$(cz|01t$p;c^wbQ2TzBuBS@f-LIOup&~a4GkJn-X z59>Hqi+X%S$MF5 z$H@dvrEnvTNd3p;<#8QfNa1EUwfG{wBo)4_D!!J$Qxf3m6yo@L0^g7i z-;|2q((z0R-^Q~VsGu*L*_%+*X#}0Nd`OPhT8e(1UAT2HG zwjFVxkQ)%*VT;*ORmoV{KFhP^b8#{jjMz-qZQU6+XHK{|Yost>2CSkf#w`{}m_vf@ zo0gk5Rao72&JH@=JPpS)BO)`VVXX?v^7A4SjE2rdQrqL&PLL@CQsIb|EzpzpyUXvJ zX}GOuxJ?>vD;jQ7hTCYkt!%i3@yTd4G^$jLM~m3w`au_~A9#g~G*>eqhR7ZbH#Jv! zxR-7z*E(dj5H%uqSKA?1jY%w#R!KR{~Iwxe!|tW3f_sS;O14ccDX6XSVuh+;%z zQWuACg}~05-L|hXka5Y`(yMYIX=ilSBMqzmm%kMC%ZzHR6!pYVmZdQFi(teZEU`~! zB&Vh{tc*lDSDaEQwE$G1HJI;=8B|~1?M8Vvk)P$-ntAz%vbmUU$>+M>z{-s6xAKaj z>@7OJ!=|U>yKH$nPHR|G8FQIX=1x_00_lpM&E!mCTP9mvB&lAv;AO-fTP=w)vqQ`;U}RR8t;J*O zR`d{FTee`y@Sc~K?;H35e#mke_z`}rp;-dY=?inYR-&eXpWvr-h85*3h0NMnu(N~0GcbuM1Lvf|yKtM1X#+3gc^xwb&f|iHZP#!? zjAuk%?lF;WxO;Yv9fT{tF{8-XI3OCGLN;sQ6KRBow;(CK;BgzO4BE*ba* zUNx{8Ti6-Oo*x_-17O`VyH@cqWXqb=D_8g0Mv*zra-yGGjI ze(bmm>bC}dCv7(UZRYgfA3gc{vy*)B&+{kWcx>wG<4=Z@S5KdLe10lwfS>T@eS%$1~JX>+7sDMqTU zy(~Ii>>#q6w}kzE{@R?S^emGV#v+=UyBy4gwacq1Dy}e$VPce9^6KW6$_7<>&(@Ko zeAt}X>M8tIxslC~qMfC4iiR!Gkc*j!=ttUMS&J}B=U6GGO2ZPO?-JbL3zFh~TF48JySCpWyk3!UrN2BdaoY|% zYH63|v^B_=b8y0?*wI29T^(W$4oQi<(b^RQ)^LZ4*-Od@4ca8UW>lCyzN@NGGQW;o{yLdH10o;vF^?oOntCaG3 zK1;a@#dQ%yt*9t{s-<`r|91J<&D)+5(m>JThSnKW^-rPtCB$0KBGwR}M2#HGK<}SH zqQ4Nr zPQxYC@82+uMH&ulY*;*rC6}n5?Zy@rEj?fMLs)8`dw zI(vP(h8H=_+a%KMi5IXkotVZARY=FrVbxQpo579!4XdYc(?to`$hDM|gC-7YXK*v2 zZfUPe*QMjrSW|`j82+s?1?jr@1+49_UY+ML9HcPM-t4Nb3*145$ zH#47GS^qwk??KjXgthaS!DHBm6WEVaIKZ#x9$dg7T)|=d75CzGO8*P@t4f?e9dWMk z5VMa2+>BSz%~1_8&r^FJKmPmh3^Dex7RT@~4iYO#WSp9pdvOs5?vQVWN`MBXr*`t*s_b%AaI|c@CRi#7dcKP31aV WUpLUtQO;FiOd-kte`NWb1O5w*&g~8W literal 0 HcmV?d00001 diff --git a/redis限流/target/classes/com/yomahub/tlog/example/feign/configuration/Log4j2Rule.class b/redis限流/target/classes/com/yomahub/tlog/example/feign/configuration/Log4j2Rule.class new file mode 100644 index 0000000000000000000000000000000000000000..bee301da05f54952487b04c873ebace125159c05 GIT binary patch literal 1210 zcmbVLT~8BH5IvV7UDkzS#Si=jKWK4r0b8I-G^U`KR3xpU1_-%rm$GEL+bp{VqdfR% zjL}3N_1Om>j5JCt(I4RN0OeO0=WdJC@Mf}mXV1B3X70|+{qei<9Y741WVE3>h=_~^ zbT?x!q7r(9Jug8OwpYRhVYMLoB=qma04_?1Nr(qA$RImLcKj8au^!=@3# zGTbW+fkIFR;CoeCk16>5W^^HnJ-F*Lg^VN$Wh7=4V!O*1_OG)nJ zRU_}4t;&au^>14rpQ`k3tbf^jxyEp+XlJQ>>g0lL>1xSef4%v<_3@qh^V##CZz~LK zeg*vry`5dub1u!CM4m^U*}~6hyg~|(ePjmfifDyd&+1E2g{Jc&1J}|#3-wfzM>_7djhqt uqG<<(Rv}lB$W2Y4wTp6qep{vkhKOmWQ*=<)Zk$CN=a8f?M&F_==l%dkz9<9$ literal 0 HcmV?d00001 diff --git a/redis限流/target/classes/com/yomahub/tlog/example/feign/configuration/RedisConfig.class b/redis限流/target/classes/com/yomahub/tlog/example/feign/configuration/RedisConfig.class new file mode 100644 index 0000000000000000000000000000000000000000..539739d658a32a43d4b6bd0bdd3a9f1ead86d309 GIT binary patch literal 1951 zcmc&#>rN9v6#k|yU8qHBtyL7fBL!MkE+Sfj5h5m7(NNNa_-ndM%ffcI*_{gYU3>ul z=^scW@d11&`Rn@+0P}bjLlol*GBJcO?k*DwCKXJ@ z(1)xm=M+rGFyI?iSMylG3`2BDH+8wp5Y9}L7$U3Ij$jxn=%(0c)XKu%;$?$0$%3VE zqr`39m0QY)+|?b1=LOBGsZFcKcN=9@8dg;mhrCudgjx}L)l@ahtmxH-&82Rc>ZaJy zomEejXBf0yaZ8Xpmqc`sDOh$@b?UZmRx38Ii37{tS9iGNs^{vWnL;}U)sok^CM~;} zp9qGqg&)T$F-4+O{NoHO!RLcQVCa|aJIw{-T@X*q5tMC1|0HaN*Fg|x&gweX_8rTd zU8luMcCiga*G!+UWV3!+&Fvrx&FYSFsj$aCa@F8wRV_-lV)+iuyYe2@DZfmlAA+!Z z3%`@E1AU-KUB+d@rvCZ-fBL60vOi{Gc(Y+jy(UV!qthh4G)+r-b3ucYyD&@C?@}B} zmA75h6~Rr)YOH8AY)!1`?t~^f=4#q43&TRMc>XlD1~ebXWehRQ1kPc1JH#=IIfhpO z2YahE-RP{j{9?O-&ByTs3k)xMC-LjiH3@M%#X=m5?oAi}_Wfc=IYLtQ&Obnuf`8G4 z^d6@&9rLtmaL38J&vcSlOWbIPv&m*y4&>}ed{;I+)C8fv5zS7_B(F$ezqhTru%#}X zqL8?ULHf6GKOr!;RcK9+q|(l4@0&aZ`{tRi&>HoW2_)%v+;_lLq)1}8hBU(7;V-mD zNS8Z7X!{u9x7n#9L{8ARos1qs*~sRO(4RfQ02yOPxRgCYJo}XngyE9hhC*u!{RBCP z>-4>W5!}Q$Zea?e9{HqC&>|-)&x;;vjA593LnNoke}phc2`x<;-*&rgyF<3SWD8Li T?$hpG?tv#i^wzPq_7U(Kf`(Ig literal 0 HcmV?d00001 diff --git a/redis限流/target/classes/com/yomahub/tlog/example/feign/controller/LimitRedisController.class b/redis限流/target/classes/com/yomahub/tlog/example/feign/controller/LimitRedisController.class new file mode 100644 index 0000000000000000000000000000000000000000..3e03560561e24fa04f97bb0e5ccfe6e701f65d1a GIT binary patch literal 1239 zcmbtTOHUI~6#j1CEv?iFV%2C+!4`$<0~Li34GAWsJ_3e~t7&^%E=*_Y%#_DYG%;#~ z5DkgCVdKIbs4*x(et>^NTVcgNFrI0#AegAJY3AH}?sREGuV z^t528O-M~#FXWQKxTPdj(vWjXQ3n-6+V>5m%Nmj)l3=(jVOk7zv3O!w8CAHdXjwjB8B)vAYqYp9KrMXhIE^j zTA;ezypoy!sRl*Q^D^#o1q8}DA|@EZhFwdmYL|7X5-OY*2Hnh?!Wa-#dfH8WvVRVu zTa4$WA%1EYg~^%Jo7Uo|XT?WTrRk?@&*#1_EtO^$R#)y9U%gykezH2h`0e#nab~*s zU}|-Kc71u0!Pk{iHyRfh&d=0@z+fCQ!{>P&%e0dDUkOj(LrA3 zhL?E?q6Wbr{0K1Auf3Qly`L;SoGCt@`*QbQXN6VSDTb!4-aV>fnw{~T ztA|F~SRYLV!+x6(Iyvb~{LnWT$jpdYpKjQfS?>jhb)NBjt5CuI}2GImNNrdjrB#&1U#eM(` C(ozcm literal 0 HcmV?d00001 diff --git a/redis限流/target/classes/com/yomahub/tlog/example/feign/exception/GlobalExceptionHandler.class b/redis限流/target/classes/com/yomahub/tlog/example/feign/exception/GlobalExceptionHandler.class new file mode 100644 index 0000000000000000000000000000000000000000..af989848ab5a301ec33caf3ccfa6e68ebbadd333 GIT binary patch literal 1389 zcmbtU%T5$Q6g>qqG_)fiFF|}iX8`GqD z25$D^mV?_4?lAPsOC{|BLwB*X!H`?l6~Qo2mP))xV_%qc?niVoRMr8HHn@>l+&IkH zEtxPZmIEDoZ*V(m>3nzhE6LqS|FtM5{ zD`T-C6G?h4Dy1#Y6d<`CmbD4JWYh%4R27dmbG_yiEuHdJ9;MXb zU_-BF3d2gVyv?iJi?|9~(6b9L+L4CAUDK%v#ABKHVY2g0&So8C`ki(}lHM+BWsQ!= z2#b}f48&Xkz36jrw}5*nIw%z|g=q&f1>8p;d9n8ML+#!B!!HMi2On!c-q!Z_86N$o z2T9VFdN2KL5m<)#QO*=^>8636VxkQc^i!?4tT z*$uKYTYb1Sahs0@GH2Q~%Y&VD!vjGtMh`tA*{>TcyBhks6nV5VTJuxiz&_Ka3+L$D zQ$KW2pnc&e0q4pTKx$_Kcj^}nibB-2WQl*$)&GcDfPWF^GeGb zO{Ha3=(#6nU~2FP_FZVwExvc#n=xo**9i6d>lDENQ}Z>sV#I`%#58zG)+3 M-d(=6!79M!JNzSXLI3~& literal 0 HcmV?d00001 diff --git a/redis限流/target/classes/com/yomahub/tlog/example/feign/vo/ResultInfo.class b/redis限流/target/classes/com/yomahub/tlog/example/feign/vo/ResultInfo.class new file mode 100644 index 0000000000000000000000000000000000000000..a770600094eac7b6d6c401e0d061bd1e2f40dd75 GIT binary patch literal 1675 zcmb7D>rN9<5dKbgyS=bbE(N?RUbo9-5zs^>Bq}j6shUXB#Gl);lqGGKY_~+-z{k-< zLZXQe;6oW_&e_r}G^Di2+1WWW-+bT9?B9RC{s7p(Mjlx#=8(n`AIp5KWZ-16nkVyV z25SPDme=XH4NqXYeBd6rPSb5SoSi82+l^NOdOg^sJAK!U+`*7fwF9pnQAlpbZ?xU0 z8&aHBsgTFm^xJ;4CGf(!h3D^_o9R$eAZ#vxl5U8HR%=!SLgozLSh z^yjz23u|H$>n3u@n;7H6#JIrHRrY}P_Sxzh15F7`_P+wfiEgb@6$|vqWa+!okm6!w zrMRAaQb`C*NV`B$gn}u$pC->Ydg}Bn+NTiq;t5jr(g`$s`2_k8@@tr(bB@qKSvuwA zC~tziGbqv@uvI>0DT;qZSzkzt8Kk7e95l?6Jr{dsagE+M-b8`!6u;|~eFHa%BeQ!> zbs5S0jr13L+7PG6e5d!6WM#;^dC9_5aj|xRTeux#yrw5(r0t(bak7%6lE)Is=~x1j zRB-HJk^*;z!fEH>%n{-4;vQ*Wg-Le$dARYB;8f!lh~<4epp#+C1ka5(Udqk+o5gc@cyhzv^rs`4Jw+t*9nBCzkJW&T6^)i`7FW?Q*et Ri~oesRi`YJs52?p{{c2zAfNyM literal 0 HcmV?d00001 diff --git a/redis限流/target/classes/rateLimiter.lua b/redis限流/target/classes/rateLimiter.lua new file mode 100644 index 0000000..362e0cc --- /dev/null +++ b/redis限流/target/classes/rateLimiter.lua @@ -0,0 +1,16 @@ +--获取KEY +local key = KEYS[1] + +local limit = tonumber(ARGV[1]) + +local curentLimit = tonumber(redis.call('get', key) or "0") + +if curentLimit + 1 > limit +then return 0 +else + -- 自增长 1 + redis.call('INCRBY', key, 1) + -- 设置过期时间 + redis.call('EXPIRE', key, ARGV[2]) + return curentLimit + 1 +end \ No newline at end of file diff --git a/redis限流/tlog-example-logback-feign-consumer.iml b/redis限流/tlog-example-logback-feign-consumer.iml new file mode 100644 index 0000000..4e9a846 --- /dev/null +++ b/redis限流/tlog-example-logback-feign-consumer.iml @@ -0,0 +1,136 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file