title: SpringBoot2.X性能監(jiān)控Actuator
date: 2020-03-20
author: maxzhao
tags:
- SpringBoot
- Actuator
categories:
- SpringBoot
一、前言
SpringBoot Actuator 服務(wù)監(jiān)控與管理**
其中包含了很多的服務(wù),比如我們常用的amqp
、JVM
、cache
等等,下面是actuator
包下的目錄
amqp,audit,beans,cache,cassandra,context,couchbase,elasticsearch,endpoint,env,flyway,health,influx,info,integration,jdbc,jms,ldap,liquibase,logging,mail,management,metrics,mongo,neo4j,redis,scheduling,security,session,solr,system,http,web
是不是感覺挺全面的。
二、服務(wù)監(jiān)控與管理
Maven 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
主要配置
Spring Boot2.x中,默認(rèn)只開放了info、health
兩個端點(diǎn),開放其他端點(diǎn)需要配置
# 開啟所有端點(diǎn)
management:
endpoints: # 這里是 endpoints
web:
# 默認(rèn)路徑
base-path: /actuator
exposure:
# Endpoint IDs that should be included or '*' for all.
include: '*'
# 顯示詳細(xì)的 health 信息
jmx:
# Whether unique runtime object names should be ensured.
domain: org.springframework.boot
exposure:
# Endpoint IDs that should be included or '*' for all.
include: '*'
# 顯示詳細(xì)的 health 信息
endpoint: # 這里是 endpoint
health:
show-details: always
# 打開 shutdown 端點(diǎn),通過 POST 訪問該端點(diǎn)可以關(guān)閉應(yīng)用
shutdown:
enabled: true
監(jiān)控狀態(tài)
啟動之后訪問 http://localhost:8062/boot/actuator/health 就可以看到對應(yīng)的項(xiàng)目監(jiān)控狀態(tài)。
訪問 http://localhost:8062/boot/actuator 可以查看有那些監(jiān)控。
健康指標(biāo) HealthIndicators 由 Spring Boot 自動配置,因此這里顯示監(jiān)控信息是由項(xiàng)目所使用的技術(shù)棧而決定的:
名稱 | 描述 |
---|---|
CassandraHealthIndicator | 檢查 Cassandra 數(shù)據(jù)庫是否啟動。 |
DiskSpaceHealthIndicator | 檢查磁盤空間不足。 |
DataSourceHealthIndicator | 檢查是否可以獲得連接 DataSource。 |
ElasticsearchHealthIndicator | 檢查 Elasticsearch 集群是否啟動。 |
InfluxDbHealthIndicator | 檢查 InfluxDB 服務(wù)器是否啟動。 |
JmsHealthIndicator | 檢查 JMS 代理是否啟動。 |
MailHealthIndicator | 檢查郵件服務(wù)器是否啟動。 |
MongoHealthIndicator | 檢查 Mongo 數(shù)據(jù)庫是否啟動。 |
Neo4jHealthIndicator | 檢查 Neo4j 服務(wù)器是否啟動。 |
RabbitHealthIndicator | 檢查 Rabbit 服務(wù)器是否啟動。 |
RedisHealthIndicator | 檢查 Redis 服務(wù)器是否啟動。 |
SolrHealthIndicator | 檢查 Solr 服務(wù)器是否已啟動。 |
常用端點(diǎn)
查看常用接口
http://localhost:8062/boot/actuator/
env 端點(diǎn),應(yīng)用獲取環(huán)境信息,包括:環(huán)境變量、JVM屬性、應(yīng)用的配置配置、命令行中的參數(shù)等等。
localhost:8080/actuator/env
mapping 端點(diǎn),url 與 控制器映射關(guān)系信息
localhost:8080/actuator/info
metrics 端點(diǎn),引用度量指標(biāo)端點(diǎn),提供引用再運(yùn)行時的信息,如內(nèi)存使用情況、HTTP請求統(tǒng)計(jì)、外部資源指標(biāo)等
查看所有度量指標(biāo) localhost:8080/actuator/metrics
查看度量指標(biāo)詳細(xì)信息 localhost:8080/actuator/metrics/jvm.gc.pause
loggers 端點(diǎn),查看可配置 loggers 的列表及相關(guān)的等級信息
localhost:8080/actuator/loggers
查看特定的 logger 詳細(xì)信息localhost:8080/actuator/loggers/{name}
健康檢查
health 端點(diǎn)用于暴露程序運(yùn)行的健康狀態(tài),暴露的信息的詳細(xì)程度由 management.endpoint.health.show-details 來控制,它具有以下三個可選值:
名稱 | 描述 |
---|---|
never | 細(xì)節(jié)永遠(yuǎn)不會顯示。 |
when-authorized | 詳細(xì)信息僅向授權(quán)用戶顯示。授權(quán)角色可以使用配置 management.endpoint.health.roles。 |
always | 詳細(xì)信息顯示給所有用戶。 |
在 org.springframework.boot.actuate.health.ShowDetails
中有詳細(xì)說明。
端點(diǎn)列表
-
info
顯示應(yīng)用的基本信息 -
health
顯示應(yīng)用的健康狀態(tài) -
metrics
顯示應(yīng)用多樣的度量信息 -
loggers
顯示和修改配置的loggers -
logfile
返回log file中的內(nèi)容(如果logging.file或者logging.path被設(shè)置) -
httptrace
顯示HTTP足跡,最近100個HTTP request/repsponse -
env
顯示當(dāng)前的環(huán)境特性 -
flyway
顯示數(shù)據(jù)庫遷移路徑的詳細(xì)信息 -
liquidbase
顯示Liquibase 數(shù)據(jù)庫遷移的纖細(xì)信息 -
shutdown
讓你逐步關(guān)閉應(yīng)用 -
mappings
顯示所有的@RequestMapping路徑 -
scheduledtasks
顯示應(yīng)用中的調(diào)度任務(wù) -
threaddump
執(zhí)行一個線程dump -
heapdump
返回一個GZip壓縮的JVM堆dump
三、自定義健康檢查
在啟動類中加入
@Bean
HealthIndicator customHealthIndicator() {
return () -> Health.status("DOWN")
.withDetail("error code", "某健康專項(xiàng)檢查失敗").build();
}
@Bean
HealthIndicator customUpHealthIndicator() {
return () -> Health.up().withDetail("success code", "自定義檢查一切正常 UP").build();
}
@Bean
HealthIndicator customDownHealthIndicator() {
return () -> Health.up().withDetail("success code", "自定義檢查一切正常 DOWN ").build();
}
訪問 http://localhost:8062/boot/actuator/health 的結(jié)果為:
這里我開啟了redis
,數(shù)據(jù)庫為mysql
{
"status": "DOWN",
"details": {
"custom": {
"status": "FATAL",
"details": {
"error code": "某健康專項(xiàng)檢查失敗"
}
},
"customUp": {
"status": "UP",
"details": {
"success code": "自定義檢查一切正常 UP"
}
},
"customDown": {
"status": "DOWN",
"details": {
"success code": "自定義檢查一切正常 DOWN "
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": "471182741504",
"free": "375580655616",
"threshold": "10485760"
}
},
"db": {
"status": "UP",
"details": {
"database": "MySQL",
"hello": "1"
}
},
"redis": {
"status": "UP",
"details": {
"version": "5.0.8"
}
}
}
}
當(dāng)前details
中有一個檢查status
為DOWN
時,Health
檢查的status
就為DOWN
,否則為UP
。
如果把第一個FATAL
改為DOWN
,Health
檢查結(jié)果同樣為DOWN
。
下表顯示了內(nèi)置狀態(tài)的默認(rèn)映射:
Status | Mapping |
---|---|
DOWN | SERVICE_UNAVAILABLE (503) |
OUT_OF_SERVICE | SERVICE_UNAVAILABLE (503) |
UP | No mapping by default, so http status is 200 |
UNKNOWN | No mapping by default, so http status is 200 |
四、自定義端點(diǎn)
Spring Boot 支持使用 @Endpoint
來自定義端點(diǎn)暴露信息。
@Endpoint(id = "customEndPoint")
@Component
public class CustomEndPoint {
@ReadOperation
public Map<String, Object> getInfo() {
Map<String, Object> dataMap = new LinkedHashMap<>();
dataMap.put("自定義信息", "custom endpoint ");
return dataMap;
}
}
請求 http://localhost:8062/boot/actuator/customEndPoint 的結(jié)果為
{
"自定義信息": "custom endpoint "
}
可用的方法注解由 HTTP 操作所決定:
operation | HTTP 方法 |
---|---|
@ReadOperation | GET |
@WriteOperation | POST |
@DeleteOperation | DELETE |