add common 依赖 spring-boot-properties-migrator ;

add 新增 modules 模块, 包含 demo, generator, job, system 模块 ;
update 更新模块包名 rateLimiter => ratelimiter ;
refactor 重构 ratelimiter 模块, 参考幂等模块加入自动装配相关配置 ;
fix 修正 AsyncConfig 实现接口 ;
fix 修正 SwaggerConfig 注解写法 ;
fix 修正 ruoyi-common-doc pom 描述 ;
delete LogAspect 去掉无用注解 ;
This commit is contained in:
zlyx
2023-01-18 20:23:11 +08:00
parent 71a2a8245d
commit 2ae3681286
208 changed files with 89 additions and 100 deletions

View File

@ -0,0 +1,36 @@
package com.ruoyi.common.ratelimiter.annotation;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.ratelimiter.enums.LimitType;
import java.lang.annotation.*;
/**
* 限流注解
*
* @author Lion Li
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimiter {
/**
* 限流key
*/
String key() default CacheConstants.RATE_LIMIT_KEY;
/**
* 限流时间,单位秒
*/
int time() default 60;
/**
* 限流次数
*/
int count() default 100;
/**
* 限流类型
*/
LimitType limitType() default LimitType.DEFAULT;
}

View File

@ -0,0 +1,64 @@
package com.ruoyi.common.ratelimiter.aspectj;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.ratelimiter.annotation.RateLimiter;
import com.ruoyi.common.ratelimiter.enums.LimitType;
import com.ruoyi.common.redis.utils.RedisUtils;
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.reflect.MethodSignature;
import org.redisson.api.RateType;
import java.lang.reflect.Method;
/**
* 限流处理
*
* @author Lion Li
*/
@Slf4j
@Aspect
public class RateLimiterAspect {
@Before("@annotation(rateLimiter)")
public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable {
int time = rateLimiter.time();
int count = rateLimiter.count();
String combineKey = getCombineKey(rateLimiter, point);
try {
RateType rateType = RateType.OVERALL;
if (rateLimiter.limitType() == LimitType.CLUSTER) {
rateType = RateType.PER_CLIENT;
}
long number = RedisUtils.rateLimiter(combineKey, rateType, count, time);
if (number == -1) {
throw new ServiceException(MessageUtils.message("rate.limiter.message"));
}
log.info("限制令牌 => {}, 剩余令牌 => {}, 缓存key => '{}'", count, number, combineKey);
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("服务器限流异常,请稍候再试");
}
}
public String getCombineKey(RateLimiter rateLimiter, JoinPoint point) {
StringBuilder stringBuffer = new StringBuilder(rateLimiter.key());
if (rateLimiter.limitType() == LimitType.IP) {
// 获取请求ip
stringBuffer.append(ServletUtils.getClientIP()).append("-");
} else if (rateLimiter.limitType() == LimitType.CLUSTER) {
// 获取客户端实例id
stringBuffer.append(RedisUtils.getClient().getId()).append("-");
}
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
Class<?> targetClass = method.getDeclaringClass();
stringBuffer.append(targetClass.getName()).append("-").append(method.getName());
return stringBuffer.toString();
}
}

View File

@ -0,0 +1,20 @@
package com.ruoyi.common.ratelimiter.config;
import com.ruoyi.common.ratelimiter.aspectj.RateLimiterAspect;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConfiguration;
/**
* @author guangxin
* @date 2023/1/18
*/
@AutoConfiguration(after = RedisConfiguration.class)
public class RateLimiterConfig {
@Bean
public RateLimiterAspect rateLimiterAspect() {
return new RateLimiterAspect();
}
}

View File

@ -0,0 +1,24 @@
package com.ruoyi.common.ratelimiter.enums;
/**
* 限流类型
*
* @author ruoyi
*/
public enum LimitType {
/**
* 默认策略全局限流
*/
DEFAULT,
/**
* 根据请求者IP进行限流
*/
IP,
/**
* 实例限流(集群多后端实例)
*/
CLUSTER
}

View File

@ -0,0 +1 @@
com.ruoyi.common.ratelimiter.config.RateLimiterConfig