Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控
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
(完)
如果觉得我的文章对你有帮助,请随意打赏。

- 版权声明:本文由 梁桂钊 发表于 梁桂钊的博客
- 转载声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证),非商业转载请注明作者及出处,商业转载请联系作者本人。
- 文章标题:Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控
- 文章链接:http://blog.720ui.com/2017/springboot_09_actuator_http_healthindicator/
Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控的更多相关文章
- Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点
文章目录 1. 继承 AbstractEndpoint 抽象类 2. 创建端点配置类 3. 运行 4. 源代码 Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点. 本 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控
文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...
- Spring Boot 揭秘与实战(一) 快速上手
文章目录 1. 简介 1.1. 什么是Spring Boot 1.2. 为什么选择Spring Boot 2. 相关知识 2.1. Spring Boot的spring-boot-starter 2. ...
- Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置
Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...
- Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
文章目录 1. 实战的开端 – Maven搭建 2. 参数的配置 - 属性参数类 3. 真的很简单 - 简单的服务类 4. 自动配置的核心 - 自动配置类 5. spring.factories 不要 ...
- Spring Boot 揭秘与实战 源码分析 - 工作原理剖析
文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...
- Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机
文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...
- Spring Boot 揭秘与实战(八) 发布与部署 - 远程调试
文章目录 1. 依赖 2. 部署 3. 调试 4. 源代码 设置远程调试,可以在正式环境上随时跟踪与调试生产故障. 依赖 在 pom.xml 中增加远程调试依赖. <plugins> &l ...
- Spring Boot 揭秘与实战(八) 发布与部署 - 开发热部署
文章目录 1. spring-boot-devtools 实现热部署 2. Spring Loaded 实现热部署 3. 模板文件热部署 4. 源代码 Spring Boot 支持页面与类文件的热部署 ...
随机推荐
- Leetcode 120
class Solution { public: int minimumTotal(vector<vector<int>>& triangle) { ) ][]; tr ...
- 70. Climbing Stairs爬楼梯
网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...
- Mac下配置/使用GitHub
一.配置1. 查看是否有id_rsa.pub文件:cd ~/.ssh 2. 如果没有id_rsa.pub文件,执行如下命令来生成id_rsa.pub文件: ssh-keygen -t rsa -C & ...
- web服务器/应用服务器/http服务器/中间件
web服务器:只处理html静态页面不处理动态页面,如apache/nginx/iis等. 应用服务器:能处理html静态页面也能处理动态页面,如tomcat/weblogic/websphere/j ...
- 把旧系统迁移到.Net Core 2.0 日记(8) - EASYUI datagrid+ Dapper+ 导出Excel
迁移也没太大变化,有一个, 之前的Request.QueryString 是返回NameValueCollection, 现在则是返回整个字符串. 你要改成Request.Query[“key”] 直 ...
- 把旧系统迁移到.Net Core 2.0 日记(4) - 使用EF+Mysql
因为Mac 不能装SqlServer, 所以把数据库迁移到MySql,然后EntityFramework要改成Pomelo.EntityFrameworkCore.MySql 数据库迁移时,nvarc ...
- GCC 命令行详解 -L 指定库的路径 -l 指定需连接的库名 zhuan
1.gcc包含的c/c++编译器gcc,cc,c++,g++,gcc和cc是一样的,c++和g++是一样的,(没有看太明白前面这半句是什么意思:))一般c程序就用gcc编译,c++程序就用g++编译 ...
- git merge branch
git branch look at your branches git branch newbranch git checkout newbrach do something git check ...
- 两点(DFS)
福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”.基础级别的时候是在一个n×m单元上玩的.像这样: 每一个单元有包含一个有色点.我们将用不同的大写字母来表示不同的颜色. 这个游戏的关键是要找出一个包含 ...
- 【Junit_Ant】使用Eclipse自带的Junit和Ant,生成测试报告
使用Eclipse自带的Junit和Ant,生成测试报告 1.点击要测试的工程,右击,选择Export 2.在弹出的页面里,点击General,选择Ant Buildfiles,点击Next 3.在下 ...