eureka是选择CAP理论中AP进行设计的,所以从下面配置中可以看出由于配置问题或者本身设计问题,可能会经常出现服务下线后注册中心发现的不及时,或者注册中心剔除了因为网络抖动等其他原因,本不应该被剔除的服务。
因此使用eureka作为注册时,我们应当考虑按照AP原理设计所带来的问题。
eureka.client.fetch-registry
默认为true
表示此客户端是否应从eureka服务器获取eureka注册列表信息。一般eureka服务端要设置为false。
eureka.client.register-with-eureka
默认为true
表示此实例是否应将其信息注册到eureka服务器以供其他服务发现。一般eureka服务端要设置为false。
eureka.server.eviction-interval-timer-in-ms
默认为60s
Eureka Server会定时(间隔值是eureka.server.eviction-interval-timer-in-ms)进行检查,如果发现实例在一定时间(此值由eureka.instance.lease-expiration-duration-in-seconds定义,默认值为90s)内没有收到心跳,
则会注销此实例。
配置eureka.server.eviction-interval-timer-in-ms=1000,可以看到输出日志1秒一次检查。
2020-05-26 10:06:58.837 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:06:59.837 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:00.837 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:01.837 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:02.838 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:03.838 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:04.838 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:05.839 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:06.840 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:07.840 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
2020-05-26 10:07:08.840 INFO 17208 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
eureka.server.enable-self-preservation
默认为true
自我保护,生产上要设置为true,避免网络抖动原因造成剔除正常的服务。
如果开启了,则会判断上一分钟的续约数是否小于自我保护阀值,
如果上一分钟的续约数(numOfRenewsInLastMin—Renews (last min))小于自我保护阀值(numberOfRenewsPerMinThreshold—Renews threshold),则开启自我保护机制,不再进行服务的剔除。
Renews threshold(自我保护阀值) = 服务总数 * (60S/客户端续约间隔(eureka.instance.lease-renewal-interval-in-seconds)) * 自我保护续约百分比阀值因子(renewalPercentThreshold默认是85%)。
protected void updateRenewsPerMinThreshold() {
this.numberOfRenewsPerMinThreshold = (int) (this.expectedNumberOfClientsSendingRenews
* (60.0 / serverConfig.getExpectedClientRenewalIntervalSeconds())
* serverConfig.getRenewalPercentThreshold());
}
Renews (last min) = 上一分钟的续约数
续期数低于阈值,自我保护关闭,可能造成注册中心的实例出了问题却不能及时下线。
RENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
自我保护开启,续期数低于阈值,不再剔除服务。
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.
自动保存模式关闭。可能无法在出现网络/其他问题时保护实例到期。
THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
当开启自我保护后,如果服务端强制停掉(kill),可能会造成到达自我保护阈值而无法下线
解决方式:
1、关闭自我保护(不建议)
2、降低自我保护触发的阈值(不建议)
3、eureka.server自我注册,高可用(推荐)
如何让eureka快速发现服务下线
- client端心跳默认30s,可以适当快一点
- client续约到期时间默认90s可以适当快一点
- server端定时检查,同样默认90s,可以适当快一点
- client下线触发shutdown,server是可以立即感知的,因为会通知server
- 向注册中心发送delete请求,localhost:11111/eureka/apps/applicationName/ip:port