亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring Boot Actuator自定義健康檢查教程

瀏覽:34日期:2023-06-30 10:38:06

健康檢查是Spring Boot Actuator中重要端點之一,可以非常容易查看應用運行至狀態。本文在前文的基礎上介紹如何自定義健康檢查。

1. 概述

本節我們簡單說明下依賴及啟用配置,展示缺省健康信息。首先需要引入依賴:

compile('org.springframework.boot:spring-boot-starter-actuator')

現在通過http://localhost:8080/actuator/health端點進行驗證:

{'status':'UP'}

缺省該端點返回應用中很多組件的匯總健康信息,但可以修改屬性配置展示詳細內容:

management: endpoint: health: show-details: always

現在再次訪問返回結果如下:

{ '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; }}

上面結果顯示當前項目啟動的路徑 . ,報警值 為10M ,這些屬性都可以通過配置進行修改。

2. 預定義健康指標

上面Json響應顯示“ping”和“diskSpace”檢查。這些檢查也稱為健康指標,如果應用引用了數據源,Spring會增加db健康指標;同時“diskSpace”是缺省配置。

Spring Boot包括很多預定義的健康指標,下面列出其中一部分:

DataSourceHealthIndicator MongoHealthIndicator Neo4jHealthIndicator CassandraHealthIndicator RedisHealthIndicator CassandraHealthIndicator RabbitHealthIndicator CouchbaseHealthIndicator DiskSpaceHealthIndicator (見上面示例) ElasticsearchHealthIndicator InfluxDbHealthIndicator JmsHealthIndicator MailHealthIndicator SolrHealthIndicator

如果在Spring Boot應用中使用Mongo或Solr等,則Spring Boot會自動增加相應健康指標。

3. 自定義健康指標

Spring Boot提供了一捆預定義健康指標,但并沒有阻止你增加自己的健康指標。一般有兩種自定義類型檢查:

單個健康指標組件和組合健康指標組件。

3.1 自定義單個指標組件

自定義需要實現HealthIndicator接口并重新health()方法,同時增加@Component注解。假設示例應用程序與服務A(啟動)和服務B(關閉)通信。如果任一服務宕機,應用程序將被視為宕機。因此,我們將寫入兩個運行狀況指標。

@Componentpublic 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 Skippedreturn isRunning; }}

@Componentpublic 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 Skippedreturn isRunning; }}

現在,我們看到健康監控響應中增加的指標。ServerA狀態是UP,ServiceB是DOWN,因此整個監控檢測狀態為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 自定義組合健康檢查

前面示例很容易查看各個指標各自的狀態。但有時需要基于幾個指標查看資源的狀態,則需要使用 HealthContributor ,該接口沒有定義方法,僅用于標記。如果一個服務有另外兩個動作組合進行實現,只有兩者同時工作該服務狀態才算正常。最后使用 CompositeHealthContributors組合多個指標:

public class ServiceAHealthIndicator implements HealthIndicator, HealthContributor {...}

下面定義組合健康檢查指標:

@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); }}

現在我們使用serverA和serverB組合新的檢查UserServiceAPI。

4. 總結

本文我們學習了Spring Boot健康指標及相關配置、以及預定義的健康指標,同時介紹了如何自定義健康指標。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 美女久久久久久久久久久 | 成人激情视频在线观看 | 日日夜夜操操操 | 91精品福利在线观看 | 黄色变态视频 | 6080yy 久久 亚洲 日本不卡 | 成年男女免费视频观看性 | 欧美成人午夜做爰视频在线观看 | 美国黄色一级大片 | 国产伦码精品一区二区三区 | 亚洲男人的天堂网站 | 亚洲欧美一区二区三区在线观看 | 日韩一级欧美一级在线观看 | 欧美黄色录像 | 成人欧美一区二区三区视频xxx | 免费摸碰碰视频在线观看 | 亚洲欧洲毛片 | 麻豆久久精品免费看国产 | 亚洲爆乳无码一区二区三区 | 91国自产精品中文字幕亚洲 | 在线视频观看免费视频18 | 亚洲精品aⅴ中文字幕乱码 亚洲精品aaa | 1024国产基地 | 玖玖精品在线视频 | 成人区精品一区二区不卡亚洲 | 国产美女91呻吟求 | 国产在线视频网站 | 97精品国产自在现线免费观看 | 国产爽妇网 | 青青草综合视频 | 国产免费人做爰午夜视频 | 中文字幕久久亚洲一区 | 成人国产精品高清在线观看 | 一级黄色片视频 | 欧美日韩国产超高清免费看片 | 黄色毛片a级 | 亚洲精品国产一区二区三区四区 | 1024视频在线| 免费观看的黄色网址 | 亚洲一区二区三区免费看 | 久久影院一区二区三区 |