1、任务日志aop实现并持久化数据库

2、定时任务类都添加@TaskLog注解提供aop切入
pull/254/head
xjs 4 years ago
parent f3cfac58d5
commit feb757b19c

@ -3,6 +3,7 @@ package com.xjs.business.log;
import com.ruoyi.common.core.constant.ServiceNameConstants;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.log.domain.ApiLog;
import com.xjs.business.log.domain.TaskLog;
import com.xjs.business.log.domain.WebmagicLog;
import com.xjs.business.log.factory.RemoteLogFactory;
import org.springframework.cloud.openfeign.FeignClient;
@ -24,5 +25,8 @@ public interface RemoteLogFeign {
@PostMapping("reptileLog/saveForPRC")
public R<Object> saveReptileLog(@RequestBody WebmagicLog webmagicLog);
R<Object> saveReptileLog(@RequestBody WebmagicLog webmagicLog);
@PostMapping("taskLog/saveForPRC")
R<Object> saveTaskLog(@RequestBody TaskLog taskLog);
}

@ -0,0 +1,37 @@
package com.xjs.business.log.domain;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
*
* @author xiejs
* @since 2022-03-01
*/
@Data
public class TaskLog implements Serializable {
private static final long serialVersionUID = 1L;
/** 主键id */
private Long id;
/** 任务名称 */
private String name;
/** 方法名 */
private String method;
/** 类路径 */
private String classPath;
/** 请求时间 */
private Long requestTime;
/**
*
*/
private Date createTime;
}

@ -3,6 +3,7 @@ package com.xjs.business.log.factory;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.log.RemoteLogFeign;
import com.xjs.business.log.domain.ApiLog;
import com.xjs.business.log.domain.TaskLog;
import com.xjs.business.log.domain.WebmagicLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -32,6 +33,12 @@ public class RemoteLogFactory implements FallbackFactory<RemoteLogFeign> {
log.error("日志模块爬虫日志服务添加调用失败");
return R.fail("日志模块爬虫日志服务添加调用失败" + cause.getMessage());
}
@Override
public R<Object> saveTaskLog(TaskLog taskLog) {
log.error("日志模块任务日志服务添加调用失败");
return R.fail("日志模块任务日志服务添加调用失败" + cause.getMessage());
}
};
}
}

@ -0,0 +1,18 @@
package com.xjs.job.aop;
import java.lang.annotation.*;
/**
*
* @author xiejs
* @since 2022-03-01
*/
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TaskLog {
/**
*
*/
public String name() default "";
}

@ -0,0 +1,105 @@
package com.xjs.job.aop;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.log.RemoteLogFeign;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
*
*
* @author xiejs
* @since 2022-03-01
*/
@Component
@Aspect
@Log4j2
public class taskLogAspect {
@Resource
private RemoteLogFeign remoteLogFeign;
/**
* AOP
*/
@Pointcut("@annotation(com.xjs.job.aop.TaskLog)")
public void pointcut() {
}
/**
*
*/
@Around("pointcut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object obj = null;
try {
//切入前-----
// 开始时间
long startTime = System.currentTimeMillis();
obj = joinPoint.proceed();
//切入后-----
// 结束时间
long endTime = System.currentTimeMillis();
long between = endTime - startTime;
log.info("调用定时任务耗费时间:{}ms", between);
this.handle(joinPoint, between);
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}
/**
*
*
* @param joinPoint
* @param between
*/
private void handle(ProceedingJoinPoint joinPoint, Long between) {
//获取目标类名及方法名
Signature signature = joinPoint.getSignature();
String method = signature.getName();
Class aClass = signature.getDeclaringType();
Method[] methods = aClass.getMethods();
String className = aClass.getName();
com.xjs.business.log.domain.TaskLog taskLog = new com.xjs.business.log.domain.TaskLog();
taskLog.setMethod(method);
taskLog.setClassPath(className);
taskLog.setRequestTime(between);
//根据目标的方法名判断当前方法
for (Method thisMethod : methods) {
if (method.equals(thisMethod.getName())) {
Annotation[] declaredAnnotations = thisMethod.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (annotation instanceof TaskLog) {
String name = ((TaskLog) annotation).name();
taskLog.setName(name);
}
}
}
}
R<Object> r = remoteLogFeign.saveTaskLog(taskLog);
log.info(r.getMsg());
}
}

@ -1,14 +1,12 @@
package com.xjs.job.task.openapi;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.api.RemoteCommonFeign;
import com.xjs.job.aop.TaskLog;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
* api
@ -22,17 +20,14 @@ public class CheckApiStatusTask {
@Resource
private RemoteCommonFeign remoteCommonFeign;
@TaskLog(name = "检查API状态任务")
public void checkApiStatus() {
log.info("---------------检查api状态定时任务Start-------------------");
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
R r = remoteCommonFeign.CheckApiStatusForRPC();
log.info("检查api状态定时任务结果:code={},msg={}",r.getCode(),r.getMsg());
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("检查api状态定时任务Job耗费时间:{}ms", between);
log.info("---------------检查api状态定时任务end---------------------");

@ -4,13 +4,13 @@ import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.api.RemoteCopyWritingFeign;
import com.xjs.business.api.domain.CopyWriting;
import com.xjs.job.aop.TaskLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
*
@ -26,19 +26,17 @@ public class CopyWritingTask {
private static final Logger log = LoggerFactory.getLogger(CopyWritingTask.class);
/**
*
*
*/
@TaskLog(name = "文案任务")
public void execute() {
log.info("---------------文案定时任务Start-------------------");
//该循环会导致执行i次重复删除sql
for (int i = 0; i < 8; i++) {
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
R<CopyWriting> r = remoteCopyWritingFeign.copyWriting();
log.info("文案定时任务[{}]结果:code={},msg={},data={}",i,r.getCode(),r.getMsg(),r.getData());
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("文案[{}]定时任务Job耗费时间:{}ms", i,between);
}
log.info("---------------文案定时任务end---------------------");

@ -1,14 +1,12 @@
package com.xjs.job.task.openapi;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.api.RemoteTopSearchFeign;
import com.xjs.job.aop.TaskLog;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
@ -27,16 +25,13 @@ public class TopSearchTask {
/**
*
*/
@TaskLog(name = "热搜榜任务")
public void getTopSearch() {
log.info("---------------热搜榜定时任务Start-------------------");
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
R<Map<String, List>> mapR = remoteTopSearchFeign.topSearchForRPC();
log.info("热搜榜定时任务结果:code={},msg={}",mapR.getCode(),mapR.getMsg());
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("热搜榜定时任务Job耗费时间:{}ms", between);
log.info("---------------热搜榜定时任务end---------------------");
}

@ -7,12 +7,11 @@ import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.warning.RemoteWarningCRUDFeign;
import com.xjs.business.warning.domain.ApiRecord;
import com.xjs.job.aop.TaskLog;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
@ -32,8 +31,8 @@ public class WarningTask {
/**
* api
*/
@TaskLog(name = "预警任务")
public void handleRecordDate() {
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
log.info("---------------预警次数初始化定时任务Start-------------------");
R<JSONArray> listR = remoteWarningCRUDFeign.findRecordListForRPC();
log.info("预警次数初始化定时任务结果:code={},msg={},data={}",listR.getCode(),listR.getMsg(),listR.getData());
@ -43,9 +42,6 @@ public class WarningTask {
handleDate.forEach(data -> {
remoteWarningCRUDFeign.updateApiRecordForRPC(data);
});
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("预警次数初始化定时任务Job耗费时间:{}ms", between);
log.info("---------------预警次数初始化定时任务end---------------------");
}

@ -3,6 +3,7 @@ package com.xjs.job.task.openapi;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.api.RemoteWeatherFeign;
import com.xjs.job.aop.TaskLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@ -25,16 +26,14 @@ public class WeatherTask {
private static final Logger log = LoggerFactory.getLogger(WeatherTask.class);
/**
*
*
*/
@TaskLog(name = "天气任务")
public void execute() {
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
log.info("---------------天气定时任务Start-------------------");
R r = remoteWeatherFeign.getWeatherForRPC();
log.info("天气定时任务结果:code={},msg={},data={}",r.getCode(),r.getMsg(),r.getData());
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("Job耗费时间:{}ms", between);
log.info("---------------天气定时任务end---------------------");
}

@ -3,6 +3,7 @@ package com.xjs.job.task.webmagic;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.webmagic.RemoteWebmagicCopyWritingNetworkFeign;
import com.xjs.job.aop.TaskLog;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
@ -25,16 +26,13 @@ public class CopyWritingNetworkTask {
/**
*
*/
@TaskLog(name = "文案网爬虫任务")
public void copyWritingNetwork() {
log.info("---------------爬虫-文案网定时任务Start-------------------");
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
R r = remoteWebmagicCopyWritingNetworkFeign.copyWritingNetworkTaskForPRC();
log.info("爬虫-文案网定时任务结果:code={},msg={},data={}",r.getCode(),r.getMsg(),r.getData());
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("爬虫-文案网定时任务Job耗费时间:{}ms", between);
log.info("---------------爬虫-文案网定时任务end---------------------");
}
}

@ -1,14 +1,12 @@
package com.xjs.job.task.webmagic;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.webmagic.RemoteWebmagicSinaFeign;
import com.xjs.job.aop.TaskLog;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
*
@ -24,16 +22,13 @@ public class SinaTask {
/**
*
*/
@TaskLog(name = "新浪新闻爬虫任务")
public void sinaNews() {
log.info("---------------爬虫-新浪新闻定时任务Start-------------------");
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
R r = remoteWebmagicSinaFeign.sinaTaskForPRC();
log.info("爬虫-新浪新闻定时任务结果:code={},msg={},data={}",r.getCode(),r.getMsg(),r.getData());
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("爬虫-新浪新闻定时任务Job耗费时间:{}ms", between);
log.info("---------------爬虫-新浪新闻定时任务end---------------------");
}
}

@ -3,6 +3,7 @@ package com.xjs.job.task.webmagic;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.webmagic.RemoteWebmagicWeiXinSouGouFeign;
import com.xjs.job.aop.TaskLog;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
@ -22,16 +23,13 @@ public class WeiXinSouGouTask {
@Resource
private RemoteWebmagicWeiXinSouGouFeign remoteWebmagicWeiXinSouGouFeign;
@TaskLog(name = "微信搜狗爬虫任务")
public void weiXinSouGou() {
log.info("---------------爬虫-微信搜狗定时任务Start-------------------");
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
R r = remoteWebmagicWeiXinSouGouFeign.WeiXinSouGouTaskForPRC();
log.info("爬虫-微信搜狗定时任务结果:code={},msg={},data={}",r.getCode(),r.getMsg(),r.getData());
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("爬虫-微信搜狗定时任务Job耗费时间:{}ms", between);
log.info("---------------爬虫-微信搜狗定时任务end---------------------");
}

@ -3,6 +3,7 @@ package com.xjs.job.task.webmagic;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.webmagic.RemoteWebmagic36wallpaperFeign;
import com.xjs.job.aop.TaskLog;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
@ -25,16 +26,13 @@ public class _36wallpaperTask {
/**
* 36
*/
@TaskLog(name = "36壁纸爬虫任务")
public void _36wallpaper() {
log.info("---------------爬虫-36壁纸网定时任务Start-------------------");
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
R r = remoteWebmagic36wallpaperFeign._36wallpaperTaskForPRC();
log.info("爬虫-36壁纸网定时任务结果:code={},msg={},data={}",r.getCode(),r.getMsg(),r.getData());
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
log.info("爬虫-36壁纸网定时任务Job耗费时间:{}ms", between);
log.info("---------------爬虫-36壁纸网定时任务end---------------------");
}

@ -0,0 +1,13 @@
<template>
</template>
<script>
export default {
name: "TaskLog"
}
</script>
<style scoped>
</style>

@ -0,0 +1,36 @@
package com.xjs.tasklog.controller;
import com.ruoyi.common.core.domain.R;
import com.xjs.tasklog.domain.TaskLog;
import com.xjs.tasklog.service.TaskLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* controller
* @author xiejs
* @since 2022-03-01
*/
@RestController
@RequestMapping("taskLog")
@Api(tags = "业务模块-任务日志")
public class TaskLogController {
@Autowired
private TaskLogService taskLogService;
//-----------------------内部调用rpc------------------------
@PostMapping("saveForPRC")
@ApiOperation("供AOP切面RPC远程调用")
public R<Object> saveTaskLog(@RequestBody TaskLog taskLog) {
boolean save = taskLogService.save(taskLog);
return save ? R.ok() : R.fail();
}
}

@ -0,0 +1,43 @@
package com.xjs.tasklog.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.ruoyi.common.core.annotation.Excel;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
*
* @author xiejs
* @since 2022-03-01
*/
@Data
public class TaskLog implements Serializable {
private static final long serialVersionUID = 1L;
/** 主键id */
private Long id;
/** 任务名称 */
@Excel(name = "任务名称")
private String name;
/** 方法名 */
@Excel(name = "方法名")
private String method;
/** 类路径 */
@Excel(name = "类路径")
private String classPath;
/** 请求时间 */
@Excel(name = "请求时间")
private Long requestTime;
@Excel(name = "创建时间",dateFormat = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
private Date createTime;
}

@ -0,0 +1,13 @@
package com.xjs.tasklog.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjs.tasklog.domain.TaskLog;
/**
* mapper
* @author xiejs
* @since 2022-03-01
*/
public interface TaskLogMapper extends BaseMapper<TaskLog> {
}

@ -0,0 +1,12 @@
package com.xjs.tasklog.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xjs.tasklog.domain.TaskLog;
/**
* service
* @author xiejs
* @since 2022-03-01
*/
public interface TaskLogService extends IService<TaskLog> {
}

@ -0,0 +1,16 @@
package com.xjs.tasklog.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjs.tasklog.domain.TaskLog;
import com.xjs.tasklog.mapper.TaskLogMapper;
import com.xjs.tasklog.service.TaskLogService;
import org.springframework.stereotype.Service;
/**
* service
* @author xiejs
* @since 2022-03-01
*/
@Service
public class TaskLogServiceImpl extends ServiceImpl<TaskLogMapper, TaskLog> implements TaskLogService {
}
Loading…
Cancel
Save