使用Eureka服务的时候,如果其中一个服务提供者挂掉,而客户端并不知道从而继续调用服务,这时候会导致异常的发生。所以Eureka在服务中心会调用服务注册者的状态,客户端在向服务中心获取服务状态的时候会知道哪个服务提供者已经下线,从而不再继续调用该服务,这里就用到了Eureka的健康检测。
在监听的过程中Eureka的Server端会发生以下这几件事:
- EurekaInstanceCanceledEvent 服务下线事件
- EurekaInstanceRegisteredEvent 服务注册事件
- EurekaInstanceRenewedEvent 服务续约事件
- EurekaRegistryAvailableEvent Eureka注册中心启动事件
- EurekaServerStartedEvent Eureka Server启动事件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置文件:
# 可访问全部敏感端口
# /evn 服务的全部环境变量信息
# /health 当前服务状态
endpoints:
sensitive: false
eureka:
instance:
lease-renewal-interval-in-seconds: 5 #心跳设置,告诉服务器该实例仍在使用
lease-expiration-duration-in-seconds: 10 #告诉服务器如果10秒内未发送任何续约请求,则关闭该客户端
metadata-map:
company-name: in
client:
serviceUrl:
defaultZone: http://admin:admin@localhost:9400/eureka
instance-info-replication-interval-seconds: 10
Controller层:
@RestController
@RequestMapping(value = "/eureka")
@Data
public class TestController {
public volatile String value = "1";
@GetMapping(value = "/health/{value}")
public void setIsDB(@PathVariable String value) {
this.value = value;
}
}
访问地址:
localhost:8080/eureka/health/1
import lombok.Data;
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;
import javax.annotation.Resource;
/**
* @author Huan Lee
* @version 1.0
* @date 2020-08-10 10:13
* @describtion 自定义健康指示器
*/
@Component
@Data
public class HealthIndicatorUtil implements HealthIndicator {
@Resource
private TestController testController;
@Override
public Health health() {
if(testController.getValue().equals("1")){
return new Health.Builder(Status.UP).build();
} else{
return new Health.Builder(Status.DOWN).build();
}
}
}
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @author Huan Lee
* @version 1.0
* @date 2020-08-10 10:12
* @describtion 健康检查处理器:将服务提供者的健康状态传递给eureka服务器
*/
@Component
public class HealthCheckHandlerUtil implements HealthCheckHandler {
@Resource
private HealthIndicatorUtil healthIndicatorUtil;
@Override
public InstanceInfo.InstanceStatus getStatus(InstanceInfo.InstanceStatus currentStatus) {
Status status = healthIndicatorUtil.health().getStatus();
if (status.equals(Status.UP)) {
return InstanceInfo.InstanceStatus.UP;
} else {
return InstanceInfo.InstanceStatus.DOWN;
}
}
}
通过执行Controller接口来改变服务状态(UP、DOWN),观察服务健康状态及Eureka上的状态。
http://10.4.13.208:8085/actuator/health
{
"status":"UP",
"details":{
"healthIndicatorUtil":{
"status":"UP"
},
"sentinel":{
"status":"UP",
"details":{
"enabled":false
}
},
"diskSpace":{
"status":"UP",
"details":{
"total":499963174912,
"free":271268904960,
"threshold":10485760
}
},
"refreshScope":{
"status":"UP"
},
"discoveryComposite":{
},
"hystrix":{
"status":"UP"
}
}
}
来源:oschina
链接:https://my.oschina.net/u/3727895/blog/4481621