AOP切面编程,统计接口耗时

最近开发要求接口响应时间不能超过1s,在此写了一个耗时注解,直接注解到接口方法上

添加一个annotation

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CostTime {
}

添加aspect

最开始此处添加了异常捕获造成接口获取不到异常消息或者获取不到自定义消息

@Aspect
@Component
@Slf4j
public class CostTimeAspect {
	//此处修改为完整包路径
    @Pointcut("@annotation(cn.*.annotation.CostTime)")
    public void costTime(){}

    @Around("costTime()")
    public Object costTimeAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object obj = null ;
        Long beginTime = System.currentTimeMillis() ;
        obj = joinPoint.proceed() ;
        //获取识别信息
        String className = joinPoint.getSignature().getDeclaringTypeName() ;
        String methodName = joinPoint.getSignature().getName();
        //耗时
        Long useTime = System.currentTimeMillis()-beginTime ;
        log.info("类:[{}],方法:[{}] ,接口耗时:[{}ms]",className,methodName,useTime);
        return obj ;
    }
}

调用

    @ApiOperation("详情")
    @GetMapping("/info/{serial}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "ok",response = MaterialInfoRes.class)
    })
    @CostTime
    public Result info(@PathVariable("serial")String serial){
        MaterialInfoRes res = materialService.info(serial) ;
        return Result.ok().put("data",res) ;
    }

调用结果

在这里插入图片描述
针对耗时多的接口再细分析处理

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值