|
|
|
@ -28,12 +28,12 @@ import java.lang.annotation.Annotation;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
|
|
|
|
import static com.xjs.consts.ApiWarnHandleConst.NO;
|
|
|
|
|
import static com.xjs.consts.ReqConst.ERROR;
|
|
|
|
|
import static com.xjs.consts.ReqConst.SUCCESS;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* api日志切面类
|
|
|
|
@ -76,14 +76,19 @@ public class ApiLogAspect {
|
|
|
|
|
//执行预警切入逻辑(降级不预警)
|
|
|
|
|
if (obj instanceof JSONObject) {
|
|
|
|
|
JSONObject jsonObject = (JSONObject) obj;
|
|
|
|
|
|
|
|
|
|
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
|
|
|
|
|
warning(between, joinPoint);
|
|
|
|
|
this.warning(between, joinPoint);
|
|
|
|
|
} else {
|
|
|
|
|
//如果降级,接口状态修改为异常
|
|
|
|
|
this.demoteHandle(joinPoint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//返回值为String情况
|
|
|
|
|
if (obj instanceof String) {
|
|
|
|
|
if (StringUtils.isNotEmpty(String.valueOf(obj))) {
|
|
|
|
|
warning(between, joinPoint);
|
|
|
|
|
this.warning(between, joinPoint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -94,6 +99,7 @@ public class ApiLogAspect {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理完请求后执行
|
|
|
|
|
*
|
|
|
|
@ -148,11 +154,12 @@ public class ApiLogAspect {
|
|
|
|
|
if (e != null || StringUtils.isEmpty(response)) {
|
|
|
|
|
entity.setIsSuccess(ReqConst.ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
entity.setIsSuccess(ReqConst.SUCCESS);
|
|
|
|
|
entity.setIsSuccess(SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
remoteLogFeign.saveApiLog(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 预警切入
|
|
|
|
|
*
|
|
|
|
@ -160,20 +167,16 @@ public class ApiLogAspect {
|
|
|
|
|
* @param joinPoint aop连接对象
|
|
|
|
|
*/
|
|
|
|
|
private void warning(long between, ProceedingJoinPoint joinPoint) {
|
|
|
|
|
//获取目标类名及方法名
|
|
|
|
|
Signature signature = joinPoint.getSignature();
|
|
|
|
|
String method = signature.getName();
|
|
|
|
|
Class aclass = signature.getDeclaringType();
|
|
|
|
|
Method[] methods = aclass.getMethods();
|
|
|
|
|
//根据目标的方法名判断当前方法
|
|
|
|
|
for (Method thisMethod : methods) {
|
|
|
|
|
if (method.equals(thisMethod.getName())) {
|
|
|
|
|
//拿到当前方法的注解判断是否为apiLog注解
|
|
|
|
|
Annotation[] declaredAnnotations = thisMethod.getDeclaredAnnotations();
|
|
|
|
|
for (Annotation annotation : declaredAnnotations) {
|
|
|
|
|
if (annotation instanceof ApiLog) {
|
|
|
|
|
String name = ((ApiLog) annotation).name();
|
|
|
|
|
String url = ((ApiLog) annotation).url();
|
|
|
|
|
|
|
|
|
|
Map<String, String> annotationInfo = this.getAnnotationInfo(joinPoint);
|
|
|
|
|
|
|
|
|
|
if (CollUtil.isEmpty(annotationInfo)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String name = annotationInfo.get("name");
|
|
|
|
|
String url = annotationInfo.get("url");
|
|
|
|
|
|
|
|
|
|
//根据拿到的url和name查询数据库是否存在,存在则count+1,不存在则add
|
|
|
|
|
ApiRecord apiRecord = new ApiRecord();
|
|
|
|
|
apiRecord.setApiName(name);
|
|
|
|
@ -187,10 +190,12 @@ public class ApiLogAspect {
|
|
|
|
|
apiRecord.setTotalCount(1L);
|
|
|
|
|
apiRecord.setDayCount(1L);
|
|
|
|
|
apiRecord.setLimitCount(30L);
|
|
|
|
|
apiRecord.setStatus(SUCCESS);
|
|
|
|
|
remoteWarningCRUDFeign.saveApiRecordForRPC(apiRecord);
|
|
|
|
|
} else {
|
|
|
|
|
ApiRecord haveApiRecord = data.get(0);
|
|
|
|
|
|
|
|
|
|
haveApiRecord.setStatus(SUCCESS);
|
|
|
|
|
haveApiRecord.setRequestTime((int) between);
|
|
|
|
|
haveApiRecord.setTotalCount(haveApiRecord.getTotalCount() + 1L);
|
|
|
|
|
//统计当前的请求次数,隔天清零
|
|
|
|
@ -234,11 +239,73 @@ public class ApiLogAspect {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 接口降级修改状态异常
|
|
|
|
|
* @param joinPoint 切入点
|
|
|
|
|
*/
|
|
|
|
|
private void demoteHandle(ProceedingJoinPoint joinPoint) {
|
|
|
|
|
|
|
|
|
|
Map<String, String> map = this.getAnnotationInfo(joinPoint);
|
|
|
|
|
|
|
|
|
|
if (CollUtil.isEmpty(map)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String name = map.get("name");
|
|
|
|
|
String url = map.get("url");
|
|
|
|
|
ApiRecord apiRecord = new ApiRecord();
|
|
|
|
|
apiRecord.setApiName(name);
|
|
|
|
|
apiRecord.setApiUrl(url);
|
|
|
|
|
|
|
|
|
|
R<List<ApiRecord>> listR = remoteWarningCRUDFeign.selectApiRecordListForRPC(apiRecord);
|
|
|
|
|
if (listR.getCode() == R.SUCCESS) {
|
|
|
|
|
List<ApiRecord> data = listR.getData();
|
|
|
|
|
if (CollUtil.isNotEmpty(data)) {
|
|
|
|
|
ApiRecord haveApiRecord = data.get(0);
|
|
|
|
|
//置为空让mp自动填充
|
|
|
|
|
haveApiRecord.setUpdateTime(null);
|
|
|
|
|
haveApiRecord.setStatus(ERROR);
|
|
|
|
|
remoteWarningCRUDFeign.updateApiRecordForRPC(haveApiRecord);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过反射获取注解信息
|
|
|
|
|
*
|
|
|
|
|
* @param joinPoint 切入点
|
|
|
|
|
* @return 注解信息map
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, String> getAnnotationInfo(ProceedingJoinPoint joinPoint) {
|
|
|
|
|
//获取目标类名及方法名
|
|
|
|
|
Signature signature = joinPoint.getSignature();
|
|
|
|
|
String method = signature.getName();
|
|
|
|
|
Class aclass = signature.getDeclaringType();
|
|
|
|
|
Method[] methods = aclass.getMethods();
|
|
|
|
|
|
|
|
|
|
//根据目标的方法名判断当前方法
|
|
|
|
|
for (Method thisMethod : methods) {
|
|
|
|
|
if (method.equals(thisMethod.getName())) {
|
|
|
|
|
|
|
|
|
|
//拿到当前方法的注解判断是否为apiLog注解
|
|
|
|
|
Annotation[] declaredAnnotations = thisMethod.getDeclaredAnnotations();
|
|
|
|
|
|
|
|
|
|
for (Annotation annotation : declaredAnnotations) {
|
|
|
|
|
if (annotation instanceof ApiLog) {
|
|
|
|
|
String name = ((ApiLog) annotation).name();
|
|
|
|
|
String url = ((ApiLog) annotation).url();
|
|
|
|
|
|
|
|
|
|
Map<String, String> hashMap = new HashMap<>();
|
|
|
|
|
hashMap.put("name", name);
|
|
|
|
|
hashMap.put("url", url);
|
|
|
|
|
return hashMap;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|