Springboot監控之一:SpringBoot四大神器之Actuator之2--springboot健康檢查--temp


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

內置 HealthIndicator 監控檢測

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.
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() 方法來實現健康檢查。

    @Component
    public class CusStatusHealthIndicator implements HealthIndicator {
     
    @Override
    public Health health() {
    int errorCode = check();
    if (errorCode != 0) {
    return Health.down()
    .withDetail("status", errorCode)
    .withDetail("message", "服務故障")
    .build();
    }
    return Health.up().build();
    }
     
    private int check(){
    // 對監控對象的檢測操作
    return HttpStatus.NOT_FOUND.value();
    }
    }

 

我們,來看看打印結果。

    {
    "status": "DOWN",
    "cusStatus": {
    "status": 404,
    "message": "服務故障"
    }
    }

 

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

    @Component
    public class CusDiskSpaceHealthIndicator extends AbstractHealthIndicator {
     
    private final FileStore fileStore;
    private final long thresholdBytes;
     
    @Autowired
    public CusDiskSpaceHealthIndicator(
    @Value("${health.filestore.path:/}") String path,
    @Value("${health.filestore.threshold.bytes:10485760}") long thresholdBytes)
    throws IOException {
    fileStore = Files.getFileStore(Paths.get(path));
    this.thresholdBytes = thresholdBytes;
    }
     
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
    long diskFreeInBytes = fileStore.getUnallocatedSpace();
    if (diskFreeInBytes >= thresholdBytes) {
    builder.up();
    } else {
    builder.down();
    }
     
    long totalSpaceInBytes = fileStore.getTotalSpace();
    builder.withDetail("disk.free", diskFreeInBytes);
    builder.withDetail("disk.total", totalSpaceInBytes);
    }
    }

 

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

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

我們,來看看打印結果。

    {
    "status": "UP",
    "cusDiskSpace": {
    "status": "UP",
    "disk.free": 79479193600,
    "disk.total": 104856547328
    }
    }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM