Day17_08_SpringCloud教程之Eureka开启健康检查功能

时间:2022-12-23 16:01:12


08_SpringCloud教程之Eureka开启健康检查功能

一. Eureka健康监测概述

默认情况下,Eureka的健康检测并不是通过actuator的health端点来实现的,而是依靠客户端心跳的方式来保持服务实例的存活.在Eureka的服务续约和剔除机制下,客户端的健康状态从注册到服务注册中心后会一直处于UP状态,除非心跳终止一段时间后,服务注册中心将其剔除.

默认的心跳的方式可以有效地检查客户端进程是否正常运行,但无法有效的检查客户端是否可以正常提供服务.比如大多数微服务应用都会有一些其他的外部资源依赖,例如数据库,REDIS缓存等.如果我们的应用与这些外部资源无法连通的时候,实际上已经不能提供正常的对外服务了,但因为客户端心跳依然在运行,所以它还是会被服务消费者调用,而这样的调用实际上并不能获得预期的后果.

二. 健康监测具体实现

那么此时我们可以将其健康检查的方式修改为actuator的Health端点来实现.

  • 1️⃣. 在pom.xml中引入​​spring-boot-starter-actuator​​模块的依赖;
  • 2️⃣. 在eureka客户端中的​​application.properties​​或yml文件中配置:​​eureka.client.healthcheck.enabled=true​​,就可以改变eureka server对客户端健康检测的方式,改用actuator的/actuator/health端点来检测;
  • 3️⃣. 如果你对actuator的health端点做了处理,比如加了前缀等,请按照我这篇文章Eureka配置的端点配置对应修改,保证服务注册中心和正确访问到端点的路径.

1. 添加actuator依赖

在eureka-client模块中添加actuator依赖包.

<dependencies>
<!--eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!--引入健康检查依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

2. 新建HealthChecker并实现HealthIndicator,添加自己的逻辑

package com.syc.cloud.config;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class HealthChecker implements HealthIndicator {

private boolean up = true;

@Override
public Health health() {
if (up) {
return new Health.Builder().withDetail("status", "up").up().build(); //自定义监控内容
} else {
return new Health.Builder().withDetail("error", "client is down").down().build();
}
}

public boolean isUp() {
return up;
}

public void setUp(boolean up) {
this.up = up;
}
}

3. 新建HealthController提供修改UP/DOWN状态的API接口

package com.syc.cloud.web;

import com.syc.cloud.config.HealthChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HealthController {

@Autowired
private HealthChecker myHealthChecker;

@RequestMapping("/up")
public String up(@RequestParam("up") Boolean up) {
myHealthChecker.setUp(up);
return myHealthChecker.isUp() ? "UP" : "DOWN";
}
}

4. 修改application.yml文件,添加eureka.client.healthcheck.enabled=true和配置actuator

server:
port: 8762
spring:
application:
name: service-client
eureka:
client:
healthcheck:
enabled: true #这个属性不会提示 修改健康检测方式为health监控
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#actuator设置
management:
endpoints:
web:
exposure:
include: "*" #暴露所有端点,默认是info和health
endpoint:
health:
show-details: always #默认是never

注意:

​eureka.client.healthcheck.enabled=true​​是不会自动提示的,actuator设置那部分是SpringBoot2.0以上版本必加的.

5. 查看运行结果

5.1 现在先后启动服务端和客户端,发现Eureka面板已经有此服务了,状态为UP.


Day17_08_SpringCloud教程之Eureka开启健康检查功能

5.2 查看一下客户端的健康信息,这里如果不配置刚才的actuator设置则只有status属性.


Day17_08_SpringCloud教程之Eureka开启健康检查功能

5.3 请求up接口修改客户端的状态.


Day17_08_SpringCloud教程之Eureka开启健康检查功能

5.4 再次查看客户端的健康信息,已经变成DOWM了


Day17_08_SpringCloud教程之Eureka开启健康检查功能

5.5 再次访问Eureka面板发现也变成DOWN了.


Day17_08_SpringCloud教程之Eureka开启健康检查功能

此时消费者就不能在这个客户端获取服务了,如果再次请求up接口变为UP状态则Eureka面板也会跟着变.