|
@ -0,0 +1,48 @@
|
|
|
|
package com.yihu.wlyy.jolokia.config.counter;
|
|
|
|
|
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
|
import org.aspectj.lang.annotation.Before;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.actuate.metrics.CounterService;
|
|
|
|
import org.springframework.boot.actuate.metrics.GaugeService;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by chenweida on 2017/7/25.
|
|
|
|
* 访问的接口计数器
|
|
|
|
*/
|
|
|
|
@Aspect
|
|
|
|
@Component
|
|
|
|
public class RestCounter {
|
|
|
|
@Autowired
|
|
|
|
private CounterService counterService;
|
|
|
|
@Autowired
|
|
|
|
private GaugeService gaugeService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 统计访问接口的次数
|
|
|
|
* @param joinPoint
|
|
|
|
*/
|
|
|
|
@Before("execution(* com.test.bookpub.controller.*.*(..))")
|
|
|
|
public void countServiceInvoke(JoinPoint joinPoint) {
|
|
|
|
counterService.increment(joinPoint.getSignature() + "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 计算访问接口的耗时数
|
|
|
|
* @param pjp
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
@Around("execution(* com.test.bookpub.controller.*.*(..))")
|
|
|
|
public void latencyService(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
pjp.proceed();
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
gaugeService.submit(pjp.getSignature().toString(), end - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|