1.actuator簡介
springboot框架,主要有四個大的特性
-
自動裝配
-
依賴管理
-
健康檢查actuator
-
CLI
自動裝配,與依賴管理只要用過springboot框架,我們都能體會到它的好處;CLI在實際項目中應用較少,不需要很關心;剩下actuator健康檢查,我們是不應該忽略它的!
那麼actuator到底是個何方神聖呢?這麼說吧,在微服務盛行的今天,監控服務狀態是很重要的一個事情,我們需要知道每一個服務的健康狀態。
於是,springboot框架給我們提供了actuator模塊,通過actuator組件可以非常方便的監控springboot應用狀態,提供了全方位的健康檢查能力(比如後面我們可以通過actuator實現服務優雅下線)。
2.搭建actuator環境
要搭建actuator監控環境,只需要遵循搭建springboot應用的三板斧
-
加依賴
-
加註解
-
寫配置
而且這個地方只需要兩步即可:加依賴、寫配置
2.1.加依賴
<!--監控健康檢查依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.2.寫配置
management:
endpoints:
web:
exposure:
include: '*'
exclude: beans,env,metrics
endpoint:
health:
show-details: always
配置說明
-
management.endpoints.web.exposure.include:配置要暴露的端點,*表示暴露所有端點
-
management.endpoints.web.exposure.exclude:配置不要暴露的端點,多個通過逗號隔開
-
management.endpoint.health.show-details:配置是否顯示health端點詳情
3.體驗actuator監控
啓動應用體驗actuator,訪問http://ip:port/actuator,查看暴露的端點信息
// 20210801222532
// http://127.0.0.1:8080/actuator
{
"_links": {
"self": {
"href": "http://127.0.0.1:8080/actuator",
"templated": false
},
"caches-cache": {
"href": "http://127.0.0.1:8080/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://127.0.0.1:8080/actuator/caches",
"templated": false
},
"health": {
"href": "http://127.0.0.1:8080/actuator/health",
"templated": false
},
"health-path": {
"href": "http://127.0.0.1:8080/actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://127.0.0.1:8080/actuator/info",
"templated": false
},
"conditions": {
"href": "http://127.0.0.1:8080/actuator/conditions",
"templated": false
},
"configprops": {
"href": "http://127.0.0.1:8080/actuator/configprops",
"templated": false
},
"loggers": {
"href": "http://127.0.0.1:8080/actuator/loggers",
"templated": false
},
"loggers-name": {
"href": "http://127.0.0.1:8080/actuator/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://127.0.0.1:8080/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://127.0.0.1:8080/actuator/threaddump",
"templated": false
},
"scheduledtasks": {
"href": "http://127.0.0.1:8080/actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://127.0.0.1:8080/actuator/mappings",
"templated": false
}
}
}
訪問health端點:http://ip:port/actuator/health
// 20210801222650
// http://127.0.0.1:8080/actuator/health
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 1000203087872,
"free": 493320859648,
"threshold": 10485760,
"exists": true
}
},
"edu": {
"status": "UP",
"details": {
"status": 666
}
},
"ping": {
"status": "UP"
}
}
}
4.好玩特性
除了基本玩法,actuator還有一些高級的玩法。比如說:
-
修改暴露端點端口
-
定義info端點信息
-
自定義業務監控指標
4.1.修改暴露端點端口
management:
server:
port: 8888
配置management.server.port後,訪問健康檢查端點端口需要換成8888,等於是於業務應用獨立開了
4.2.定義info端點信息
info:
app: follow-me-springboot-actuator
author: yanghouhua
date: 20210731
通過info.xxx的形式,可以給http://ip:port/actuator/info端點增加描述信息,效果
// 20210801223330
// http://127.0.0.1:8080/actuator/info
{
"app": "follow-me-springboot-actuator",
"author": "yanghouhua",
"date": 20210731
}
4.3.自定義業務監控指標
除了springboot內置健康檢查的相關端點,我們可以通過擴展HealthIndicator接口,增加業務指標監控,比如說
/**
* 自定義健康檢查
*
* @author ThinkPad
* @version 1.0
* @date 2021/7/26 20:43
*/
@Component
public class EduHealthIndicator implements HealthIndicator{
public Health health() {
// return Health.unknown().withDetail("status",666).build();
// return Health.unknown().withDetail("status",666).build();
return Health.up().withDetail("status",666).build();
}
}
效果