Spring Cloud(Dalston.SR5)--Eureka 服务实例健康检查

时间:2023-03-09 03:11:18
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,表示我们的自定义健康检查是正常运行的。

    Spring Cloud(Dalston.SR5)--Eureka 服务实例健康检查