说明:1、实现api预警信息aop保存到数据库
This commit is contained in:
parent
36c2bad94a
commit
093ca22344
|
|
@ -3,6 +3,7 @@ package com.xjs.business.warning;
|
|||
import com.ruoyi.common.core.constant.ServiceNameConstants;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.business.warning.domain.ApiRecord;
|
||||
import com.xjs.business.warning.domain.ApiWarning;
|
||||
import com.xjs.business.warning.factory.RemoteWarningCRUDFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||
|
|
@ -31,9 +32,9 @@ public interface RemoteWarningCRUDFeign {
|
|||
public R<ApiRecord> updateApiRecordForRPC(@RequestBody ApiRecord apiRecord);
|
||||
|
||||
@GetMapping("apiwarning")
|
||||
R<List<ApiRecord>> selectApiRecordListForRPC(@SpringQueryMap ApiRecord apiRecord) ;
|
||||
|
||||
|
||||
R<List<ApiRecord>> selectApiRecordListForRPC(@SpringQueryMap ApiRecord apiRecord);
|
||||
|
||||
@PostMapping("apiwarning/saveApiwarningForRPC")
|
||||
R<ApiWarning> saveApiWarningForRPC(@RequestBody ApiWarning apiWarning);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,112 @@
|
|||
package com.xjs.business.warning.domain;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc api预警实体类
|
||||
* @create 2022-01-07
|
||||
*/
|
||||
|
||||
public class ApiWarning implements Serializable {
|
||||
/** id */
|
||||
private Long id;
|
||||
|
||||
/** api名称 */
|
||||
private String apiName;
|
||||
|
||||
/** 预警类型 */
|
||||
private String warningType;
|
||||
|
||||
/** 预警等级 */
|
||||
private String warningLevel;
|
||||
|
||||
/** 预警记录信息 */
|
||||
private String warningMessage;
|
||||
|
||||
/** 限定值 */
|
||||
private String limitValue;
|
||||
|
||||
/** 实际值 */
|
||||
private String realValue;
|
||||
|
||||
/** 是否处理 1 处理 2未处理 */
|
||||
private Integer handle;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getApiName() {
|
||||
return apiName;
|
||||
}
|
||||
|
||||
public void setApiName(String apiName) {
|
||||
this.apiName = apiName;
|
||||
}
|
||||
|
||||
public String getWarningType() {
|
||||
return warningType;
|
||||
}
|
||||
|
||||
public void setWarningType(String warningType) {
|
||||
this.warningType = warningType;
|
||||
}
|
||||
|
||||
public String getWarningLevel() {
|
||||
return warningLevel;
|
||||
}
|
||||
|
||||
public void setWarningLevel(String warningLevel) {
|
||||
this.warningLevel = warningLevel;
|
||||
}
|
||||
|
||||
public String getWarningMessage() {
|
||||
return warningMessage;
|
||||
}
|
||||
|
||||
public void setWarningMessage(String warningMessage) {
|
||||
this.warningMessage = warningMessage;
|
||||
}
|
||||
|
||||
public String getLimitValue() {
|
||||
return limitValue;
|
||||
}
|
||||
|
||||
public void setLimitValue(String limitValue) {
|
||||
this.limitValue = limitValue;
|
||||
}
|
||||
|
||||
public String getRealValue() {
|
||||
return realValue;
|
||||
}
|
||||
|
||||
public void setRealValue(String realValue) {
|
||||
this.realValue = realValue;
|
||||
}
|
||||
|
||||
public Integer getHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
public void setHandle(Integer handle) {
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.xjs.business.warning.factory;
|
|||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.business.warning.RemoteWarningCRUDFeign;
|
||||
import com.xjs.business.warning.domain.ApiRecord;
|
||||
import com.xjs.business.warning.domain.ApiWarning;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
|
@ -40,6 +41,12 @@ public class RemoteWarningCRUDFactory implements FallbackFactory<RemoteWarningCR
|
|||
log.error("调用预警服务查询接口失败,执行降级处理----"+apiRecord.getApiName());
|
||||
return R.fail();
|
||||
}
|
||||
|
||||
@Override
|
||||
public R<ApiWarning> saveApiWarningForRPC(ApiWarning apiWarning) {
|
||||
log.error("调用预警服务api预警查询接口失败,执行降级处理----"+apiWarning.getApiName());
|
||||
return R.fail();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
package com.xjs.consts;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc api预警处理常量
|
||||
* @create 2022-01-07
|
||||
*/
|
||||
public class ApiWarnHandleConst {
|
||||
//已处理
|
||||
public static final Integer YES= 1;
|
||||
//未处理
|
||||
public static final Integer NO= 2;
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package com.xjs.enums;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 预警等级枚举
|
||||
* @create 2022-01-07
|
||||
*/
|
||||
public enum WarnLevelEnum {
|
||||
/* 普通 **/
|
||||
NOEMAL("普通"),
|
||||
/* 警告 **/
|
||||
WARNING("警告"),
|
||||
/* 严重 **/
|
||||
DANGER("严重");
|
||||
|
||||
private final String message;
|
||||
|
||||
WarnLevelEnum(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.xjs.enums;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 预警类型枚举
|
||||
* @create 2022-01-07
|
||||
*/
|
||||
public enum WarnTypeEnum {
|
||||
|
||||
API("API","API接口调用超出限定值。限定值--%s次,实际值--%s次");
|
||||
|
||||
private final String type;
|
||||
|
||||
private final String message;
|
||||
|
||||
WarnTypeEnum(String type, String message) {
|
||||
this.type = type;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,10 @@ import cn.hutool.core.date.DateUtil;
|
|||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.business.warning.RemoteWarningCRUDFeign;
|
||||
import com.xjs.business.warning.domain.ApiRecord;
|
||||
import com.xjs.business.warning.domain.ApiWarning;
|
||||
import com.xjs.enums.StatusEnum;
|
||||
import com.xjs.enums.WarnLevelEnum;
|
||||
import com.xjs.enums.WarnTypeEnum;
|
||||
import com.xjs.log.mapper.ApiLogMapper;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
|
|
@ -25,6 +28,8 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.xjs.consts.ApiWarnHandleConst.NO;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc api日志切面类
|
||||
|
|
@ -166,9 +171,23 @@ public class ApiLogAspect {
|
|||
remoteWarningCRUDFeign.updateApiRecordForRPC(haveApiRecord);
|
||||
//判断接口请求是否超过阈值
|
||||
if (haveApiRecord.getDayCount() > haveApiRecord.getLimitCount()) {
|
||||
//TODO 把记录添加到预警表中,表还没设计
|
||||
|
||||
|
||||
//把记录添加到预警表中
|
||||
ApiWarning apiWarning = new ApiWarning();
|
||||
apiWarning.setLimitValue(String.valueOf(haveApiRecord.getLimitCount()));
|
||||
apiWarning.setRealValue(String.valueOf(haveApiRecord.getDayCount()));
|
||||
apiWarning.setApiName(haveApiRecord.getApiName());
|
||||
apiWarning.setHandle(NO);
|
||||
apiWarning.setWarningLevel(WarnLevelEnum.NOEMAL.getMessage());
|
||||
if(haveApiRecord.getDayCount()>haveApiRecord.getLimitCount()*2){
|
||||
apiWarning.setWarningLevel(WarnLevelEnum.WARNING.getMessage());
|
||||
} else if (haveApiRecord.getDayCount() > haveApiRecord.getLimitCount() * 3) {
|
||||
apiWarning.setWarningLevel(WarnLevelEnum.DANGER.getMessage());
|
||||
}
|
||||
apiWarning.setWarningType(WarnTypeEnum.API.getType());
|
||||
String message = String.format(WarnTypeEnum.API.getMessage(),
|
||||
haveApiRecord.getLimitCount(), haveApiRecord.getDayCount());
|
||||
apiWarning.setWarningMessage(message);
|
||||
remoteWarningCRUDFeign.saveApiWarningForRPC(apiWarning);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import com.ruoyi.common.log.annotation.Log;
|
|||
import com.ruoyi.common.log.enums.BusinessType;
|
||||
import com.ruoyi.common.security.annotation.RequiresPermissions;
|
||||
import com.xjs.domain.ApiRecord;
|
||||
import com.xjs.domain.ApiWarning;
|
||||
import com.xjs.service.ApiWarningService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
|
@ -50,12 +51,28 @@ public class ApiWarningController extends BaseController {
|
|||
return apiWarningService.updateApiRecordByUrl(apiRecord) ? R.ok() : R.fail();
|
||||
}
|
||||
|
||||
/**
|
||||
* 远程查询api记录信息
|
||||
* @param apiRecord
|
||||
* @return R<List<ApiRecord>>
|
||||
*/
|
||||
@GetMapping
|
||||
public R<List<ApiRecord>> selectApiRecordListForRPC(ApiRecord apiRecord) {
|
||||
List<ApiRecord> apiRecords = apiWarningService.selectApiRecordListByUrl(apiRecord);
|
||||
return R.ok(apiRecords);
|
||||
}
|
||||
|
||||
/**
|
||||
* 远程保存api预警信息
|
||||
* @param apiWarning 预警实体类
|
||||
* @return R
|
||||
*/
|
||||
@PostMapping("saveApiwarningForRPC")
|
||||
public R<ApiWarning> saveApiWarningForRPC(@RequestBody ApiWarning apiWarning) {
|
||||
boolean save = apiWarningService.save(apiWarning);
|
||||
return save? R.ok():R.fail();
|
||||
}
|
||||
|
||||
|
||||
//-------------------------代码生成------------------------------------
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,55 @@
|
|||
package com.xjs.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.ruoyi.common.core.annotation.Excel;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc api预警实体类
|
||||
* @create 2022-01-07
|
||||
*/
|
||||
@TableName("api_warning")
|
||||
@Data
|
||||
public class ApiWarning implements Serializable {
|
||||
/** id */
|
||||
private Long id;
|
||||
|
||||
/** api名称 */
|
||||
@Excel(name = "api名称")
|
||||
private String apiName;
|
||||
|
||||
/** 预警类型 */
|
||||
@Excel(name = "预警类型")
|
||||
private String warningType;
|
||||
|
||||
/** 预警等级 */
|
||||
@Excel(name = "预警等级")
|
||||
private String warningLevel;
|
||||
|
||||
/** 预警记录信息 */
|
||||
@Excel(name = "预警记录信息")
|
||||
private String warningMessage;
|
||||
|
||||
/** 限定值 */
|
||||
@Excel(name = "限定值")
|
||||
private String limitValue;
|
||||
|
||||
/** 实际值 */
|
||||
@Excel(name = "实际值")
|
||||
private String realValue;
|
||||
|
||||
/** 是否处理 1 处理 2未处理 */
|
||||
@Excel(name = "是否处理",readConverterExp=" 1=处理,2=未处理")
|
||||
private Integer handle;
|
||||
|
||||
@Excel(name = "创建时间", dateFormat = "yyyy-MM-dd HH:mm:ss")
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Date createTime;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.xjs.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xjs.domain.ApiWarning;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc mp api预警mapper
|
||||
* @create 2022-01-07
|
||||
*/
|
||||
|
||||
public interface ApiWarningMapper extends BaseMapper<ApiWarning> {
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
package com.xjs.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.xjs.domain.ApiRecord;
|
||||
import com.xjs.domain.ApiWarning;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -9,7 +11,7 @@ import java.util.List;
|
|||
* @desc
|
||||
* @create 2021-12-31
|
||||
*/
|
||||
public interface ApiWarningService {
|
||||
public interface ApiWarningService extends IService<ApiWarning> {
|
||||
|
||||
/**
|
||||
* 保存apirecord 当存在相同api时,不允许保存
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package com.xjs.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.xjs.domain.ApiRecord;
|
||||
import com.xjs.domain.ApiWarning;
|
||||
import com.xjs.exception.BusinessException;
|
||||
import com.xjs.mapper.ApiRecordMapper;
|
||||
import com.xjs.mapper.ApiWarningMapper;
|
||||
import com.xjs.service.ApiWarningService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
|
@ -17,7 +20,7 @@ import java.util.Objects;
|
|||
* @create 2021-12-31
|
||||
*/
|
||||
@Service
|
||||
public class ApiWarningServiceImpl implements ApiWarningService {
|
||||
public class ApiWarningServiceImpl extends ServiceImpl<ApiWarningMapper, ApiWarning> implements ApiWarningService {
|
||||
|
||||
@Resource
|
||||
private ApiRecordMapper apiRecordMapper;
|
||||
|
|
|
|||
Loading…
Reference in New Issue