默认情况下,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 服务实例健康检查的更多相关文章

  1. Spring Cloud(Dalston.SR5)--Eureka 服务消费

    服务被注册.发布到 Eureka 服务器后,需要有程序去发现他,并且进行调用,称为服务消费,一个服务可能会部署多个实例,调用过程可能涉及负载均衡.服务器查找等问题,这些问题 Netflix 项目已经帮 ...

  2. Spring Cloud(Dalston.SR5)--Eureka 常用配置

    配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...

  3. Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用-服务提供和消费

    由于 Eureka 注册中心只是在内存中保存服务注册实例,并且没有将服务注册实例进行同步,因此我们需要对服务提供和消费进行调整,需要指定服务提供和消费的注册.服务发现的具体Eureka 注册中心配置, ...

  4. Spring Cloud(Dalston.SR5)--Eureka 注册中心搭建

    基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务治理可以说是微服务架构中最为核心和基础的模块,他主要用来实现各个微服务实例的自动化注册与发现 服务注册:在 ...

  5. Spring Cloud(Dalston.SR5)--Eureka 服务提供者

    要使微服务应用向注册中心发布自己,首先需要在 pom.xml 配置文件中增加对 spring-boot-starter-eureka 的依赖,然后在主类中增加 @EnableDiscoveryClie ...

  6. Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用搭建

    高可用集群 在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对与微服务和服务注册中心都需要高可用部署,Eureka 高可用实际上就是将自己 ...

  7. Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群

    通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...

  8. Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合

    创建项目 要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下: <?xmlversion=" ...

  9. Spring Cloud(Dalston.SR5)--Hystrix 断路器

    Spring Cloud 对 Hystrix 进行了封装,使用 Hystrix 是通过 @HystrixCommand 注解来使用的,被 @HystrixCommand 注解标注的方法,会使用 Asp ...

随机推荐

  1. Java学习笔记13(equals()方法;toString()方法)

    equals()方法: equals方法是Object类中的方法:Object是所有类的祖宗,所以所有类都有equals()方法: boolean equals(Object obj); equals ...

  2. HDU 6045 17多校2 Is Derek lying?

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  3. java的异常抛出和String类常用方法

    一.异常抛出 异常是程序的异种非错误的意外情况,分为运行期异常(RuntimeException)和编译期异常(CheckedExcption) 处理异常可以用try——catch或自定义 impor ...

  4. tableView 分割线的处理

    有时候根据UI设计图的需要我们需要对原生的TableView分割线做靠左,靠右的操作 在下面这个方法中实现即可. - (void)tableView:(UITableView *)tableView ...

  5. gcc/g++多版本切换 (ubuntu18.04)

    使用Ubuntu18.04已经有一段时间了,在使用过程中经常需要处理不同软件的编译工作,但是这时候就遇到这样一个问题,那就是不同软件,甚至是同一个软件的不同版本都会使用不同版本的gcc/g++来进行编 ...

  6. [LeetCode&Python] Problem 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  7. [LeetCode&Python] Problem 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  8. HDU - 3982:Harry Potter and J.K.Rowling(半平面交+圆与多边形求交)(WA ing)

    pro:给定一枚蛋糕,蛋糕上某个位置有个草莓,寿星在上面切了N刀,最后寿星会吃含有草莓的那一块蛋糕,问他的蛋糕占总蛋糕的面积比. sol:显然需要半平面交求含有蛋糕的那一块,然后有圆弧,不太方便求交. ...

  9. # 20155219实验二 Java面向对象程序设计

    20155219实验二 Java面向对象程序设计 一.实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5 ...

  10. react写单选按钮或table标签

    首先,原理是一样的: class Loca_choose_wrap extends Component{ constructor(){ super(); this.state={ port_name: ...