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() 方法来实现健康检查。

  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

(完)

如果觉得我的文章对你有帮助,请随意打赏。

Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控的更多相关文章

  1. Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点

    文章目录 1. 继承 AbstractEndpoint 抽象类 2. 创建端点配置类 3. 运行 4. 源代码 Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点. 本 ...

  2. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控

    文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...

  3. Spring Boot 揭秘与实战(一) 快速上手

    文章目录 1. 简介 1.1. 什么是Spring Boot 1.2. 为什么选择Spring Boot 2. 相关知识 2.1. Spring Boot的spring-boot-starter 2. ...

  4. Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置

    Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...

  5. Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块

    文章目录 1. 实战的开端 – Maven搭建 2. 参数的配置 - 属性参数类 3. 真的很简单 - 简单的服务类 4. 自动配置的核心 - 自动配置类 5. spring.factories 不要 ...

  6. Spring Boot 揭秘与实战 源码分析 - 工作原理剖析

    文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...

  7. Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机

    文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...

  8. Spring Boot 揭秘与实战(八) 发布与部署 - 远程调试

    文章目录 1. 依赖 2. 部署 3. 调试 4. 源代码 设置远程调试,可以在正式环境上随时跟踪与调试生产故障. 依赖 在 pom.xml 中增加远程调试依赖. <plugins> &l ...

  9. Spring Boot 揭秘与实战(八) 发布与部署 - 开发热部署

    文章目录 1. spring-boot-devtools 实现热部署 2. Spring Loaded 实现热部署 3. 模板文件热部署 4. 源代码 Spring Boot 支持页面与类文件的热部署 ...

随机推荐

  1. ORACLE SQL 函数 INITCAP()

    INITCAP() 假设c1为一字符串.函数INITCAP()是将每个单词的第一个字母大写,其它字母变为小写返回. 单词由空格,控制字符,标点符号等非字母符号限制. select initcap('h ...

  2. Intersecting Lines

    Intersecting Lines We all know that a pair of distinct points on a plane defines a line and that a p ...

  3. Hadoop---Java-API对HDFS的操作

    Java-API对HDFS的操作 哈哈哈哈,深夜来一波干货哦!!! Java-PAI对hdfs的操作,首先我们建一个maven项目,我主要说,我们可以通过Java代码来对HDFS的具体信息的打印,然后 ...

  4. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  5. 把旧系统迁移到.Net Core 2.0 日记 (16) --Cors跨域访问

    IE浏览器的Intranet局域网设置默认是可以跨域访问的.chrome就不可以. 这里说的跨域是指javascript代码不能跨域, 当然你在后端controller代码里用HttpClient.G ...

  6. prefix super supra sex sept septi out~2

    1★ super   2★ supra 超过,超出   3★ sept 4★ septi   7     5★ sex 6    

  7. Ubuntu Windows双系统重装windows后看不到ubuntu启动引导

    1.下载并安装Easy BCD 2.点击编辑引导菜单,看到只有windows一项 3.点击“添加新条目”,添加引导菜单,选择linux/bsd ,类型选择GRUB 2,然后输入名称,选择Ubuntu所 ...

  8. linux用户管理 用户和用户组信息

    用户管理配置文件 用户信息文件  /etc/passwd 密码文件 /etc/shadow 用户配置文件 /etc/login.defs /etc/default/useradd 新用户信息文件 /e ...

  9. 分布式锁与实现(一)基于Redis实现

    目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个分布式系统都无法同时满足一致性(Consistency).可用性( ...

  10. python全栈开发笔记---基本数据类型--字符串魔法

    字符串: def capitalize(self, *args, **kwargs) test = "aLxs" v = test.capitalize() #capitalize ...