转自: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/

Spring Actuator源码分析(转)的更多相关文章

  1. Spring Security 源码分析 --- WebSecurity

    概述 spring security 源码分析系列文章. 源码分析 我们想一下,我们使用 ss 框架的步骤是怎么样的. @Configuration @EnableWebSecurity @Enabl ...

  2. Spring Security 源码分析(四):Spring Social实现微信社交登录

    社交登录又称作社会化登录(Social Login),是指网站的用户可以使用腾讯QQ.人人网.开心网.新浪微博.搜狐微博.腾讯微博.淘宝.豆瓣.MSN.Google等社会化媒体账号登录该网站. 前言 ...

  3. spring事务源码分析结合mybatis源码(一)

    最近想提升,苦逼程序猿,想了想还是拿最熟悉,之前也一直想看但没看的spring源码来看吧,正好最近在弄事务这部分的东西,就看了下,同时写下随笔记录下,以备后查. spring tx源码分析 这里只分析 ...

  4. spring AOP源码分析(三)

    在上一篇文章 spring AOP源码分析(二)中,我们已经知道如何生成一个代理对象了,那么当代理对象调用代理方法时,增强行为也就是拦截器是如何发挥作用的呢?接下来我们将介绍JDK动态代理和cglib ...

  5. Spring AOP 源码分析 - 拦截器链的执行过程

    1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...

  6. Spring AOP 源码分析 - 创建代理对象

    1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...

  7. Spring AOP 源码分析 - 筛选合适的通知器

    1.简介 从本篇文章开始,我将会对 Spring AOP 部分的源码进行分析.本文是 Spring AOP 源码分析系列文章的第二篇,本文主要分析 Spring AOP 是如何为目标 bean 筛选出 ...

  8. Spring AOP 源码分析系列文章导读

    1. 简介 前一段时间,我学习了 Spring IOC 容器方面的源码,并写了数篇文章对此进行讲解.在写完 Spring IOC 容器源码分析系列文章中的最后一篇后,没敢懈怠,趁热打铁,花了3天时间阅 ...

  9. Spring IOC 源码分析

    Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢?阅读本文 ...

随机推荐

  1. 20169207 《Linux内核原理与分析》第十一周作业

    阅读学习教材「Linux内核设计与实现 (Linux Kernel Development)」第教材第17,19,20章. 在第17章设备与模块章节,关于设备驱动和设备管理,我们讨论四中内核成分. 1 ...

  2. POJ3273--Monthly Expense(Binary Search)

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  3. 一篇谈Flink不错的文章

    精华 : 在执行引擎这一层,流处理系统与批处理系统最大不同在于节点间的数据传输方式.对于一个流处理系统,其节点间数据传输的标准模型是:当一条数据被处理完成后,序列化到缓存中,然后立刻通过网络传输到下一 ...

  4. spark图解

    导语 spark 已经成为广告.报表以及推荐系统等大数据计算场景中首选系统,因效率高,易用以及通用性越来越得到大家的青睐,我自己最近半年在接触spark以及spark streaming之后,对spa ...

  5. 使用PinYin4j.jar将汉字转换为拼音

    package com.Test.util; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j ...

  6. Cerebro_变量名搜索插件

    Cerebro 安装 兼容环境:Windows, MacOS, Linux 插件依赖于 Cerebro,下载地址: https://github.com/KELiON/cerebro/releases ...

  7. Python自动化开发 - select模块

    介绍: IO-多路复用:监听多个socker对象是否有变化,包括可读.可写.发送错误 Python中的select模块专注于I/O多路复用,提供了select poll epoll三个方法(其中后两个 ...

  8. I - Dividing Stones

    Description There are N stones, which can be divided into some piles arbitrarily. Let the value of e ...

  9. unigui如何连接数据库

    unigui如何连接数据库 UNIGUI既可以二层直连数据库,也可以通过中间件连接数据库. 这里只介绍UNIGUI二层直连数据库. 数据库连接控件.数据集控件都要拖放在MainModule窗体上.UN ...

  10. 1*1卷积核在GoogleLeNet中的作用

    1. 实现跨通道的交互和信息整合 1×1的卷积层(可能)引起人们的重视是在NIN的结构中,论文中林敏师兄的想法是利用MLP代替传统的线性卷积核,从而提高网络的表达能力.文中同时利用了跨通道poolin ...