色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:23日期: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
相關文章:
主站蜘蛛池模板: 国内精品国语自产拍在线观看55 | 久热色| 国产黄网 | 欠草视频 | 一级毛片不收费 | 欧美一区二区三区久久久人妖 | 成年美女黄网站色视频大全免费 | 久久精品国产半推半就 | 亚洲欧美久久精品一区 | 大视频在线爱爱爱爱 | 欧美ab| 国产欧美日韩在线观看精品 | 欧美自拍另类 | 韩国免费毛片在线看 | 亚洲va中文字幕欧美不卡 | 亚洲综色 | 亚洲视频精品在线 | 在线精品欧美日韩 | 国产在线91精品天天更新 | 久久国产一片免费观看 | 一级a毛片免费观看久久精品 | a理论片 | 欧美日韩国产在线人成dvd | 粉嫩高中生的第一次在线观看 | 免费观看欧美一级毛片 | 91视频国内 | 国产一级一片免费播放视频 | 国产色司机在线视频免费观看 | 久草在线看 | 亚洲成a v人片在线看片 | 又黄又爽视频好爽视频 | 中文字幕在线成人免费看 | 国产乱码精品一区二区三区四川人 | 日韩一级欧美一级一级国产 | 久久久免费视频播放 | 亚洲成a人v大片在线观看 | 久久99久久精品视频 | 国产成人一区二区三区精品久久 | 日本a级片免费观看 | 国产黄色激情视频 | 成人免费久久精品国产片久久影院 |