Spring Actuator源码分析(转)

转自:http://blog.csdn.net/wsscy2004/article/details/50166333html

 

Actuator Endpoint

Actuator模块经过Endpoint暴露一些接口,能够是Rest方式,也能够是JMX等其余方式.spring

若是使用Rest方式,一般SpringMVC是使用@RequestMapping,以及@Controller标注一个控制器方法,若是不使用SpringMVC,即没引入SpringMVC的包,那么Springboot就会出错.因此为了避免走正常的SpringMVC机制,Actuator用EndpointHandlerMapping重写了RequestMappingInfoHandlerMapping,匹配的是实现了MvcEndpoint接口的”控制器” 
这里写图片描述app

Endpoint和MvcEndpoint两个的区别? 
MvcEndpoint是对Endpoint SpringMVC层的装饰,添加了@RequestMapping,以及@ResponseBody.具体逻辑委托给Endpoint处理,.Endpoint的id即为url.框架

文档中已经提到了自定义endpoint的方法,curl

Health Check

HealthEndpoint是Actuator自带的Health Check,具体的检查操做都是交由HealthIndicator处理,根据文档,实现 HealthIndicator便可自定义一些Health Check的逻辑,以下ide

@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 是这样的:spring-boot

$ curl http://localhost:8080/health
{
    "status": "DOWN",
    "tair": "timeout",
    "tfs": "ok"
}

HealthIndicatorAutoConfiguration会在EndpointAutoConfiguration以前,自动配置全部的HealthIndicator 
Actuator已经自带了一些HealthIndicator,自动启用部分: 
这里写图片描述ui

多个HealchIndicator会由CompositeHealthIndicator调用HealthAggregator作aggregate(聚合),目前只有OrderedHealthAggregator,用于排序this

Metrics Endpoint

这个Endpoint展现Metrics信息,具体的Metrics是由实现了PublicMetrics接口的类处理. 
MetricsEndpoint维护着一份PublicMetrics列表,Actuator已经实现了以下: 
这里写图片描述 
全部被激活的PublicMetrics,均可以经过访问/metrics查看:url

{
    "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/

相关文章
相关标签/搜索