如何使用actuator
那么,我们如何使用actuator呢?要使用springboot的actuator的监控功能,我们需要在应用中引入相关组件spring-boot-starter-actuator,引入该组件依赖后,应用将自动引入审计、健康检查、监控功能。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Actuator的运行原理
只要在应用中引入上述依赖,则应用运行时就会自动开启/actuator/health和/actuator/info这两个endpoint,当然,我们也可以通过配置只使用一个endpoint,如只使用安全检查, 我们则在应用中添加如下配置文件即可
management:
endpoints:
web:
exposure:
include: health
如果我们需要打开所有端点,使用*即可;打开多个时,可以使用,分割各个端点
例如,我们只配置了health时,在启用应用后,查看actuator中正常运行的端点,如/actuator,/actuator/health/{*path},/actuator/health
访问localhost:8080/actuator/health查看展示状态时,我们发现状态为up
如果我们要查看详细的health信息需要配置management.endpoint.health.show-details 的值为always,再次访问localhost:8080/actuator/health时,可以看到显示了所有节点
参考配置
endpoints:
web:
exposure:
include: health
endpoint:
health:
show-details: always
健康检查的范围
我们从网页上查看到Springboot actuator框架自带的 HealthIndicators目前包括:
HealthIndicator | 描述 |
CassandraHealthIndicator | Checks for low disk space. |
DiskSpaceHealthIndicator | Checks that a connection to DataSource can be obtained. |
DataSourceHealthIndicator | Checks that an Elasticsearch cluster is up. |
ElasticsearchHealthIndicator | Checks that an InfluxDB server is up. |
InfluxDbHealthIndicator | Checks that a JMS broker is up. |
JmsHealthIndicator | Checks that a mail server is up. |
MailHealthIndicatorMongoHealthIndicator | Checks that a Mongo database is up. |
Neo4jHealthIndicator | Checks that a Neo4j server is up. |
RabbitHealthIndicator | Checks that a Rabbit server is up. |
RedisHealthIndicator | Checks that a Redis server is up |
SolrHealthIndicator | Checks that a Solr server is up. |
我们可以使用配置 management.health.defaults.enabled将它们全部禁用掉,也可以使用任意配置management.health.xxxx.enabled某一个禁用掉,
例我们禁用rabbit,再次访问localhost:8080/actuator/health时,可以验证rabbit节点已经没有了
参考配置
endpoints:
web:
exposure:
include: health
health:
rabbit:
enabled: false
web:
exposure:
include: health
health:
rabbit:
enabled: false
下面是内置健康状态类型对应的HTTP状态码列表:
状态 | 描述 |
DOWN | 服务不可达 (503) |
OUT_OF_SERVICE | 服务不可达 (503) |
UP | http status is 200 |
UNKNOWN | http status is 200 |