parent
3cbfd09956
commit
ede181d001
@ -0,0 +1,39 @@
|
||||
package com.xjs.business.log.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 爬虫日志实体类
|
||||
* @author xiejs
|
||||
* @since 2022-02-17
|
||||
*/
|
||||
@Data
|
||||
public class WebmagicLog implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 主键 */
|
||||
private Long id;
|
||||
|
||||
/** 爬虫名称 */
|
||||
private String name;
|
||||
|
||||
/** 爬虫地址 */
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 复杂度
|
||||
*/
|
||||
private Long complexRate;
|
||||
|
||||
private Integer status;
|
||||
|
||||
/** 请求耗费时间(单位毫秒) */
|
||||
private Long requestTime;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.xjs.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 自定义爬虫日志注解
|
||||
* @author xiejs
|
||||
* @since 2022-02-17
|
||||
*/
|
||||
@Target({ ElementType.PARAMETER, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ReptileLog {
|
||||
|
||||
/**
|
||||
* 爬虫名称
|
||||
*/
|
||||
String name() default "";
|
||||
|
||||
/**
|
||||
* 请求url
|
||||
*/
|
||||
String url() default "";
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.xjs.reptileLog.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.reptileLog.domain.WebmagicLog;
|
||||
import com.xjs.reptileLog.service.WebmagicLogService;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 爬虫日志控制器
|
||||
* @author xiejs
|
||||
* @since 2022-02-17
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("reptileLog")
|
||||
@Api(tags = "业务模块-爬虫日志")
|
||||
public class WebmagicLogController {
|
||||
|
||||
@Autowired
|
||||
private WebmagicLogService webmagicLogService;
|
||||
|
||||
|
||||
|
||||
//-----------------------内部调用rpc------------------------
|
||||
|
||||
@PostMapping("saveForPRC")
|
||||
@ApiOperation("供AOP切面RPC远程调用")
|
||||
public R<Object> saveReptileLog(@RequestBody WebmagicLog webmagicLog) {
|
||||
boolean save = webmagicLogService.save(webmagicLog);
|
||||
return save?R.ok():R.fail();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.xjs.reptileLog.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-02-17
|
||||
*/
|
||||
@Data
|
||||
public class WebmagicLog implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 主键 */
|
||||
private Long id;
|
||||
|
||||
/** 爬虫名称 */
|
||||
@Excel(name = "爬虫名称")
|
||||
private String name;
|
||||
|
||||
/** 爬虫地址 */
|
||||
@Excel(name = "爬虫地址")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 复杂度
|
||||
*/
|
||||
@Excel(name = "复杂度")
|
||||
private Long complexRate;
|
||||
|
||||
|
||||
@Excel(name = "执行结果",readConverterExp = "1=成功,2=失败")
|
||||
private Integer status;
|
||||
|
||||
/** 请求耗费时间(单位毫秒) */
|
||||
@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.reptileLog.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xjs.reptileLog.domain.WebmagicLog;
|
||||
|
||||
/**
|
||||
* WebmagicLog mapper
|
||||
* @author xiejs
|
||||
* @since 2022-02-17
|
||||
*/
|
||||
public interface WebmagicLogMapper extends BaseMapper<WebmagicLog> {
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.xjs.reptileLog.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.xjs.reptileLog.domain.WebmagicLog;
|
||||
|
||||
/**
|
||||
* 爬虫日志 Service接口
|
||||
* @author xiejs
|
||||
* @since 2022-02-17
|
||||
*/
|
||||
public interface WebmagicLogService extends IService<WebmagicLog> {
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.xjs.reptileLog.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.xjs.reptileLog.domain.WebmagicLog;
|
||||
import com.xjs.reptileLog.mapper.WebmagicLogMapper;
|
||||
import com.xjs.reptileLog.service.WebmagicLogService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @since 2022-02-17
|
||||
*/
|
||||
@Service
|
||||
public class WebmagicLogServiceImpl extends ServiceImpl<WebmagicLogMapper, WebmagicLog> implements WebmagicLogService {
|
||||
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package com.xjs.common.aop;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.annotation.ReptileLog;
|
||||
import com.xjs.business.log.RemoteLogFeign;
|
||||
import com.xjs.business.log.domain.WebmagicLog;
|
||||
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;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static com.xjs.consts.ReqConst.ERROR;
|
||||
import static com.xjs.consts.ReqConst.SUCCESS;
|
||||
|
||||
/**
|
||||
* 爬虫日志切面类
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-02-17
|
||||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
@Log4j2
|
||||
public class reptileLogAspect {
|
||||
|
||||
@Resource
|
||||
private RemoteLogFeign remoteLogFeign;
|
||||
|
||||
/**
|
||||
* 声明AOP签名
|
||||
*/
|
||||
@Pointcut("@annotation(com.xjs.annotation.ReptileLog)")
|
||||
public void pointcut() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 环绕切入
|
||||
*/
|
||||
@Around("pointcut()")
|
||||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
Object obj = null;
|
||||
try {
|
||||
//切入前-----
|
||||
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
|
||||
|
||||
obj = joinPoint.proceed();
|
||||
|
||||
//切入后-----
|
||||
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
|
||||
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
|
||||
log.info("调用爬虫接口耗费时间:{}ms", between);
|
||||
|
||||
this.handle(joinPoint, between, obj);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理切面逻辑
|
||||
*
|
||||
* @param joinPoint 切入点
|
||||
* @param between 请求时长
|
||||
* @param obj 返回值
|
||||
*/
|
||||
private void handle(ProceedingJoinPoint joinPoint, Long between, Object obj) {
|
||||
//获取目标类名及方法名
|
||||
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())) {
|
||||
Annotation[] declaredAnnotations = thisMethod.getDeclaredAnnotations();
|
||||
for (Annotation annotation : declaredAnnotations) {
|
||||
if (annotation instanceof ReptileLog) {
|
||||
String name = ((ReptileLog) annotation).name();
|
||||
String url = ((ReptileLog) annotation).url();
|
||||
|
||||
WebmagicLog webmagicLog = new WebmagicLog();
|
||||
webmagicLog.setName(name);
|
||||
webmagicLog.setUrl(url);
|
||||
webmagicLog.setRequestTime(between);
|
||||
if (obj instanceof Long) {
|
||||
webmagicLog.setComplexRate((Long) obj);
|
||||
}
|
||||
this.saveData(webmagicLog);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 持久化保存数据
|
||||
*/
|
||||
private void saveData(WebmagicLog webmagicLog) {
|
||||
if (webmagicLog.getComplexRate() != null && webmagicLog.getComplexRate() == 0L) {
|
||||
webmagicLog.setStatus(ERROR);
|
||||
} else {
|
||||
webmagicLog.setStatus(SUCCESS);
|
||||
}
|
||||
R<Object> r = remoteLogFeign.saveReptileLog(webmagicLog);
|
||||
log.info(r.getMsg());
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue