Spring Actuator源码分析(转)
转自: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源码分析(转)的更多相关文章
- Spring Security 源码分析 --- WebSecurity
概述 spring security 源码分析系列文章. 源码分析 我们想一下,我们使用 ss 框架的步骤是怎么样的. @Configuration @EnableWebSecurity @Enabl ...
- Spring Security 源码分析(四):Spring Social实现微信社交登录
社交登录又称作社会化登录(Social Login),是指网站的用户可以使用腾讯QQ.人人网.开心网.新浪微博.搜狐微博.腾讯微博.淘宝.豆瓣.MSN.Google等社会化媒体账号登录该网站. 前言 ...
- spring事务源码分析结合mybatis源码(一)
最近想提升,苦逼程序猿,想了想还是拿最熟悉,之前也一直想看但没看的spring源码来看吧,正好最近在弄事务这部分的东西,就看了下,同时写下随笔记录下,以备后查. spring tx源码分析 这里只分析 ...
- spring AOP源码分析(三)
在上一篇文章 spring AOP源码分析(二)中,我们已经知道如何生成一个代理对象了,那么当代理对象调用代理方法时,增强行为也就是拦截器是如何发挥作用的呢?接下来我们将介绍JDK动态代理和cglib ...
- Spring AOP 源码分析 - 拦截器链的执行过程
1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...
- Spring AOP 源码分析 - 创建代理对象
1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...
- Spring AOP 源码分析 - 筛选合适的通知器
1.简介 从本篇文章开始,我将会对 Spring AOP 部分的源码进行分析.本文是 Spring AOP 源码分析系列文章的第二篇,本文主要分析 Spring AOP 是如何为目标 bean 筛选出 ...
- Spring AOP 源码分析系列文章导读
1. 简介 前一段时间,我学习了 Spring IOC 容器方面的源码,并写了数篇文章对此进行讲解.在写完 Spring IOC 容器源码分析系列文章中的最后一篇后,没敢懈怠,趁热打铁,花了3天时间阅 ...
- Spring IOC 源码分析
Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢?阅读本文 ...
随机推荐
- hihocode 1336 Matrix Sum 【二维树状数组】
题目 两个操作: 1. Add x y value: Add value to the element Axy. (Subscripts starts from 0 2. Sum x1 y1 x2 y ...
- windows安装mysql 5.7
1.下载mysql 5.7 压缩包,解压在D:\software\mysql\目录下,更名称mysql-5.7.22 ,并新建data空文件夹和my.ini文件 my.ini文件的内容 [client ...
- win10专业版激活工具很不错!
下载链接: http://www.yishimei.cn/?=0xj20
- [ASE][Daily Scrum]11.10
明天就是一年一度的光棍节了 提前提醒各位已经有妹子蓝孩子藏好自己的银行卡. 涛哥上周回家去了,进度上稍有一些耽搁不过今天就能补完所以影响不严重. 此外纠正我(PM-poor man)之前一个错误,我之 ...
- neo4j图数据库安装以及基本操作命令
neo4j图数据安装以及基本操作命令 neo4j安装配置使用, 安装环境:Ubuntu系统 jdk1.8 (neo4j 只支持jdk1.8以上版本) 下载 neo4j neo4j的下载window可以 ...
- 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation
[源码下载] 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation 作者:webabcd 介绍背水一战 Wind ...
- Spring IOC 容器源码分析 - 获取单例 bean
1. 简介 为了写 Spring IOC 容器源码分析系列的文章,我特地写了一篇 Spring IOC 容器的导读文章.在导读一文中,我介绍了 Spring 的一些特性以及阅读 Spring 源码的一 ...
- Android---------------Handler的学习
public LocalVPNService() { mHandlerThread = new HandlerThread(TAG); mHandlerThread.start(); mBackgro ...
- 百度小程序button去掉默认边框
百度小程序button去掉默认边框: button::after{ border:none; }
- Atom 清空残余数据
rm -rf ~/.atom rm /usr/local/bin/atom rm /usr/local/bin/apm rm ~/Library/Preferences/com.github.atom ...