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/

相關文章
相關標籤/搜索