1. 程式人生 > >Spring Boot 揭秘與實戰(九) 應用監控篇 - HTTP 健康監控

Spring Boot 揭秘與實戰(九) 應用監控篇 - HTTP 健康監控

don 此外 final 監控 blog jms 例如 簡單的 3.0

文章目錄

  1. 1. 內置 HealthIndicator 監控檢測
  2. 2. 自定義 HealthIndicator 監控檢測
  3. 3. 源代碼

Health 信息是從 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring Boot 內置了一些 HealthIndicator。

內置 HealthIndicator 監控檢測

NameDescription
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.
JmsHealthIndicator Checks that a JMS broker is up.
MailHealthIndicator Checks that a mail server is up.
MongoHealthIndicator
Checks that a Mongo database 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.

我們,來看下源代碼清單。

技術分享

可見,Spring Boot 幫忙我們集成了許多比較常見的健康監控,例如 MySQL、 MongoDB、 Redis、 ElasticSearch、 Solr、 RabbitMQ 等。

自定義 HealthIndicator 監控檢測

一般情況下,Spring Boot 提供的健康監控無法滿足我們復雜的業務場景,此時,我們就需要定制自己的 HealthIndicator, 擴展自己的業務監控。

我們,實現 HealthIndicator 接口創建一個簡單的檢測器類。它的作用很簡單,只是進行服務狀態監測。此時,通過重寫 health() 方法來實現健康檢查。

  1. @Component
  2. public class CusStatusHealthIndicator implements HealthIndicator {
  3. @Override
  4. public Health health() {
  5. int errorCode = check();
  6. if (errorCode != 0) {
  7. return Health.down()
  8. .withDetail("status", errorCode)
  9. .withDetail("message", "服務故障")
  10. .build();
  11. }
  12. return Health.up().build();
  13. }
  14. private int check(){
  15. // 對監控對象的檢測操作
  16. return HttpStatus.NOT_FOUND.value();
  17. }
  18. }

我們,來看看打印結果。

  1. {
  2. "status": "DOWN",
  3. "cusStatus": {
  4. "status": 404,
  5. "message": "服務故障"
  6. }
  7. }

此外,我們還可以通過繼承 AbstractHealthIndicator 類,創建一個檢測器類。

  1. @Component
  2. public class CusDiskSpaceHealthIndicator extends AbstractHealthIndicator {
  3. private final FileStore fileStore;
  4. private final long thresholdBytes;
  5. @Autowired
  6. public CusDiskSpaceHealthIndicator(
  7. @Value("${health.filestore.path:/}") String path,
  8. @Value("${health.filestore.threshold.bytes:10485760}") long thresholdBytes)
  9. throws IOException {
  10. fileStore = Files.getFileStore(Paths.get(path));
  11. this.thresholdBytes = thresholdBytes;
  12. }
  13. @Override
  14. protected void doHealthCheck(Health.Builder builder) throws Exception {
  15. long diskFreeInBytes = fileStore.getUnallocatedSpace();
  16. if (diskFreeInBytes >= thresholdBytes) {
  17. builder.up();
  18. } else {
  19. builder.down();
  20. }
  21. long totalSpaceInBytes = fileStore.getTotalSpace();
  22. builder.withDetail("disk.free", diskFreeInBytes);
  23. builder.withDetail("disk.total", totalSpaceInBytes);
  24. }
  25. }

AbstractHealthIndicator 實現 HealthIndicator 接口,並重寫了 health() 方法來實現健康檢查。因此,我們只需要重寫 doHealthCheck 方法即可。

一般情況下,我們不會直接實現 HealthIndicator 接口,而是繼承 AbstractHealthIndicator 抽象類。因為,我們只需要重寫 doHealthCheck 方法,並在這個方法中我們關註於具體的健康檢測的業務邏輯服務。

我們,來看看打印結果。

  1. {
  2. "status": "UP",
  3. "cusDiskSpace": {
  4. "status": "UP",
  5. "disk.free": 79479193600,
  6. "disk.total": 104856547328
  7. }
  8. }

源代碼

相關示例完整代碼: springboot-action

(完)



技術分享
  • 版權聲明:本文由 梁桂釗 發表於 梁桂釗的博客
  • 轉載聲明:自由轉載-非商用-非衍生-保持署名(創意共享3.0許可證),非商業轉載請註明作者及出處,商業轉載請聯系作者本人。
  • 文章標題:Spring Boot 揭秘與實戰(九) 應用監控篇 - HTTP 健康監控
  • 文章鏈接:http://blog.720ui.com/2017/springboot_09_actuator_http_healthindicator/

Spring Boot 揭秘與實戰(九) 應用監控篇 - HTTP 健康監控