Spring Boot教程(十二)Spring Boot Actuator监控端点小结(2)
2018-02-08 00:00
1661 查看
度量指标类
上面我们所介绍的应用配置类端点所提供的信息报告在应用启动的时候都已经基本确定了其返回内容,可以说是一个静态报告。而度量指标类端点提供的报告内容则是动态变化的,这些端点提供了应用程序在运行过程中的一些快照信息,比如:内存使用情况、HTTP请求统计、外部资源指标等。这些端点对于我们构建微服务架构中的监控系统非常有帮助,由于Spring Boot应用自身实现了这些端点,所以我们可以很方便地利用它们来收集我们想要的信息,以制定出各种自动化策略。下面,我们就来分别看看这些强大的端点功能。/metrics:该端点用来返回当前应用的各类重要度量指标,比如:内存信息、线程信息、垃圾回收信息等。
{ "mem": 541305, "mem.free": 317864, "processors": 8, "instance.uptime": 33376471, "uptime": 33385352, "systemload.average": -1, "heap.committed": 476672, "heap.init": 262144, "heap.used": 158807, "heap": 3701248, "nonheap.committed": 65856, "nonheap.init": 2496, "nonheap.used": 64633, "nonheap": 0, "threads.peak": 22, "threads.daemon": 20, "threads.totalStarted": 26, "threads": 22, "classes": 7669, "classes.loaded": 7669, "classes.unloaded": 0, "gc.ps_scavenge.count": 7, "gc.ps_scavenge.time": 118, "gc.ps_marksweep.count": 2, "gc.ps_marksweep.time": 234, "httpsessions.max": -1, "httpsessions.active": 0, "gauge.response.beans": 55, "gauge.response.env": 10, "gauge.response.hello": 5, "gauge.response.metrics": 4, "gauge.response.configprops": 153, "gauge.response.star-star": 5, "counter.status.200.beans": 1, "counter.status.200.metrics": 3, "counter.status.200.configprops": 1, "counter.status.404.star-star": 2, "counter.status.200.hello": 11, "counter.status.200.env": 1 }从上面的示例中,我们看到有这些重要的度量值:
系统信息:包括处理器数量
processors、运行时间
uptime和
instance.uptime、系统平均负载
systemload.average。
mem.*:内存概要信息,包括分配给应用的总内存数量以及当前空闲的内存数量。这些信息来自
java.lang.Runtime。
对于
gauge.*和
counter.*的统计,这里有一个特殊的内容请求
star-star,它代表了对静态资源的访问。这两类度量指标非常有用,我们不仅可以使用它默认的统计指标,还可以在程序中轻松的增加自定义统计值。只需要通过注入
org.springframework.boot.actuate.metrics.CounterService和
org.springframework.boot.actuate.metrics.GaugeService来实现自定义的统计指标信息。比如:我们可以像下面这样自定义实现对
hello接口的访问次数统计。
heap.*:堆内存使用情况。这些信息来自
java.lang.management.MemoryMXBean接口中
getHeapMemoryUsage方法获取的
java.lang.management.MemoryUsage。
nonheap.*:非堆内存使用情况。这些信息来自
java.lang.management.MemoryMXBean接口中
getNonHeapMemoryUsage方法获取的
java.lang.management.MemoryUsage。
threads.*:线程使用情况,包括线程数、守护线程数(daemon)、线程峰值(peak)等,这些数据均来自
java.lang.management.ThreadMXBean。
classes.*:应用加载和卸载的类统计。这些数据均来自
java.lang.management.ClassLoadingMXBean。
gc.*:垃圾收集器的详细信息,包括垃圾回收次数
gc.ps_scavenge.count、垃圾回收消耗时间
gc.ps_scavenge.time、标记-清除算法的次数
gc.ps_marksweep.count、标记-清除算法的消耗时间
gc.ps_marksweep.time。这些数据均来自
java.lang.management.GarbageCollectorMXBean。
httpsessions.*:Tomcat容器的会话使用情况。包括最大会话数
httpsessions.max和活跃会话数
httpsessions.active。该度量指标信息仅在引入了嵌入式Tomcat作为应用容器的时候才会提供。
gauge.*:HTTP请求的性能指标之一,它主要用来反映一个绝对数值。比如上面示例中的
gauge.response.hello: 5,它表示上一次
hello请求的延迟时间为5毫秒。
counter.*:HTTP请求的性能指标之一,它主要作为计数器来使用,记录了增加量和减少量。如上示例中
counter.status.200.hello: 11,它代表了
hello请求返回
200状态的次数为11。
@RestController public class HelloController { @Autowired private CounterService counterService; @RequestMapping("/hello") public String greet() { counterService.increment("didispace.hello.count"); return ""; } }
/metrics端点可以提供应用运行状态的完整度量指标报告,这项功能非常的实用,但是对于监控系统中的各项监控功能,它们的监控内容、数据收集频率都有所不同,如果我们每次都通过全量获取报告的方式来收集,略显粗暴。所以,我们还可以通过
/metrics/{name}接口来更细粒度的获取度量信息,比如我们可以通过访问
/metrics/mem.free来获取当前可用内存数量。
/health:该端点在一开始的示例中我们已经使用过了,它用来获取应用的各类健康指标信息。在
spring-boot-starter-actuator模块中自带实现了一些常用资源的健康指标检测器。这些检测器都通过
HealthIndicator接口实现,并且会根据依赖关系的引入实现自动化装配,比如用于检测磁盘的
DiskSpaceHealthIndicator、检测DataSource连接是否可用的
DataSourceHealthIndicator等。有时候,我们可能还会用到一些Spring Boot的Starter POMs中还没有封装的产品来进行开发,比如:当使用RocketMQ作为消息代理时,由于没有自动化配置的检测器,所以我们需要自己来实现一个用来采集健康信息的检测器。比如,我们可以在Spring Boot的应用中,为
org.springframework.boot.actuate.health.HealthIndicator接口实现一个对RocketMQ的检测器类:
@Component public class RocketMQHealthIndicator implements HealthIndicator { @Override public Health health() { int errorCode = check(); if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } private int check() { // 对监控对象的检测操作 } }通过重写
health()函数来实现健康检查,返回的
Heath对象中,共有两项内容,一个是状态信息,除了该示例中的
UP与
DOWN之外,还有
UNKNOWN和
OUT_OF_SERVICE,可以根据需要来实现返回;还有一个详细信息,采用Map的方式存储,在这里通过
withDetail函数,注入了一个Error Code信息,我们也可以填入一下其他信息,比如,检测对象的IP地址、端口等。重新启动应用,并访问
/health接口,我们在返回的JSON字符串中,将会包含了如下信息:
"rocketMQ": { "status": "UP" }
/dump:该端点用来暴露程序运行中的线程信息。它使用
java.lang.management.ThreadMXBean的
dumpAllThreads方法来返回所有含有同步信息的活动线程详情。
/trace:该端点用来返回基本的HTTP跟踪信息。默认情况下,跟踪信息的存储采用
org.springframework.boot.actuate.trace.InMemoryTraceRepository实现的内存方式,始终保留最近的100条请求记录。它记录的内容格式如下:
[ { "timestamp": 1482570022463, "info": { "method": "GET", "path": "/metrics/mem", "headers": { "request": { "host": "localhost:8881", "connection": "keep-alive", "cache-control": "no-cache", "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36", "postman-token": "9817ea4d-ad9d-b2fc-7685-9dff1a1bc193", "accept": "*/*", "accept-encoding": "gzip, deflate, sdch", "accept-language": "zh-CN,zh;q=0.8" }, "response": { "X-Application-Context": "hello:dev:8881", "Content-Type": "application/json;charset=UTF-8", "Transfer-Encoding": "chunked", "Date": "Sat, 24 Dec 2016 09:00:22 GMT", "status": "200" } } } }, ... ]
操作控制类
仔细的读者可能会发现,我们在“初识Actuator”时运行示例的控制台中输出的所有监控端点,已经在介绍应用配置类端点和度量指标类端点时都讲解完了。那么还有哪些是操作控制类端点呢?实际上,由于之前介绍的所有端点都是用来反映应用自身的属性或是运行中的状态,相对于操作控制类端点没有那么敏感,所以他们默认都是启用的。而操作控制类端点拥有更强大的控制能力,如果要使用它们的话,需要通过属性来配置开启。在原生端点中,只提供了一个用来关闭应用的端点:
/shutdown。我们可以通过如下配置开启它:
endpoints.shutdown.enabled=true在配置了上述属性之后,只需要访问该应用的
/shutdown端点就能实现关闭该应用的远程操作。由于开放关闭应用的操作本身是一件非常危险的事,所以真正在线上使用的时候,我们需要对其加入一定的保护机制,比如:定制Actuator的端点路径、整合Spring Security进行安全校验等。
源码来源
相关文章推荐
- Spring Boot教程(十一)Spring Boot Actuator监控端点小结(1)
- Spring Boot Actuator监控端点小结
- Spring Boot Actuator监控端点小结
- Spring Boot Actuator监控端点小结
- Spring Boot Actuator监控端点小结
- Spring Boot教程(十三)在传统Spring应用中使用spring-boot-actuator模块提供监控端点
- Spring Boot Actuator监控端点小结
- 在传统Spring应用中使用spring-boot-actuator模块提供监控端点
- spring boot starter actuator(健康监控)配置和使用教程
- 在传统Spring应用中使用spring-boot-actuator模块提供监控端点
- Spring Boot Actuator监控端点
- springboot 使用Spring Boot Actuator监控应用小结
- 在传统Spring应用中使用spring-boot-actuator模块提供监控端点
- Spring Boot参考教程(三)内部应用监控(Actuator)
- springboot【23】监控管理之Actuator监控端点实践
- springboot【24】监控管理之Actuator的/info端点输出Git版本信息
- Spring Boot教程(十四)Spring Boot中使用Actuator的/info端点输出Git版本信息
- Spring Boot 构建应用——使用 Actuator 监控端点
- 监控和管理生产环境spring boot actuator
- SpringBoot actuator 应用监控。