自我保护机制
机制
Eureka在CAP理论当中是属于AP , 也就说当产生网络分区时,Eureka保证系统的可用性,但不保证系统里面数据的一致性
默认开启,服务器端容错的一种方式,即短时间心跳不到达仍不剔除服务列表里的节点
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
默认情况下,Eureka Server在一定时间内,没有接收到某个微服务心跳,会将某个微服务注销(90S)。但是当网络故障时,微服务与Server之间无法正常通信,上述行为就非常危险,因为微服务正常,不应该注销。
Eureka Server通过自我保护模式来解决整个问题,当Server在短时间内丢失过多客户端时,那么Server会进入自我保护模式,会保护注册表中的微服务不被注销掉。当网络故障恢复后,退出自我保护模式。
思想:宁可保留健康的和不健康的,也不盲目注销任何健康的服务。
自我保护触发
客户端每分钟续约数量小于客户端总数的85%时会触发保护机制
自我保护机制的触发条件: (当每分钟心跳次数( renewsLastMin ) 小于 numberOfRenewsPerMinThreshold 时,并且开启自动保护模式开关( eureka.server.enable-self-preservation = true ) 时,触发自我保护机制,不再自动过期租约。) numberOfRenewsPerMinThreshold = expectedNumberOfRenewsPerMin * 续租百分比( eureka.server.renewalPercentThreshold, 默认0.85 ) expectedNumberOfRenewsPerMin = 当前注册的应用实例数 x 2
为什么乘以 2:
默认情况下,注册的应用实例每半分钟续租一次,那么一分钟心跳两次,因此 x 2 。
服务实例数:10个,期望每分钟续约数:10 * 2=20,期望阈值:20*0.85=17,自我保护少于17时 触发。
剔除:
AbstractInstanceRegistry
public void evict(long additionalLeaseMs) {
logger.debug("Running the evict task");
if (!isLeaseExpirationEnabled()) {
logger.debug("DS: lease expiration is currently disabled.");
return;
}
此代码意思:if中判断为true,不走此逻辑,走下面的剔除。如果if为false。走此逻辑,不剔除。
PeerAwareInstanceRegistryImpl
@Override
public boolean isLeaseExpirationEnabled() {
if (!isSelfPreservationModeEnabled()) {
//如果打开自我保护,不进入此逻辑。
// The self preservation mode is disabled, hence allowing the instances to expire.
return true;
}
return numberOfRenewsPerMinThreshold > 0 && getNumOfRenewsInLastMin() > numberOfRenewsPerMinThreshold;
}
关闭
eureka.server.enable-self-preservation=false
关闭后会提示
![]()
清理时间
默认60秒
eureka.server.eviction-interval-timer-in-ms=3000
使用Spring Boot2.x Actuator监控应用
开启监控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
实现:
在服务提供方的provider中,加入上面的依赖


访问端点信息:
http://localhost:8088//actuator
{
"_links": {
"self": {
"href": "http://localhost:8080//actuator",
"templated": false
},
"health": {
"href": "http://localhost:8080//actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:8080//actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://localhost:8080//actuator/info",
"templated": false
}
}
}
ttp://localhost:8088//actuator/health
{
"status": "UP"
}
默认端点
Spring Boot 2.0 的Actuator只暴露了health和info端点,提供的监控信息无法满足我们的需求
在1.x中有n多可供我们监控的节点,官方的回答是为了安全….
如果想开启所有的端点信息:
management.endpoints.web.exposure.include=*
再次访问:http://localhost:8088//actuator
{
"_links": {
"self": {
"href": "http://localhost:8088//actuator",
"templated": false
},
"archaius": {
"href": "http://localhost:8088//actuator/archaius",
"templated": false
},
"beans": {
"href": "http://localhost:8088//actuator/beans",
"templated": false
},
"caches": {
"href": "http://localhost:8088//actuator/caches",
"templated": false
},
"caches-cache": {
"href": "http://localhost:8088//actuator/caches/{cache}",
"templated": true
},
"health-path": {
"href": "http://localhost:8088//actuator/health/{*path}",
"templated": true
},
"health": {
"href": "http://localhost:8088//actuator/health",
"templated": false
},
"info": {
"href": "http://localhost:8088//actuator/info",
"templated": false
},
"conditions": {
"href": "http://localhost:8088//actuator/conditions",
"templated": false
},
"configprops": {
"href": "http://localhost:8088//actuator/configprops",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8088//actuator/env/{toMatch}",
"templated": true
},
"env": {
"href": "http://localhost:8088//actuator/env",
"templated": false
},
"loggers-name": {
"href": "http://localhost:8088//actuator/loggers/{name}",
"templated": true
},
"loggers": {
"href": "http://localhost:8088//actuator/loggers",
"templated": false
},
"heapdump": {
"href": "http://localhost:8088//actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8088//actuator/threaddump",
"templated": false
},
"metrics": {
"href": "http://localhost:8088//actuator/metrics",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8088//actuator/metrics/{requiredMetricName}",
"templated": true
},
"scheduledtasks": {
"href": "http://localhost:8088//actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://localhost:8088//actuator/mappings",
"templated": false
},
"refresh": {
"href": "http://localhost:8088//actuator/refresh",
"templated": false
},
"features": {
"href": "http://localhost:8088//actuator/features",
"templated": false
},
"service-registry": {
"href": "http://localhost:8088//actuator/service-registry",
"templated": false
}
}
}
api端点功能
Health
会显示系统状态
{"status":"UP"}
shutdown
用来关闭节点
开启远程关闭功能
management.endpoint.shutdown.enabled=true
使用Post方式请求端点
{
"message": "Shutting down, bye..."
}
autoconfig
获取应用的自动化配置报告 beans
获取应用上下文中创建的所有Bean
configprops
获取应用中配置的属性信息报告
env
获取应用所有可用的环境属性报告
Mappings
获取应用所有Spring Web的控制器映射关系报告
info
获取应用自定义的信息
metrics
返回应用的各类重要度量指标信息
Metrics节点并没有返回全量信息,我们可以通过不同的key去加载我们想要的值
metrics/jvm.memory.max
Threaddump
1.x中为dump
返回程序运行中的线程信息
Eureka 健康检查
由于server和client通过心跳保持 服务状态,而只有状态为UP的服务才能被访问。看eureka界面中的status。
比如心跳一直正常,服务一直UP,但是此服务DB连不上了,无法正常提供服务。
此时,我们需要将微服务的健康状态也同步到server。只需要启动eureka的健康检查就行。这样微服务就会将自己的健康状态同步到eureka。配置如下即可。
开启手动控制
在client端配置:将自己真正的健康状态传播到server。
eureka:
client:
healthcheck:
enabled: true
Client端配置Actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
改变健康状态的Service
@Service
public class HealthStatusService implements HealthIndicator{
private Boolean status = true;
public void setStatus(Boolean status) {
this.status = status;
}
@Override
public Health health() {
// TODO Auto-generated method stub
if(status)
return new Health.Builder().up().build();
return new Health.Builder().down().build();
}
public String getStatus() {
// TODO Auto-generated method stub
return this.status.toString();
}
}
测试用的Controller
@GetMapping("/health")
public String health(@RequestParam("status") Boolean status) {
healthStatusSrv.setStatus(status);
return healthStatusSrv.getStatus();
}
演示
服务UP:




本文深入探讨Eureka自我保护机制的工作原理,包括其在CAP理论中的定位、触发条件及如何避免误删健康服务。同时介绍了如何利用Spring Boot Actuator进行健康检查和服务状态监控。
1407

被折叠的 条评论
为什么被折叠?



