基于 spring-boot-starter-actuator health节点检查

 

• 前提条件:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

• 健康节点http请求地址:

• 健康状态响应数据:

{
     "description": "Composite Discovery Client",
     "status": "UP",
     "discoveryComposite": {
         "description": "Composite Discovery Client",
         "status": "UP",
         "discoveryClient": {
             "description": "Composite Discovery Client",
             "status": "UP",
             "services": [
                 "fileservice",
                 "dj-atliwen-edi-mqconsumption"
             ]
         },
         "eureka": {
             "description": "Eureka discovery client has not yet successfully connected to a Eureka server",
             "status": "UP",
             "applications": {
                 "SPRING-CLOUD-ZIPKIN": 1,
                 "DJ-WANGRUI-EDI-GLOBAL": 1
             }
         }
     },
     "diskSpace": {
         "status": "UP",
         "total": 285775753216,
         "free": 173917204480,
         "threshold": 10485760
     },
     "rabbit": {
         "status": "UP",
         "version": "3.7.7"
     },
     "db": {
         "status": "UP",
         "database": "MySQL",
         "hello": 1
     },
     "binders": {
         "status": "UP",
         "rabbit": {
             "status": "UP",
             "binderHealthIndicator": {
                 "status": "UP",
                 "version": "3.7.7"
             }
         }
     },
     "configServer": {
         "status": "UP",
         "propertySources": [
             "configClient",
             "http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
         ]
     },
     "hystrix": {
         "status": "UP"
     }
 }

• 异常状态响应数据:

{
     "status": "DOWN",
     "discoveryComposite": {
         "description": "Composite Discovery Client",
         "status": "UP",
         "discoveryClient": {
             "description": "Composite Discovery Client",
             "status": "UP",
             "services": [
                 "fileservice",
                 "security-wo-remote"
             ]
         },
         "eureka": {
             "description": "Eureka discovery client has not yet successfully connected to a Eureka server",
             "status": "UP",
             "applications": {
                 "SPRING-CLOUD-ZIPKIN": 1,
                 "DJ-WANGRUI-EDI-GLOBAL": 1
             }
         }
     },
     "diskSpace": {
         "status": "UP",
         "total": 285775753216,
         "free": 173917204480,
         "threshold": 10485760
     },
     "rabbit": {
         "status": "UP",
         "version": "3.7.7"
     },
     "db": {
         "status": "DOWN",
         "error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up."
     },
     "binders": {
         "status": "UP",
         "rabbit": {
             "status": "UP",
             "binderHealthIndicator": {
                 "status": "UP",
                 "version": "3.7.7"
             }
         }
     },
     "configServer": {
         "status": "UP",
         "propertySources": [
             "configClient",
             "http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
         ]
     },
     "hystrix": {
         "status": "UP"
     }
 }

• 异常恢复之后响应数据:

{
     "description": "Composite Discovery Client",
     "status": "UP",
     "discoveryComposite": {
         "description": "Composite Discovery Client",
         "status": "UP",
         "discoveryClient": {
             "description": "Composite Discovery Client",
             "status": "UP",
             "services": [
                 "fileservice",
                 "security-wo-remote"
             ]
         },
         "eureka": {
             "description": "Eureka discovery client has not yet successfully connected to a Eureka server",
             "status": "UP",
             "applications": {
                 "SPRING-CLOUD-ZIPKIN": 1,
                 "DJ-WANGRUI-EDI-GLOBAL": 1
             }
         }
     },
     "diskSpace": {
         "status": "UP",
         "total": 285775753216,
         "free": 173917155328,
         "threshold": 10485760
     },
     "rabbit": {
         "status": "UP",
         "version": "3.7.7"
     },
     "db": {
         "status": "UP",
         "database": "MySQL",
         "hello": 1
     },
     "binders": {
         "status": "UP",
         "rabbit": {
             "status": "UP",
             "binderHealthIndicator": {
                 "status": "UP",
                 "version": "3.7.7"
             }
         }
     },
     "configServer": {
         "status": "UP",
         "propertySources": [
             "configClient",
             "http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
         ]
     },
     "hystrix": {
         "status": "UP"
     }
 }

• 自定义健康节点:异常状态

{
     "status": "DOWN",
     "test": {
         "status": "DOWN",
         "Error Code": 1
     },
     "discoveryComposite": {
         "description": "Composite Discovery Client",
         "status": "UP",
         "discoveryClient": {
             "description": "Composite Discovery Client",
             "status": "UP",
             "services": [
                 "fileservice",
                 "dj-atliwen-edi-mqconsumption"
             ]
         },
         "eureka": {
             "description": "Eureka discovery client has not yet successfully connected to a Eureka server",
             "status": "UP",
             "applications": {
                 "SPRING-CLOUD-ZIPKIN": 1,
                 "SPRING-CLOUD-DASHBOARD": 1
             }
         }
     },
     "diskSpace": {
         "status": "UP",
         "total": 285775753216,
         "free": 173916606464,
         "threshold": 10485760
     },
     "rabbit": {
         "status": "UP",
         "version": "3.7.7"
     },
     "db": {
         "status": "UP",
         "database": "MySQL",
         "hello": 1
     },
     "binders": {
         "status": "UP",
         "rabbit": {
             "status": "UP",
             "binderHealthIndicator": {
                 "status": "UP",
                 "version": "3.7.7"
             }
         }
     },
     "configServer": {
         "status": "UP",
         "propertySources": [
             "configClient",
             "http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
         ]
     },
     "hystrix": {
         "status": "UP"
     }
 }

• 自定义健康监控节点  :正常状态

{
     "status": "UP",
     "test": {
         "status": "UP"
     },
     "discoveryComposite": {
         "description": "Composite Discovery Client",
         "status": "UP",
         "discoveryClient": {
             "description": "Composite Discovery Client",
             "status": "UP",
             "services": [
                 "fileservice",
                 "dj-atliwen-edi-mqconsumption"
             ]
         },
         "eureka": {
             "description": "Eureka discovery client has not yet successfully connected to a Eureka server",
             "status": "UP",
             "applications": {
                 "SPRING-CLOUD-ZIPKIN": 1,
                 "DJ-WANGRUI-EDI-GLOBAL": 1
             }
         }
     },
     "diskSpace": {
         "status": "UP",
         "total": 285775753216,
         "free": 173916557312,
         "threshold": 10485760
     },
     "rabbit": {
         "status": "UP",
         "version": "3.7.7"
     },
     "db": {
         "status": "UP",
         "database": "MySQL",
         "hello": 1
     },
     "binders": {
         "status": "UP",
         "rabbit": {
             "status": "UP",
             "binderHealthIndicator": {
                 "status": "UP",
                 "version": "3.7.7"
             }
         }
     },
     "configServer": {
         "status": "UP",
         "propertySources": [
             "configClient",
             "http://10.10.7.60:20080/zygfengyuwuzu/config-dev.git/application.yml"
         ]
     },
     "hystrix": {
         "status": "UP"
     }
 }

 

• Spring boot 启动后执行健康检查节点

/**
  * @author 李文
  * @create 2019-03-14 16:22
  **/
 @Slf4j
 @Component
 public class HealthApplication implements ApplicationRunner
 {
     @Autowired
     ConfigurableApplicationContext applicationContext;
     @Override
     public void run(ApplicationArguments applicationArguments) {

Map<String, HealthIndicator> handcars = applicationContext.getBeansOfType(HealthIndicator.class);
         boolean b = true;
         for (Map.Entry<String, HealthIndicator> entry : handcars.entrySet()) {
             log.error("  entry.getValue()  " + entry.getValue().getClass());
             Health health = entry.getValue().health();
             if (!health.getStatus().equals(Status.UP)) {
                 log.error(" 启动健康检查异常 {} ", health.toString());
                 b = false;
             }
         }
         if (!b) {
             applicationContext.close();
         }
     }
 }

• 自定义健康监控节点

/**
  * 自定义健康监控节点
  *
  * @author 李文
  * @create 2019-03-14 14:14
  **/
 @Component
 public class TestHealthIndicator implements HealthIndicator
 {
     @Override
     public Health health() {

int errorCode = check();
         if (errorCode != 0) {
             return Health.down().withDetail("Error Code", errorCode).build();
         }
         return Health.up().build();
     }

private int check() {
         return 0;
     }
 }

补充信息:

默认状态节点名称为类名称 如 TestHealthIndicator 为 test 节点

非spring boot 项目也可以集成。

HealthIndicator组件只带的检查项

CassandraHealthIndicator   检查Cassandra数据库状况   (介于关系数据库和非关系数据库之间的开源产品,是最像关系数据库的NOSQL数据库)

 

DiskSpaceHealthIndicator  低磁盘空间检查  (10 * 1024 * 1024  单位是bytes)

 

DataSourceHealthIndicator 检查是否能从DataSource获取连接 (检查数据库情况 只支持spring-boot-data-jpa)

 

关键代码:
        private JdbcTemplate jdbcTemplate;
        private static final String DEFAULT_QUERY = "SELECT 1";
        this.jdbcTemplate.query()
        builder.withDetail("hello", result);

ElasticsearchHealthIndicator 检查Elasticsearch集群状况

 

JmsHealthIndicator  检查JMS消息代理状况

 

MailHealthIndicator 检查邮件服务器状况

 

MongoHealthIndicator  检查Mongo数据库状况

 

RabbitHealthIndicator  检查Rabbit服务器状况

 

RedisHealthIndicator  检查Redis服务器状况

 

SolrHealthIndicator   检查Solr服务器状况

 

ConfigServerHealthIndicator  检查是否能够读取配置文件(在应用配置中心Maven 后才生效)

关键日志信息:

Registering application zjs-crypto with eureka with status UP
  //通知注册中心当前状态修改为UP
 Saw local status change event StatusChangeEvent [timestamp=1552551178514, current=UP, previous=STARTING]
 DiscoveryClient_ZJS-CRYPTO/10.10.12.27:zjs-crypto:11001: registering service…
 // 通知请求执行成功
 DiscoveryClient_ZJS-CRYPTO/10.10.12.27:zjs-crypto:11001 - registration status: 204

//健康检查异常
 启动健康检查异常 UNKNOWN {error=no property sources located}

Unregistering application zjs-crypto with eureka with status DOWN
 //通知注册中心修改当前状态为DOWN
 Saw local status change event StatusChangeEvent [timestamp=1552551180100, current=DOWN, previous=UP]

DiscoveryClient_ZJS-CRYPTO/10.10.12.27:zjs-crypto:11001: registering service…
 // 通知请求执行成功
 DiscoveryClient_ZJS-CRYPTO/10.10.12.27:zjs-crypto:11001 - registration status: 204

实际触发的验证类:

自定义健康监控节点类
  com.zjs.password.healthIndicator.CustomHealthIndicator
  com.zjs.password.healthIndicator.TestHealthIndicator

Spirng boot 相关包自带的健康监控节点类
  org.springframework.cloud.client.discovery.health.DiscoveryCompositeHealthIndicator
  org.springframework.boot.actuate.health.DiskSpaceHealthIndicator
  org.springframework.boot.actuate.health.RabbitHealthIndicator
  org.springframework.boot.actuate.health.DataSourceHealthIndicator
  org.springframework.boot.actuate.health.CompositeHealthIndicator
  org.springframework.cloud.config.client.ConfigServerHealthIndicator

注意!
         请使用spring boot 支持的组件。尤其是数据库,只针对 spring-boot-data-jpa !! 如使用其他数据库 请自定义健康节点方法自行实现。

注意! 不读取配置文件已算是健康检查失败!

PS : 根据引入的maven包不通,所以触发的健康验证类也不一样。可想而知,是不同的包自己实现自己的健康验证方法。

Spirng boot 启动的时候进行监控检查不通过停止服务与自定义健康监控节点的更多相关文章

  1. 我的前端故事----来聊聊react-native应用的健康监控

    监控什么 今天我们来聊聊如何监控你的应用程序,这里的监控说的不是让我们去监控用户,而是监控应用的健康状态,什么是健康状态呢?对于后端的同学来说,在微服务的架构下,每个子服务是否正常工作.返回的结果是否 ...

  2. Security基础(六):部署Zabbix监控平台、配置及使用Zabbix监控系统、自定义Zabbix监控项目、实现Zabbix报警功能

    一.部署Zabbix监控平台 目标: 本案例要求部署一台Zabbix监控服务器,一台被监控主机,为进一步执行具体的监控任务做准备: 在监控服务器上安装LAMP环境    修改PHP配置文件,满足Zab ...

  3. Spring Boot启动过程(七):Connector初始化

    Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...

  4. Spring Boot 启动原理分析

    https://yq.aliyun.com/articles/6056 转 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启 ...

  5. S3C6410 纯粹的裸机启动,自己写的SD BOOT启动

    这几天晚上一直折腾S3C6410的裸机SD卡启动,不大想使用UBOOT,我是搞硬件的,对底层非常感兴趣,不喜欢已经写好的,因此自己一直在尝试,其实很早之前就试过SD卡启动,也就是ARM11上电后会把S ...

  6. Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动

    之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...

  7. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十五):系统服务监控

    系统服务监控 新建监控工程 新建Spring Boot项目,取名 kitty-monitor,结构如下. 添加项目依赖 添加 spring boot admin 的相关依赖. pom.xml < ...

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

    文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...

  9. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

随机推荐

  1. 15 jmeter分布式性能测试

    背景由于jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起Java内存溢出的错误.要解决这个问题,可以使用分布式测试,运行多台机器运用所 ...

  2. 万恶之源 - Python基础知识补充

    编码转换 编码回顾: 1. ASCII : 最早的编码. ⾥⾯有英⽂⼤写字⺟, ⼩写字⺟, 数字, ⼀些特殊字符. 没有中⽂, 8个01代码, 8个bit, 1个byte 2. GBK: 中⽂国标码, ...

  3. HTTP权威指南读书笔记

    HTTP权威指南笔记 读书有两种境界,第一种境界是将书读薄,另一种是读厚.本篇文章就是HTTP权威指南的读书笔记,算是读书的第一重境界,将厚书读薄.文章对HTTP的一些关键概念做了比较详细的概述,通读 ...

  4. python3专业版安装及破解

    1.网址 https://www.jetbrains.com/pycharm/download/#section=windows,打开页面,点击下载专业版 2.这是下载好的文件,双击运行即可. //详 ...

  5. ES6(简)

    一. let.const 和 var let和const只在当前块级作用域中有效const用来声明常量var是全局作用域有效的 constants.js 模块export const A = 1;ex ...

  6. Summary: 书架问题

    Consider the problem of storing n books on shelves in a library. The order of the books is fixed by ...

  7. VS2010/MFC编程入门之五十一(图形图像:GDI对象之画刷CBrush)

    上一节中鸡啄米主要讲的是画笔CPen的用法,前面也说了,GDI对象中最常用的就是画笔和画刷,本节就讲讲画刷CBrush. 鸡啄米依然是通过实例的方式来说明画刷的用法.此实例要实现的功能是,对话框上有一 ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON Roberts2

    zw版[转发·台湾nvp系列Delphi例程]HALCON Roberts2 procedure TForm1.Button1Click(Sender: TObject);var op: HOpera ...

  9. iframe嵌套

    iframe基本内涵 通常我们使用iframe直接直接在页面嵌套iframe标签指定src就可以了. <iframe src="demo_iframe_sandbox.htm" ...

  10. leetcode_目录

    3Sum Closest 3Sum 4Sum Add Binary Add Two Numbers Anagrams Balanced Binary Tree Best Time to Buy and ...