转自:http://blog.csdn.net/wsscy2004/article/details/50166333
Actuator Endpoint
Actuator模块通过Endpoint暴露一些接口,可以是Rest方式,也可以是JMX等其他方式.
如果使用Rest方式,通常SpringMVC是使用@RequestMapping,以及@Controller标注一个控制器方法,如果不使用SpringMVC,即没引入SpringMVC的包,那么Springboot就会出错.所以为了不走正常的SpringMVC机制,Actuator用EndpointHandlerMapping重写了RequestMappingInfoHandlerMapping,匹配的是实现了MvcEndpoint接口的”控制器”
Endpoint和MvcEndpoint两个的区别?
MvcEndpoint是对Endpoint SpringMVC层的装饰,添加了@RequestMapping,以及@ResponseBody.具体逻辑委托给Endpoint处理,.Endpoint的id即为url.
文档中已经提到了自定义endpoint的方法,
Health Check
HealthEndpoint是Actuator自带的Health Check,具体的检查操作都是交由HealthIndicator处理,根据文档,实现 HealthIndicator即可自定义一些Health Check的逻辑,如下
@Component public class MyHealth implements HealthIndicator { @Override public Health health() { return new Health.Builder() .withDetail("tair", "timeout") // some logic check tair .withDetail("tfs", "ok") // some logic check tfs .status("500") .down() .build(); } }
现在访问 health endpoint 是这样的:
$ curl http://localhost:8080/health { "status": "DOWN", "tair": "timeout", "tfs": "ok" }
HealthIndicatorAutoConfiguration会在EndpointAutoConfiguration之前,自动配置所有的HealthIndicator
Actuator已经自带了一些HealthIndicator,自动启用部分:
多个HealchIndicator会由CompositeHealthIndicator调用HealthAggregator做aggregate(聚合),目前只有OrderedHealthAggregator,用于排序
Metrics Endpoint
这个Endpoint展示Metrics信息,具体的Metrics是由实现了PublicMetrics接口的类处理.
MetricsEndpoint维护着一份PublicMetrics列表,Actuator已经实现了如下:
所有被激活的PublicMetrics,都可以通过访问/metrics
查看:
{ "counter.status.200.root": 20, "counter.status.200.metrics": 3, "counter.status.200.star-star": 5, "counter.status.401.root": 4, "gauge.response.star-star": 6, "gauge.response.root": 2, "gauge.response.metrics": 3, "classes": 5808, "classes.loaded": 5808, "classes.unloaded": 0, "heap": 3728384, "heap.committed": 986624, "heap.init": 262144, "heap.used": 52765, "mem": 986624, "mem.free": 933858, "processors": 8, "threads": 15, "threads.daemon": 11, "threads.peak": 15, "uptime": 494836, "instance.uptime": 489782, "datasource.primary.active": 5, "datasource.primary.usage": 0.25 }
MetricReaderPublicMetrics
通过这个PublicMetrics可以获取到控制器访问情况:
"gauge.response.hi": 5, "counter.status.200.hi": 19,
分别为hi接口响应时间,访问次数.
整个过程:
MetricRepositoryAutoConfiguration -> CounterBuffers,GaugeBuffers用于保存计数数据
MetricRepositoryAutoConfiguration -> 初始化GaugeService + CounterService(内含CounterBuffers,GaugeBuffers)
MetricFilterAutoConfiguration -> 初始化MetricsFilter,该过滤器
使用GaugeService + CounterService统计访问次数以及响应时间
PublicMetricsAutoConfiguration -> 初始化MetricReaderPublicMetrics,塞入CompositeMetricReader(CounterBuffers,GaugeBuffers).
MetricReaderPublicMetrics读取CounterBuffers,GaugeBuffers保存的统计数据
我们重点来看下MetricsFilter
这个过滤器:
自定义Metrics
根据文档,可以在业务代码中注入CounterService或GaugeService来统计信息:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.metrics.CounterService; import org.springframework.stereotype.Service; @Service public class MyService { private final CounterService counterService; @Autowired public MyService(CounterService counterService) { this.counterService = counterService; } @PostConstruct public void exampleMethod() { this.counterService.increment("services.system.myservice.invoked"); } }
@PostConstruct必须添加
当然也可以使用AOP做一个method level的统计.但是我希望做一个与业务无关,集成到框架里的Metrics统计
Reference
http://kielczewski.eu/2015/01/application-metrics-with-spring-boot-actuator/