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

基于 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包不通,所以触发的健康验证类也不一样。可想而知,是不同的包自己实现自己的健康验证方法。

转载于:https://www.cnblogs.com/atliwen/p/10550180.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值