zoukankan      html  css  js  c++  java
  • Spring Boot 揭秘与实战(九) 应用监控篇

    文章目录

    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.  
    4. @Override
    5. public Health health() {
    6. int errorCode = check();
    7. if (errorCode != 0) {
    8. return Health.down()
    9. .withDetail("status", errorCode)
    10. .withDetail("message", "服务故障")
    11. .build();
    12. }
    13. return Health.up().build();
    14. }
    15.  
    16. private int check(){
    17. // 对监控对象的检测操作
    18. return HttpStatus.NOT_FOUND.value();
    19. }
    20. }

    我们,来看看打印结果。

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

    此外,我们还可以通过继承 AbstractHealthIndicator 类,创建一个检测器类。

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

    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

    (完)

    微信公众号
  • 相关阅读:
    linux系统/var/log目录下的信息详解
    SQL 显示表名显示列名
    P2P平台介绍
    outlook署名最后一行没换行
    CSS3下的渐变文字效果实现
    SSH(poderosa)を使って、さくらのMySQLサーバーに接続する方法
    内网IP外网IP的关联及访问互联网原理
    自己吃的哑巴亏,怎么也要吞下去
    解决Ehcache缓存警告问题
    管理的艺术
  • 原文地址:https://www.cnblogs.com/cnblog-long/p/7245952.html
Copyright © 2011-2022 走看看