...
|
...
|
@@ -17,33 +17,33 @@ import com.yoho.search.common.filter.YohoHttpServletRequestWrapper; |
|
|
|
|
|
@Component
|
|
|
@Aspect
|
|
|
public class AutoDownGradePersionalAspect {
|
|
|
public class PersionalRateLimitAspect {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger("DOWNGRADE");
|
|
|
private static final Logger DOWNGRADE = LoggerFactory.getLogger("DOWNGRADE");
|
|
|
|
|
|
@Autowired
|
|
|
private AutoDownGradePersionalRateLimitService autoDownGradePersionalRateLimitService;
|
|
|
private PersionalRateLimitService persionalRateLimitService;
|
|
|
|
|
|
@Around("@annotation(com.yoho.search.common.downgrade.persional.AutoDownGradePersional)")
|
|
|
public Object downGrade(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
@Around("@annotation(com.yoho.search.common.downgrade.persional.PersionalRateLimit)")
|
|
|
public Object persionalRateLimit(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
try {
|
|
|
String autoDownGradeKey = this.getAutoDownGradeKey(pjp);
|
|
|
String persionalRateLimitKey = this.getPersionalRateLimitKey(pjp);
|
|
|
MethodSignature signature = (MethodSignature) pjp.getSignature();
|
|
|
AutoDownGradePersional autoDownGrade = signature.getMethod().getAnnotation(AutoDownGradePersional.class);
|
|
|
PersionalRateLimit rersionalRateLimit = signature.getMethod().getAnnotation(PersionalRateLimit.class);
|
|
|
Object[] arges = pjp.getArgs();
|
|
|
for (Object object : arges) {
|
|
|
if (this.dealPersional(object, autoDownGrade, autoDownGradeKey)) {
|
|
|
if (this.doPersionalRateLimit(object, rersionalRateLimit, persionalRateLimitKey)) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return pjp.proceed();
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
DOWNGRADE.error(e.getMessage());
|
|
|
throw e;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean dealPersional(Object object, AutoDownGradePersional autoDownGrade, String autoDownGradeKey) {
|
|
|
private boolean doPersionalRateLimit(Object object, PersionalRateLimit rateLimit, String rateLimitKey) {
|
|
|
if (!(object instanceof YohoHttpServletRequestWrapper)) {
|
|
|
return false;
|
|
|
}
|
...
|
...
|
@@ -55,17 +55,27 @@ public class AutoDownGradePersionalAspect { |
|
|
if (StringUtils.isNotBlank(yohoHttpServletRequestWrapper.getParameter("order"))) {
|
|
|
return false;
|
|
|
}
|
|
|
int rateLimitInSecond = autoDownGrade.rateLimitInSecond();
|
|
|
int rateLimit = autoDownGrade.rateLimit();
|
|
|
if (autoDownGradePersionalRateLimitService.isRateLimit(autoDownGradeKey, rateLimitInSecond, rateLimit)) {
|
|
|
logger.error("AutoDownGradePersionalAspect,autoDownGradeKey is [{}],rateLimitInSecond is [{}],rateLimit is [{}]", autoDownGradeKey, rateLimitInSecond, rateLimit);
|
|
|
if (persionalRateLimitService.isPersionalRateLimit(rateLimitKey, rateLimit)) {
|
|
|
DOWNGRADE.error("PersionalRateLimit happen ,rateLimitKey is [{}],rateLimit is [{}]", rateLimitKey, this.getRateLimitInfo(rateLimit));
|
|
|
yohoHttpServletRequestWrapper.addParams("uid", new String[] { "0" });
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private String getAutoDownGradeKey(ProceedingJoinPoint pjp) {
|
|
|
private String getRateLimitInfo(PersionalRateLimit rateLimit) {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
sb.append(rateLimit.limit()).append("c/").append(rateLimit.second()).append("s");
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @controller层则获取url名称
|
|
|
* @service层则获取方法名称
|
|
|
* @param pjp
|
|
|
* @return
|
|
|
*/
|
|
|
private String getPersionalRateLimitKey(ProceedingJoinPoint pjp) {
|
|
|
// 1、获取method和class
|
|
|
Method targetMethod = ((MethodSignature) (pjp.getSignature())).getMethod();
|
|
|
Class<?> clazz = targetMethod.getDeclaringClass();
|
...
|
...
|
|