zoukankan      html  css  js  c++  java
  • Spring Cloud(Dalston.SR5)--Eureka 服务实例健康检查

    默认情况下,Eureka 客户端每隔 30 秒会发送一次心跳给服务器端,告知正常存活,但是,实际环境中有可能出现这种情况,客户端表面上可以正常发送心跳,但实际上服务是不可用的,例如,一个需要访问数据的服务提供者,但是数据库已经无法访问了;或者依赖的第三方服务已经无法访问了,对于这样的情况应当告诉服务器当前客户端的状态,可以使用 Eureka 的健康检查访问控制器来实现。

    Spring Boot Actuator

    该模块主要用于系统监控,当应用程序整合了 Actuator 后,就会自动提供多个服务端点,这些端点可以让外部看到应用程序的健康状况。在之前的服务提供者项目中,增加如下依赖:

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

    启动项目,并访问 http://localhost:8080/health ,可以看到返回了系统自检的内容,如下:

    {"description":"Spring Cloud Eureka Discovery Client","status":"UP"}

       

    自定义健康检查

    客户端表面上可以正常发送心跳,但实际上服务是不可用的,例如,一个需要访问数据的服务提供者,但是数据库已经无法访问了;或者依赖的第三方服务已经无法访问了,这时就需要我们自己实现健康检查,我们需要实现一个HealthIndicator,继承 org.springframework.boot.actuate.health.HealthIndicator 接口;如果服务提供者希望把健康状态告诉Eureka 服务端,则还需要实现一个自定义的 HealthCheckHandler(健康检查处理器), HealthCheckHandler 会将应用的健康状态保存到内存中,状态一旦发生改变,就会重新向服务器进行注册,示例如下:

    • 创建自定义 HealthIndicator

      package org.lixue.webservices.services;

         

      import org.springframework.boot.actuate.health.Health;

      import org.springframework.boot.actuate.health.HealthIndicator;

      import org.springframework.boot.actuate.health.Status;

      import org.springframework.stereotype.Component;

         

      @Component

      public class MyHealthIndicator implements HealthIndicator{

      private int healthIndicatorErrorCount;

      private int healthIndicatorCount;

      private boolean hasError=false;

      @Override

      public Health health(){

      if(!hasError){

      healthIndicatorCount++;

      //每检测5次,就返回DOWN

      if(healthIndicatorCount%5==0){

      hasError=true;

      }

      }else{

      //DOWN计数10次就UP

      healthIndicatorErrorCount++;

      if(healthIndicatorErrorCount>10){

      hasError=false;

      healthIndicatorErrorCount=0;

      }

      }

      if(hasError){

      return new Health.Builder(Status.DOWN).build();

      }

      return new Health.Builder(Status.UP).build();

      }

      }

         

    • 创建自定义 HealthCheckHandler

      package org.lixue.webservices.services;

         

      import com.netflix.appinfo.HealthCheckHandler;

      import com.netflix.appinfo.InstanceInfo;

      import org.springframework.beans.factory.annotation.Autowired;

      import org.springframework.boot.actuate.health.Status;

      import org.springframework.stereotype.Component;

         

      @Component

      public class MyHealthHealthCheckHandler implements HealthCheckHandler{

      @Autowired

      private MyHealthIndicator myHealthIndicator;

      @Override

      public InstanceInfo.InstanceStatus getStatus(InstanceInfo.InstanceStatus currentStatus){

      Status status=myHealthIndicator.health().getStatus();

      if(status==Status.UP){

      returnInstanceInfo.InstanceStatus.UP;

      }else{

      returnInstanceInfo.InstanceStatus.DOWN;

      }

      }

      }

    • 启动类增加自定义 healthIndicator 的Bean

      @SpringBootApplication

      @EnableDiscoveryClient

      public class ServiceProviderApplication{

      public static void main(String[]args){

      SpringApplication.run(ServiceProviderApplication.class,args);

      }

         

      @Bean

      public MyHealthIndicator myHealthIndicator(){

      return new MyHealthIndicator();

      }

      }

    • 测试验证

      启动项目,Eureka 中会启动一个定时器,定时刷新本地实例的信息,默认情况下,每隔30秒执行一次健康检查,访问我们的Eureka 服务端 http://eurekaserver01:9000 查询服务提供者的状态,可以看到经过5次健康检查后,出现了 DOWN,表示我们的自定义健康检查是正常运行的。

         

  • 相关阅读:
    前端与算法 leetcode 344. 反转字符串
    JavaScript闭包使用姿势指南
    前端与算法 leetcode 48. 旋转图像
    前端与算法 leetcode 36. 有效的数独
    前端与算法 leetcode 1. 两数之和
    前端与算法 leetcode 283. 移动零
    前端与编译原理 用js去运行js代码 js2run
    前端与算法 leetcode 66. 加一
    前端与算法 leetcode 350. 两个数组的交集 II
    前端与算法 leetcode 26. 删除排序数组中的重复项
  • 原文地址:https://www.cnblogs.com/li3807/p/8676401.html
Copyright © 2011-2022 走看看