Springboot监控之一:SpringBoot四大神器之Actuator之2--springboot健康检查
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
- }
- }
源代码
相关示例完整代码: springboot-action
Springboot监控之一:SpringBoot四大神器之Actuator之2--springboot健康检查的更多相关文章
- Springboot监控之一:SpringBoot四大神器之Actuator之2--spring boot健康检查对Redis的连接检查的调整
因为项目里面用到了redis集群,但并不是用spring boot的配置方式,启动后项目健康检查老是检查redis的时候状态为down,导致注册到eureka后项目状态也是down.问下能不能设置sp ...
- Spring Boot]SpringBoot四大神器之Actuator
论文转载自博客: https://blog.csdn.net/Dreamhai/article/details/81077903 https://bigjar.github.io/2018/08/19 ...
- Springboot监控之一:SpringBoot四大神器之Actuator之3-springBoot的监控和管理--指标说明
Spring Boot包含很多其他的特性,它们可以帮你监控和管理发布到生产环境的应用.你可以选择使用HTTP端点,JMX或远程shell(SSH或Telnet)来管理和监控应用.审计(Auditing ...
- Springboot监控之一:SpringBoot四大神器之Actuator
介绍 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供 ...
- Springboot监控之一:SpringBoot四大神器之Actuator之2--覆盖修改spring cloud的默认的consul健康检查规则
微服务网关是socket长连接与支付公司对接,该网关需要提供http接口给内部系统调用,当socket没有建立连接时(网关服务的高可用是haProxy搭建的,有些服务的socket可能未连上支付公司) ...
- SpringBoot四大神器之Actuator
介绍 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供 ...
- SpringBoot四大神器之Starter
SpringBoot的starter主要用来简化依赖用的.本文主要分两部分,一部分是列出一些starter的依赖,另一部分是教你自己写一个starter. 部分starters的依赖 Starter( ...
- SpringBoot四大神器之auto-configuration
SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...
- SpringBoot 2.x (15):Actuator监控
Actuator监控:SpringBoot自带的,对生成环境进行监控的系统 使用:既然是监控,那就不能监控一个空项目 这里我使用SpringBoot整合MyBatis的Demo: https://ww ...
随机推荐
- CF910B
题解: dp f[i][j]表示i根a,j根b要多少 然后随便转移一下 代码: #include<bits/stdc++.h> using namespace std; ][],n,a,b ...
- WebLogic和Tomcat
J2ee开发主要是浏览器和服务器进行交互的一种结构.逻辑都是在后台进行处理,然后再把结果传输回给浏览器.可以看出服务器在这种架构是非常重要的. 这几天接触到两种Java的web服务器,做项目用的Tom ...
- qml 知识积累
本文仅仅只是记录,怕日后遗忘. ListView相关应用 https://www.cnblogs.com/SaveDictator/p/8192391.html ListView 清空操作 https ...
- (转)Mac os x 下配置Intellij IDEA + Tomcat 出现权限问题的解决办法
出现的错误提示如下: 下午9:11:27 All files are up-to-date下午9:11:27 All files are up-to-date下午9:11:27 Error runni ...
- Django中关于事务的代码编写
Django中对于数据库的事务,默认每执行一句数据库操作,便会自动提交. 在Django中可以通过django.db.transaction模块提供的atomic来定义一个事务,atomic提供两种用 ...
- SQL Server(MSSQLSERVER)启动失败,提示“请求失败或服务未及时响应
1.SQL Server(MSSQLSERVER)启动失败,提示“请求失败或服务未及时响应. --------------------------- SQL Server 配置管理器 -------- ...
- (转)MapReduce Design Patterns(chapter 3 (part 2))(六)
Top Ten Pattern Description Top ten模式跟前面的有很大的不同,跟输入数据大小无关,最终得到的记录数量是确定的.而在通用filtering中,输出的规模取决于输入数据. ...
- 我也说说Emacs吧(2) - Emacs其实就是函数的组合
Emacs本质上是函数的组合 从帮助上看emacs有何不同 Vim和Sublime Text等编辑器,本质上是一个编辑器. 比如我们看看vim的帮助,是这个风格的,比如我要看i命令的帮助: <i ...
- keras 报错 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(5001, 128), dtype=float32)'
在服务器上训练并保存模型,复制到本地之后load_model()报错: ValueError: Tensor conversion requested dtype int32 for Tensor w ...
- wireshark的提示
内容:12个wrieshark的提示 1.[Packet size limited during capture] 在捕获数据包大小有限,即包没有抓全 2.[TCP previous segment ...