了解 Spring Cloud Eureka 服务器的自我保护和更新门槛

我是开发微服务的新手,尽管我已经研究了一段时间,阅读了 Spring 和 Netflix 的文档。

我已经开始了一个简单的项目 可以在 Github 上找到。它基本上是一个 Eureka 服务器(Archimedes)和三个 Eureka 客户端微服务(一个公共 API 和两个私有 API)。检查 github 的自述文件以获得详细描述。

关键是,当所有事情都在运行时,我希望如果一个私有微服务被终止,Eureka 服务器会实现并从注册表中删除它。

I 在 Stackoverflow 上找到了这个问题,并且该解决方案通过在 Eureka Server 配置中使用 enableSelfPreservation:false来传递。在一段时间后执行此操作,被终止的服务将如预期的那样消失。

然而,我可以看到以下信息:

自我保存模式已关闭。这可能不会保护实例 网络/其他问题的过期。

1.自我保护的目的是什么?医生表明,与自我保存的 “客户可以得到不再存在的实例”。那么什么时候开/关是明智的呢?

此外,当自我保存打开时,您可能会在 Eureka 服务器控制台中收到一个突出的消息警告:

紧急情况! EUREKA 可能错误地声称 不是的,更新不会超过临界值,因此 例子不会为了安全起见而过期。

现在,继续使用 Spring Eureka 控制台。

Lease expiration enabled    true/false
Renews threshold    5
Renews (last min)   4

我遇到过一个奇怪的阈值计数行为: 当我单独启动 Eureka Server 时,阈值是1。

2.我有一个单一的 Eureka 服务器,并与 registerWithEureka: false配置,以防止它注册到另一个服务器。那么,为什么它出现在阈值计数?

3.对于每个客户端,我开始的阈值计数增加 + 2。我猜是因为他们每分钟发送2条更新消息,对吗?

4.Eureka 服务器从不发送更新,所以最后一分钟更新总是低于阈值。这正常吗?

renew threshold 5
rewnews last min: (client1) +2 + (client2) +2 -> 4

服务器 cfg:

server:
port: ${PORT:8761}


eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enableSelfPreservation: false
#   waitTimeInMsWhenSyncEmpty: 0

客户端1 cfg:

spring:
application:
name: random-image-microservice


server:
port: 9999


eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
healthcheck:
enabled: true
53429 次浏览

I got the same question as @codependent met, I googled a lot and did some experiment, here I come to contribute some knowledge about how Eureka server and instance work.

Every instance needs to renew its lease to Eureka Server with frequency of one time per 30 seconds, which can be define in eureka.instance.leaseRenewalIntervalInSeconds.

Renews (last min): represents how many renews received from Eureka instance in last minute

Renews threshold: the renews that Eureka server expects received from Eureka instance per minute.

For example, if registerWithEureka is set to false, eureka.instance.leaseRenewalIntervalInSeconds is set to 30 and run 2 Eureka instance. Two Eureka instance will send 4 renews to Eureka server per minutes, Eureka server minimal threshold is 1 (written in code), so the threshold is 5 (this number will be multiply a factor eureka.server.renewalPercentThreshold which will be discussed later).

SELF PRESERVATION MODE: if Renews (last min) is less than Renews threshold, self preservation mode will be activated.

So in upper example, the SELF PRESERVATION MODE is activated, because threshold is 5, but Eureka server can only receive 4 renews/min.

  1. Question 1:

The SELF PRESERVATION MODE is design to avoid poor network connectivity failure. Connectivity between Eureka instance A and B is good, but B is failed to renew its lease to Eureka server in a short period due to connectivity hiccups, at this time Eureka server can't simply just kick out instance B. If it does, instance A will not get available registered service from Eureka server despite B is available. So this is the purpose of SELF PRESERVATION MODE, and it's better to turn it on.

  1. Question 2:

The minimal threshold 1 is written in the code. registerWithEureka is set to false so there will be no Eureka instance registers, the threshold will be 1.

In production environment, generally we deploy two Eureka server and registerWithEureka will be set to true. So the threshold will be 2, and Eureka server will renew lease to itself twice/minute, so RENEWALS ARE LESSER THAN THRESHOLD won't be a problem.

  1. Question 3:

Yes, you are right. eureka.instance.leaseRenewalIntervalInSeconds defines how many renews sent to server per minute, but it will multiply a factor eureka.server.renewalPercentThreshold mentioned above, the default value is 0.85.

  1. Question 4:

Yes, it's normal, because the threshold initial value is set to 1. So if registerWithEureka is set to false, renews is always below threshold.

I have two suggestions for this:

  1. Deploy two Eureka server and enable registerWithEureka.
  2. If you just want to deploy in demo/dev environment, you can set eureka.server.renewalPercentThreshold to 0.49, so when you start up a Eureka server alone, threshold will be 0.

I've created a blog post with the details of Eureka here, that fills in some missing detail from Spring doc or Netflix blog. It is the result of several days of debugging and digging through source code. I understand it's preferable to copy-paste rather than linking to an external URL, but the content is too big for an SO answer.

You can try to set renewal threshold limit in your eureka server properties. If you have around 3 to 4 Microservices to register on eureka, then you can set it to this:

eureka.server.renewalPercentThreshold=0.33
 server:
enableSelfPreservation: false

if set to true, Eureka expects service instances to register themselves and to continue to send registration renewal requests every 30 s. Normally, if Eureka doesn’t receive a renewal from a service for three renewal periods (or 90 s), it deregisters that instance.

if set to false, in this case, Eureka assumes there’s a network problem, enters self-preservation mode, and won’t deregister service instances.

Even if you decide to disable self-preservation mode for development, you should leave it enabled when you go into production.