转自: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. HDU 2546 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2546 经典的01背包 预留5元买最贵的,剩余的就是01背包. #include<stdio.h> # ...

  2. (DP 雷格码)Gray code -- hdu -- 5375

    http://acm.hdu.edu.cn/showproblem.php?pid=5375 Gray code Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. 万能头文件#include <bits/stdc++.h>

    最近在做题的时候看到别人的题解发现别人都用这个 突然之间打开新世界的大门 去百度之后才知道#include <bits/stdc++.h>包含了目前所有的c++头文件 也就是说只要用#in ...

  4. 解决UNIGUI字体太小的问题

    解决UNIGUI字体太小的问题 Unigui运行在chrome浏览器下可以有最佳的效果,但用ie打开用unigui做的项目会发现字体明显小一截,可以用自定义css来解决这个问题. 可以在UniServ ...

  5. 关于质能等效的两个思想实验 Two Ideological Experiments on Mass-Energy Equivalence

    大家知道,物质和能量是等效的,虽然质能方程已暗示了这种等效关系,但并非显而易见.此等效性可以从以下两个思想实验中获知. 实验一:一台电子称上放置一个金属物体,加热它,称的读数将会略微增加.这是因为金属 ...

  6. redis集群(主从配置)

    市面上太多kv的缓存,最常用的就属memcache了,但是memcache存在单点问题,不过小日本有复制版本,但是使用的人比较少,redis的出现让kv内存存储的想法成为现实.今天主要内容便是redi ...

  7. 动态产生select option列表

    在很久之前,Insus.NET刚学习前端时,有写过<动态创建一些常的html标签>http://www.cnblogs.com/insus/p/3741665.html 但其中没有实现动态 ...

  8. XML hexadecimal value 0x__, is an invalid character

    XML操作时异常:(十六进制值 0x__) 是无效的字符. 方法一: 设置 CheckCharacters=false. XmlReaderSettings xmlReaderSettings = n ...

  9. Android开发 - 获取Android设备的唯一标识码(Android 6.0或更高)

    在我们的APP开发中,通常需要获取到设备的唯一标识.在Android6.0之前,有很多方法我们可以方便获取到硬件的唯一标识,但是在Android6.0之后,Android系统大幅限制了我们获取设备的硬 ...

  10. Mac 下查看端口是否被占用

    1. lsof -i :8080 2. netstat -anp tcp | grep 8080 3. nc -w 10 -n -z 127.0.0.1 8070-8090