Spring Boot (十九):使用 Spring Boot Actuator 监控应用
本文内容纲要:
-Actuator监控
-Actuator的REST接口
-快速上手
-相关配置
-命令详解
-health
-info
-beans
-conditions
-heapdump
-shutdown
-mappings
-threaddump
-参考
微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题?
在这种框架下,微服务的监控显得尤为重要。本文主要结合SpringBootActuator,跟大家一起分享微服务SpringBootActuator的常见用法,方便我们在日常中对我们的微服务进行监控治理。
Actuator监控
SpringBoot使用“习惯优于配置的理念”,采用包扫描和自动化配置的机制来加载依赖Jar中的Springbean,不需要任何Xml配置,就可以实现Spring的所有配置。虽然这样做能让我们的代码变得非常简洁,但是整个应用的实例创建和依赖关系等信息都被离散到了各个配置类的注解上,这使得我们分析整个应用中资源和实例的各种关系变得非常的困难。
Actuator是SpringBoot提供的对应用系统的自省和监控的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的Springbeans以及一些环境属性等。
为了保证actuator暴露的监控接口的安全性,需要添加安全控制的依赖spring-boot-start-security依赖,访问应用监控端点时,都需要输入验证信息。Security依赖,可以选择不加,不进行安全管理,但不建议这么做。
Actuator的REST接口
Actuator监控分成两类:原生端点和用户自定义端点;自定义端点主要是指扩展性,用户可以根据自己的实际应用,定义一些比较关心的指标,在运行期进行监控。
原生端点是在应用程序里提供众多Web接口,通过它们了解应用程序运行时的内部状况。原生端点又可以分成三类:
- 应用配置类:可以查看应用在运行期的静态信息:例如自动配置信息、加载的springbean信息、yml文件配置信息、环境信息、请求映射信息;
- 度量指标类:主要是运行期的动态信息,例如堆栈、请求连、一些健康指标、metrics信息等;
- 操作控制类:主要是指shutdown,用户可以发送一个请求将应用的监控功能关闭。
Actuator提供了13个接口,具体如下表所示。
| HTTP方法 | 路径 | 描述 |
|---|---|---|
| GET | /auditevents | 显示应用暴露的审计事件(比如认证进入、订单失败) |
| GET | /beans | 描述应用程序上下文里全部的Bean,以及它们的关系 |
| GET | /conditions | 就是1.0的/autoconfig,提供一份自动配置生效的条件情况,记录哪些自动配置条件通过了,哪些没通过 |
| GET | /configprops | 描述配置属性(包含默认值)如何注入Bean |
| GET | /env | 获取全部环境属性 |
| GET | /env/{name} | 根据名称获取特定的环境属性值 |
| GET | /flyway | 提供一份Flyway数据库迁移信息 |
| GET | /liquidbase | 显示Liquibase数据库迁移的纤细信息 |
| GET | /health | 报告应用程序的健康指标,这些值由HealthIndicator的实现类提供 |
| GET | /heapdump | dump一份应用的JVM堆信息 |
| GET | /httptrace | 显示HTTP足迹,最近100个HTTPrequest/repsponse |
| GET | /info | 获取应用程序的定制信息,这些信息由info打头的属性提供 |
| GET | /logfile | 返回logfile中的内容(如果logging.file或者logging.path被设置) |
| GET | /loggers | 显示和修改配置的loggers |
| GET | /metrics | 报告各种应用程序度量信息,比如内存用量和HTTP请求计数 |
| GET | /metrics/{name} | 报告指定名称的应用程序度量值 |
| GET | /scheduledtasks | 展示应用中的定时任务信息 |
| GET | /sessions | 如果我们使用了SpringSession展示应用中的HTTPsessions信息 |
| POST | /shutdown | 关闭应用程序,要求endpoints.shutdown.enabled设置为true |
| GET | /mappings | 描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系 |
| GET | /threaddump | 获取线程活动的快照 |
快速上手
相关配置
项目依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
配置文件
info.app.name=spring-boot-actuator
info.app.version=1.0.0
info.app.test=test
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#management.endpoints.web.base-path=/monitor
management.endpoint.shutdown.enabled=true
management.endpoints.web.base-path=/monitor代表启用单独的url地址来监控SpringBoot应用,为了安全一般都启用独立的端口来访问后端的监控信息management.endpoint.shutdown.enabled=true启用接口关闭SpringBoot
配置完成之后,启动项目就可以继续验证各个监控功能了。
命令详解
在SpringBoot2.x中为了安全期间,Actuator只开放了两个端点/actuator/health和/actuator/info。可以在配置文件中设置打开。
可以打开所有的监控点
management.endpoints.web.exposure.include=*
也可以选择打开部分
management.endpoints.web.exposure.exclude=beans,trace
Actuator默认所有的监控点路径都在/actuator/*,当然如果有需要这个路径也支持定制。
management.endpoints.web.base-path=/manage
设置完重启后,再次访问地址就会变成/manage/*
Actuator几乎监控了应用涉及的方方面面,我们重点讲述一些经常在项目中常用的命令。
health
health主要用来检查应用的运行状态,这是我们使用最高频的一个监控点。通常使用此接口提醒我们应用实例的运行状态,以及应用不”健康“的原因,比如数据库连接、磁盘空间不够等。
默认情况下health的状态是开放的,添加依赖后启动项目,访问:http://localhost:8080/actuator/health即可看到应用的状态。
{
"status":"UP"
}
默认情况下,最终的SpringBoot应用的状态是由HealthAggregator汇总而成的,汇总的算法是:
- 1设置状态码顺序:
setStatusOrder(Status.DOWN,Status.OUT_OF_SERVICE,Status.UP,Status.UNKNOWN);。 - 2过滤掉不能识别的状态码。
- 3如果无任何状态码,整个SpringBoot应用的状态是UNKNOWN。
- 4将所有收集到的状态码按照1中的顺序排序。
- 5返回有序状态码序列中的第一个状态码,作为整个SpringBoot应用的状态。
health通过合并几个健康指数检查应用的健康情况。SpringBootActuator有几个预定义的健康指标比如DataSourceHealthIndicator,DiskSpaceHealthIndicator,MongoHealthIndicator,RedisHealthIndicator等,它使用这些健康指标作为健康检查的一部分。
举个例子,如果你的应用使用Redis,RedisHealthindicator将被当作检查的一部分;如果使用MongoDB,那么MongoHealthIndicator将被当作检查的一部分。
可以在配置文件中关闭特定的健康检查指标,比如关闭redis的健康检查:
management.health.redise.enabled=false
默认,所有的这些健康指标被当作健康检查的一部分。
info
info就是我们自己配置在配置文件中以info开头的配置信息,比如我们在示例项目中的配置是:
info.app.name=spring-boot-actuator
info.app.version=1.0.0
info.app.test=test
启动示例项目,访问:http://localhost:8080/actuator/info返回部分信息如下:
{
"app":{
"name":"spring-boot-actuator",
"version":"1.0.0",
"test":"test"
}
}
beans
根据示例就可以看出,展示了bean的别名、类型、是否单例、类的地址、依赖等信息。
启动示例项目,访问:http://localhost:8080/actuator/beans返回部分信息如下:
[
{
"context":"application:8080:management",
"parent":"application:8080",
"beans":[
{
"bean":"embeddedServletContainerFactory",
"aliases":[
],
"scope":"singleton",
"type":"org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory",
"resource":"null",
"dependencies":[
]
},
{
"bean":"endpointWebMvcChildContextConfiguration",
"aliases":[
],
"scope":"singleton",
"type":"org.springframework.boot.actuate.autoconfigure.EndpointWebMvcChildContextConfiguration$$EnhancerBySpringCGLIB$$a4a10f9d",
"resource":"null",
"dependencies":[
]
}
}
]
conditions
SpringBoot的自动配置功能非常便利,但有时候也意味着出问题比较难找出具体的原因。使用conditions可以在应用运行时查看代码了某个配置在什么条件下生效,或者某个自动配置为什么没有生效。
启动示例项目,访问:http://localhost:8080/actuator/conditions返回部分信息如下:
{
"positiveMatches":{
"DevToolsDataSourceAutoConfiguration":{
"notMatched":[
{
"condition":"DevToolsDataSourceAutoConfiguration.DevToolsDataSourceCondition",
"message":"DevToolsDataSourceConditiondidnotfindasingleDataSourcebean"
}
],
"matched":[]
},
"RemoteDevToolsAutoConfiguration":{
"notMatched":[
{
"condition":"OnPropertyCondition",
"message":"@ConditionalOnProperty(spring.devtools.remote.secret)didnotfindproperty'secret'"
}
],
"matched":[
{
"condition":"OnClassCondition",
"message":"@ConditionalOnClassfoundrequiredclasses'javax.servlet.Filter','org.springframework.http.server.ServerHttpRequest';@ConditionalOnMissingClassdidnotfindunwantedclass"
}
]
}
}
}
heapdump
返回一个GZip压缩的JVM堆dump
启动示例项目,访问:http://localhost:8080/actuator/heapdump会自动生成一个Jvm的堆文件heapdump,我们可以使用JDK自带的Jvm监控工具VisualVM打开此文件查看内存快照。类似如下图:
shutdown
开启接口优雅关闭SpringBoot应用,要使用这个功能首先需要在配置文件中开启:
management.endpoint.shutdown.enabled=true
配置完成之后,启动示例项目,使用curl模拟post请求访问shutdown接口。
shutdown接口默认只支持post请求。
curl-XPOST"http://localhost:8080/actuator/shutdown"
{
"message":"Shuttingdown,bye..."
}
此时你会发现应用已经被关闭。
mappings
描述全部的URI路径,以及它们和控制器的映射关系
启动示例项目,访问:http://localhost:8080/actuator/mappings返回部分信息如下:
{
"/**/favicon.ico":{
"bean":"faviconHandlerMapping"
},
"{[/hello]}":{
"bean":"requestMappingHandlerMapping",
"method":"publicjava.lang.Stringcom.neo.controller.HelloController.index()"
},
"{[/error]}":{
"bean":"requestMappingHandlerMapping",
"method":"publicorg.springframework.http.ResponseEntity<java.util.Map<java.lang.String,java.lang.Object>>org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)"
}
}
threaddump
/threaddump接口会生成当前线程活动的快照。这个功能非常好,方便我们在日常定位问题的时候查看线程的情况。
主要展示了线程名、线程ID、线程的状态、是否等待锁资源等信息。
启动示例项目,访问:http://localhost:8080/actuator/threaddump返回部分信息如下:
[
{
"threadName":"http-nio-8088-exec-6",
"threadId":49,
"blockedTime":-1,
"blockedCount":0,
"waitedTime":-1,
"waitedCount":2,
"lockName":"java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@1630a501",
"lockOwnerId":-1,
"lockOwnerName":null,
"inNative":false,
"suspended":false,
"threadState":"WAITING",
"stackTrace":[
{
"methodName":"park",
"fileName":"Unsafe.java",
"lineNumber":-2,
"className":"sun.misc.Unsafe",
"nativeMethod":true
},
...
{
"methodName":"run",
"fileName":"TaskThread.java",
"lineNumber":61,
"className":"org.apache.tomcat.util.threads.TaskThread$WrappingRunnable",
"nativeMethod":false
}
...
],
"lockInfo":{
"className":"java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject",
"identityHashCode":372286721
}
}
...
]
生产出现问题的时候,可以通过应用的线程快照来检测应用正在执行的任务。
文章内容已经升级到SpringBoot2.x
示例代码-github
示例代码-码云
参考
SpringBootActuator:Production-readyfeatures
对没有监控的微服务SayNo!
SpringBootActuator使用
本文内容总结:Actuator监控,Actuator的REST接口,快速上手,相关配置,命令详解,health,info,beans,conditions,heapdump,shutdown,mappings,threaddump,参考,
原文链接:https://www.cnblogs.com/ityouknow/p/8423590.html