Spring Boot Actuator自定義健康檢查教程
健康檢查是Spring Boot Actuator中重要端點(diǎn)之一,可以非常容易查看應(yīng)用運(yùn)行至狀態(tài)。本文在前文的基礎(chǔ)上介紹如何自定義健康檢查。
1. 概述
本節(jié)我們簡(jiǎn)單說(shuō)明下依賴及啟用配置,展示缺省健康信息。首先需要引入依賴:
compile("org.springframework.boot:spring-boot-starter-actuator")
現(xiàn)在通過(guò)http://localhost:8080/actuator/health端點(diǎn)進(jìn)行驗(yàn)證:
{"status":"UP"}
缺省該端點(diǎn)返回應(yīng)用中很多組件的匯總健康信息,但可以修改屬性配置展示詳細(xì)內(nèi)容:
management:
endpoint:
health:
show-details: always
現(xiàn)在再次訪問(wèn)返回結(jié)果如下:
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 214748360704,
"free": 112483500032,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
查看DiskSpaceHealthIndicatorProperties文件的源碼:
@ConfigurationProperties(prefix = "management.health.diskspace")
public class DiskSpaceHealthIndicatorProperties {
/**
* Path used to compute the available disk space.
*/
private File path = new File(".");
/**
* Minimum disk space that should be available.
*/
private DataSize threshold = DataSize.ofMegabytes(10);
public File getPath() {
return this.path;
}
public void setPath(File path) {
this.path = path;
}
public DataSize getThreshold() {
return this.threshold;
}
public void setThreshold(DataSize threshold) {
Assert.isTrue(!threshold.isNegative(), "threshold must be greater than or equal to 0");
this.threshold = threshold;
}
}
上面結(jié)果顯示當(dāng)前項(xiàng)目啟動(dòng)的路徑 . ,報(bào)警值 為10M ,這些屬性都可以通過(guò)配置進(jìn)行修改。
2. 預(yù)定義健康指標(biāo)
上面Json響應(yīng)顯示“ping”和“diskSpace”檢查。這些檢查也稱為健康指標(biāo),如果應(yīng)用引用了數(shù)據(jù)源,Spring會(huì)增加db健康指標(biāo);同時(shí)“diskSpace”是缺省配置。
Spring Boot包括很多預(yù)定義的健康指標(biāo),下面列出其中一部分:
DataSourceHealthIndicatorMongoHealthIndicatorNeo4jHealthIndicatorCassandraHealthIndicatorRedisHealthIndicatorCassandraHealthIndicatorRabbitHealthIndicatorCouchbaseHealthIndicatorDiskSpaceHealthIndicator(見(jiàn)上面示例)ElasticsearchHealthIndicatorInfluxDbHealthIndicatorJmsHealthIndicatorMailHealthIndicatorSolrHealthIndicator
如果在Spring Boot應(yīng)用中使用Mongo或Solr等,則Spring Boot會(huì)自動(dòng)增加相應(yīng)健康指標(biāo)。
3. 自定義健康指標(biāo)
Spring Boot提供了一捆預(yù)定義健康指標(biāo),但并沒(méi)有阻止你增加自己的健康指標(biāo)。一般有兩種自定義類型檢查:
單個(gè)健康指標(biāo)組件和組合健康指標(biāo)組件。
3.1 自定義單個(gè)指標(biāo)組件
自定義需要實(shí)現(xiàn)HealthIndicator接口并重新health()方法,同時(shí)增加@Component注解。假設(shè)示例應(yīng)用程序與服務(wù)A(啟動(dòng))和服務(wù)B(關(guān)閉)通信。如果任一服務(wù)宕機(jī),應(yīng)用程序?qū)⒈灰暈殄礄C(jī)。因此,我們將寫(xiě)入兩個(gè)運(yùn)行狀況指標(biāo)。
@Component
public class ServiceAHealthIndicator implements HealthIndicator {
private final String message_key = "Service A";
@Override
public Health health() {
if (!isRunningServiceA()) {
return Health.down().withDetail(message_key, "Not Available").build();
}
return Health.up().withDetail(message_key, "Available").build();
}
private Boolean isRunningServiceA() {
Boolean isRunning = true;
// Logic Skipped
return isRunning;
}
}
@Component
public class ServiceBHealthIndicator implements HealthIndicator {
private final String message_key = "Service B";
@Override
public Health health() {
if (!isRunningServiceB()) {
return Health.down().withDetail(message_key, "Not Available").build();
}
return Health.up().withDetail(message_key, "Available").build();
}
private Boolean isRunningServiceB() {
Boolean isRunning = false;
// Logic Skipped
return isRunning;
}
}
現(xiàn)在,我們看到健康監(jiān)控響應(yīng)中增加的指標(biāo)。ServerA狀態(tài)是UP,ServiceB是DOWN,因此整個(gè)監(jiān)控檢測(cè)狀態(tài)為DOWN.
{
"status": "DOWN",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 214748360704,
"free": 112483229696,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
},
"serviceA": {
"status": "UP",
"details": {
"Service A": "Available"
}
},
"serviceB": {
"status": "DOWN",
"details": {
"Service B": "Not Available"
}
}
}
}
3.2 自定義組合健康檢查
前面示例很容易查看各個(gè)指標(biāo)各自的狀態(tài)。但有時(shí)需要基于幾個(gè)指標(biāo)查看資源的狀態(tài),則需要使用 HealthContributor ,該接口沒(méi)有定義方法,僅用于標(biāo)記。如果一個(gè)服務(wù)有另外兩個(gè)動(dòng)作組合進(jìn)行實(shí)現(xiàn),只有兩者同時(shí)工作該服務(wù)狀態(tài)才算正常。最后使用 CompositeHealthContributors組合多個(gè)指標(biāo):
public class ServiceAHealthIndicator
implements HealthIndicator, HealthContributor {
...
}
下面定義組合健康檢查指標(biāo):
@Component("UserServiceAPI")
public class UserServiceAPIHealthContributor
implements CompositeHealthContributor {
private Map<String, HealthContributor>
contributors = new LinkedHashMap<>();
@Autowired
public UserServiceAPIHealthContributor(
ServiceAHealthIndicator serviceAHealthIndicator, ServiceBHealthIndicator serviceBHealthIndicator) {
contributors.put("serverA", serviceAHealthIndicator);
contributors.put("serverB", serviceBHealthIndicator);
}
/**
* return list of health contributors
*/
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
return contributors.entrySet().stream()
.map((entry) -> NamedContributor.of(entry.getKey(), entry.getValue())).iterator();
}
@Override
public HealthContributor getContributor(String name) {
return contributors.get(name);
}
}
現(xiàn)在我們使用serverA和serverB組合新的檢查UserServiceAPI。
4. 總結(jié)
本文我們學(xué)習(xí)了Spring Boot健康指標(biāo)及相關(guān)配置、以及預(yù)定義的健康指標(biāo),同時(shí)介紹了如何自定義健康指標(biāo)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 通過(guò) 二三法 巧解前端數(shù)據(jù)顯示
實(shí)踐來(lái)源于理論,做開(kāi)發(fā)前肯定要先了解相關(guān)的規(guī)則和原理,看到標(biāo)題或許你會(huì)好奇什么是二三法。本篇文章帶你深入了解,需要的朋友可以參考下2021-10-10
java 實(shí)現(xiàn)下壓棧的操作(能動(dòng)態(tài)調(diào)整數(shù)組大小)
這篇文章主要介紹了java 實(shí)現(xiàn)下壓棧的操作(能動(dòng)態(tài)調(diào)整數(shù)組大小),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
springboot+VUE前后端分離實(shí)現(xiàn)疫情防疫平臺(tái)JAVA
本文主要使用了Java、springmvc、VUE、node.js、mybatis、mysql、tomcat、jquery、layui、bootstarp、JavaScript、html、css、jsp、log4j等一些常見(jiàn)的基本技術(shù),實(shí)現(xiàn)一個(gè)疫情防疫小平臺(tái)2021-08-08
Arrays.sort如何實(shí)現(xiàn)降序排序
這篇文章主要介紹了Arrays.sort如何實(shí)現(xiàn)降序排序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
springboot實(shí)現(xiàn)添加郵件發(fā)送及壓縮功能
這篇文章主要介紹了springboot實(shí)現(xiàn)添加郵件發(fā)送及壓縮功能 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
Java?properties?和?yml?的區(qū)別解析
properties和yml都是Spring?Boot支持的兩種配置文件,它們可以看做Spring?Boot在不同時(shí)期的兩種“產(chǎn)品”,這篇文章主要介紹了Java?properties?和?yml?的區(qū)別,需要的朋友可以參考下2023-02-02

