服务端性能监控最佳实践(二)—— Spring Boot Actuator介绍
文章目录
监控后台服务是否正常运行,有很多指标需要我们关注,一是机器本身的状态,比如CPU利用率、磁盘使用率、内存、网络等,通过这些来判断机器是否运行正常。这些是属于机器指标,一般云服务商会提供。今天我们要分析的是程序的性能指标,因为即使机器正常,但程序可能已经挂了。
对java程序来说,我们主要关注JVM的状态是否正常,希望能把一般通过jconsole
得到的数据能通过监控自动获取出趋势图。此时我们就需要一个非常强大的模块:Spring Boot Actuator
来帮助获取到应用的统计信息。Spring Boot Actuator
不止能提供jvm相关信息,也能对应用相关依赖做健康检查,功能十分强大,接入非常简单。
给一个Maven项目增加Actuator
使用如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
介绍Endpoints
Endpoints是Actuator中的端点,每个端点都提供了不同的功能,Actuator内置了很多Endpoints,配置就可以使用。
如/health
提供了应用健康信息。
/info
提供了应用基本信息。
/logfile
可以直接查看日志文件等等。
同时我们也可以定义自己的Endpoints,以此来自定义想要的监控项。
查看所有endpoints
启动项目,访问http://localhost:8080/actuator查看,你应该能看到如下列表
{
"_links":{
"self":{
"href":"http://localhost:7777/actuator",
"templated":false
},
"auditevents":{
"href":"http://localhost:7777/actuator/auditevents",
"templated":false
},
"health":{
"href":"http://localhost:7777/actuator/health",
"templated":false
},
"env":{
"href":"http://localhost:7777/actuator/env",
"templated":false
}
}
}
访问/actuator获取到的是当前已开启的endpoints列表,有一些文档说很多endpoints是默认开启的,但从Spring Boot2.0之后,因为安全原因,大部分的endpoints都是默认关闭的了,需要在配置文件中手动开启。
配置endpoints的开关
在配置文件中,可以通过include
和exclude
配置endpoint在http或jmx中是否开启或关闭,如下。
management:
endpoints:
web:
exposure:
exclude: shutdown
include: ["auditevents", "info", "health", "metrics", "loggers", "logfile", "httptrace", "env", "flyway", "mappings",
"scheduledtasks", "prometheus"]
jmx:
exposure:
include: * #所有
这样就能在/actuator
页面看到大多数的endpoints了。
重要的endpoint
id | <
---|