Spring boot——Actuator 健康检查
一、使用方法
在Spring boot應用中,要實現可監控的功能,依賴的是 spring-boot-starter-actuator 這個組件。
它提供了很多監控和管理你的spring boot應用的HTTP或者JMX端點,并且你可以有選擇地開啟和關閉部分功能。
當你的spring boot應用中引入下面的依賴之后,將自動的擁有審計、健康檢查、Metrics監控功能。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
具體的使用方法:
引入上述的依賴jar;
通過下面的配置啟用所有的監控端點,默認情況下,這些端點是禁用的;
management.endpoints.web.exposure.include=*
management.endpoints.jmx.exposure.include=*
“*”號代表啟用所有的監控端點,可以單獨啟用,例如,health,info,metrics等。
通過actuator/+端點名就可以獲取相應的信息。
一般的監控管理端點的配置信息,如下:
management.server.port=10111 management.server.ssl.enabled=false management.server.servlet.context-path=/ management.endpoint.health.show-details=always
management.endpoint.health.show-details的值除了always之外還有when-authorized、never,默認值是never。
二、健康檢查
當我們開啟health的健康端點時,我們能夠查到應用健康信息是一個匯總的信息,訪問http://127.0.0.1:10111/actuator/health時,我們獲取到的信息是{"status":"UP"},status的值還有可能是 DOWN。
要想查看詳細的應用健康信息需要配置management.endpoint.health.show-details 的值為always,配置之后我們再次訪問http://127.0.0.1:10111/actuator/health,獲取的信息如下:
{
"status": "UP",
"details": {
"diskSpace": {
"status": "UP",
"details": {
"total": 250685575168,
"free": 172252426240,
"threshold": 10485760
}
},
"redis": {
"status": "UP",
"details": {
"version": "3.2.11"
}
},
"db": {
"status": "UP",
"details": {
"database": "Oracle",
"hello": "Hello"
}
}
}
}
從上面的應用的詳細健康信息發現,健康信息包含磁盤空間、redis、DB,啟用監控的這個spring boot應用確實是連接了redis和oracle DB,actuator就自動給監控起來了,確實是很方便、很有用。
經過測試發現,details中所有的監控項中的任何一個健康狀態是DOWN,整體應用的健康狀態也是DOWN。
三、健康檢查原理
Spring boot的健康信息都是從ApplicationContext中的各種HealthIndicatorBeans中收集到的,Spring boot框架中包含了大量的HealthIndicators的實現類,當然你也可以實現自己認為的健康狀態。
默認情況下,最終的spring boot應用的狀態是由HealthAggregator匯總而成的,匯總的算法是:
設置狀態碼順序:setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, Status.UP, Status.UNKNOWN)。
過濾掉不能識別的狀態碼。
如果無任何狀態碼,整個spring boot應用的狀態是 UNKNOWN。
將所有收集到的狀態碼按照 1 中的順序排序。
返回有序狀態碼序列中的第一個狀態碼,作為整個spring boot應用的狀態。
源代碼請參見:
org.springframework.boot.actuate.health.OrderedHealthAggregator。
Spring boot框架自帶的HealthIndicators目前包括:
| Name | Description |
|---|---|
CassandraHealthIndicator |
Checks that a Cassandra database is up. |
DiskSpaceHealthIndicator |
Checks for low disk space. |
DataSourceHealthIndicator |
Checks that a connection to DataSource can be obtained. |
ElasticsearchHealthIndicator |
Checks that an Elasticsearch cluster is up. |
InfluxDbHealthIndicator |
Checks that an InfluxDB server is up. |
JmsHealthIndicator |
Checks that a JMS broker is up. |
MailHealthIndicator |
Checks that a mail server is up. |
MongoHealthIndicator |
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將其中任意一個禁用掉。
四、自定義 HealthIndicator 健康檢查
有時候需要提供自定義的健康狀態檢查信息,你可以通過實現HealthIndicator的接口來實現,并將該實現類注冊為spring bean。
你需要實現其中的health()方法,并返回自定義的健康狀態響應信息,該響應信息應該包括一個狀態碼和要展示詳細信息。
例如,下面就是一個接口HealthIndicator的實現類:
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // perform some specific health check
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
}
另外,除了Spring boot定義的幾個狀態類型,我們也可以自定義狀態類型,用來表示一個新的系統狀態。
在這種情況下,你還需要實現接口 HealthAggregator ,或者通過配置 management.health.status.order 來繼續使用HealthAggregator的默認實現。
例如,在你自定義的健康檢查HealthIndicator的實現類中,使用了自定義的狀態類型FATAL,為了配置該狀態類型的嚴重程度,你需要在application的配置文件中添加如下配置:
management.health.status.order=FATAL, DOWN, OUT_OF_SERVICE, UNKNOWN, UP
在做健康檢查時,響應中的HTTP狀態碼反應了整體的健康狀態,(例如,UP 對應 200, 而 OUT_OF_SERVICE 和 DOWN 對應 503)。
同樣,你也需要為自定義的狀態類型設置對應的HTTP狀態碼,例如,下面的配置可以將 FATAL 映射為 503(服務不可用):
management.health.status.http-mapping.FATAL=503
如果你需要更多的控制,你可以定義自己的HealthStatusHttpMapperbean。
下面是內置健康狀態類型對應的HTTP狀態碼列表:
| 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 |
引用:
https://www.jianshu.com/p/1aadc4c85f51
總結
以上是生活随笔為你收集整理的Spring boot——Actuator 健康检查的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [UML]UML 教程
- 下一篇: 驭怎么读(邑怎么读)