自我保护机制的触发条件: (当每分钟心跳次数( 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监控应用
=============================
开启监控
org.springframework.boot
spring-boot-starter-actuator
实现:
在服务提供方的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”}
最后
文章中涉及到的知识点我都已经整理成了资料,录制了视频供大家下载学习,诚意满满,希望可以帮助在这个行业发展的朋友,在论坛博客等地方少花些时间找资料,把有限的时间,真正花在学习上,所以我把这些资料,分享出来。相信对于已经工作和遇到技术瓶颈的朋友们,在这份资料中一定都有你需要的内容。