了解 Spring Cloud Eureka Server 自保和更新阈值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33921557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:53:20  来源:igfitidea点击:

Understanding Spring Cloud Eureka Server self preservation and renew threshold

springspring-cloudnetflix-eureka

提问by codependent

I am new to developing microservices, although I have been researching about it for a while, reading both Spring's docs and Netflix's.

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

I have started a simple project available on Github. It is basically a Eureka server (Archimedes) and three Eureka client microservices (one public API and two private). Check github's readme for a detailed description.

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

The point is that when everything is running I would like that if one of the private microservices is killed, the Eureka server realizes and removes it from the registry.

关键是,当一切都在运行时,我希望如果其中一个私有微服务被杀死,Eureka 服务器会意识到并将其从注册表中删除。

I found this question on Stackoverflow, and the solution passes by using enableSelfPreservation:falsein the Eureka Server config. Doing this after a while the killed service disappears as expected.

在 Stackoverflow 上发现了这个问题,解决方案通过enableSelfPreservation:false在 Eureka Server 配置中使用。一段时间后执行此操作,被杀死的服务会按预期消失。

However I can see the following message:

但是我可以看到以下消息:

THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

自我保护模式已关闭。如果出现网络/其他问题,这可能无法保护实例到期。

1. What is the purpose of the self preservation? The docstates that with self preservation on "clients can get the instances that do not exist anymore". So when is it advisable to have it on/off?

1.自我保护的目的是什么?该文档指出,通过自我保护“客户可以获得不再存在的实例”。那么什么时候开启/关闭它是可取的呢?

Furthermore, when self preservation is on, you may get an outstanding message in the Eureka Server console warning:

此外,当自我保护开启时,您可能会在 Eureka Server 控制台警告中收到一条未完成的消息:

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 可能不正确地声称实例已启动,但实际上并非如此。续订少于阈值,因此实例不会仅仅为了安全而过期。

Now, going on with the Spring Eureka Console.

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

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

I have come across a weird behaviour of the threshold count: when I start the Eureka Server alone, the threshold is 1.

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

2. I have a single Eureka server and is configured with registerWithEureka: falseto prevent it from registering on another server. Then, why does it show up in the threshold count?

2. 我有一个 Eureka 服务器,并配置registerWithEureka: false为防止它在另一台服务器上注册。那么,为什么它会出现在阈值计数中呢?

3. For every client I start the threshold count increases by +2. I guess it is because they send 2 renew messages per min, am I right?

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

4. The Eureka server never sends a renew so the last min renews is always below the threshold. Is this normal?

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

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

Server 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

Client 1 cfg:

客户端 1 配置:

spring:
  application:
    name: random-image-microservice

server:
  port: 9999

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
    healthcheck:
      enabled: true

采纳答案by Nie Xing

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.

我和@codependent 遇到了同样的问题,我在谷歌上搜索了很多并做了一些实验,在这里我来贡献一些关于 Eureka 服务器和实例如何工作的知识。

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.

每个实例都需要以每 30 秒一次的频率更新对 Eureka Server 的租约,可以在eureka.instance.leaseRenewalIntervalInSeconds.

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

Renews (last min): 表示最后一分钟从 Eureka 实例收到的更新次数

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

更新阈值:Eureka 服务器每分钟期望从 Eureka 实例收到的更新。

For example, if registerWithEurekais set to false, eureka.instance.leaseRenewalIntervalInSecondsis set to 30 and run 2 Eureka instance. Two Eureka instance will send 4renews 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.renewalPercentThresholdwhich will be discussed later).

例如,如果registerWithEureka设置为falseeureka.instance.leaseRenewalIntervalInSeconds则设置为 30 并运行 2 个 Eureka 实例。两个 Eureka 实例每分钟会向Eureka 服务器发送4 次更新,Eureka 服务器最小阈值是1(代码中写的),所以阈值是5(这个数字将乘以一个因子eureka.server.renewalPercentThreshold,稍后讨论)。

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.

所以在上面的例子中,SELF PRESERVATION MODE 被激活,因为阈值是 5,但是 Eureka 服务器只能接收 4 个更新/分钟。

  1. Question 1:
  1. 问题 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.

SELF PRESERVATION MODE 旨在避免不良的网络连接故障。Eureka 实例 A 和 B 之间的连通性是好的,但是 B 由于连接问题无法在短时间内续约到 Eureka 服务器,此时 Eureka 服务器不能简单地踢出实例 B。如果是,则实例尽管 B 可用,但 A 将无法从 Eureka 服务器获得可用的注册服务。所以这就是SELF PRESERVATION MODE的目的,最好开启。

  1. Question 2:
  1. 问题2:

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

最小阈值 1 写在代码中。registerWithEureka设置为 false 则不会有 Eureka 实例寄存器,阈值为 1。

In production environment, generally we deploy two Eureka server and registerWithEurekawill 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 THRESHOLDwon't be a problem.

在生产环境中,一般我们会部署两台Eureka服务器,并且registerWithEureka会设置为true。所以阈值将是2,并且Eureka服务器将更新租约两次/分钟,所以RENEWALS ARE LESSER THAN THRESHOLD不会有问题。

  1. Question 3:
  1. 问题 3:

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

你是对的。eureka.instance.leaseRenewalIntervalInSeconds定义每分钟发送到服务器的更新次数,但它会乘以上述系数eureka.server.renewalPercentThreshold,默认值为 0.85。

  1. Question 4:
  1. 问题 4:

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

是的,这是正常的,因为阈值初始值设置为1。所以如果registerWithEureka设置为false,更新总是低于阈值。

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.renewalPercentThresholdto 0.49, so when you start up a Eureka server alone, threshold will be 0.
  1. 部署两个 Eureka 服务器并启用registerWithEureka.
  2. 如果只是想部署在demo/dev环境,可以设置eureka.server.renewalPercentThreshold为0.49,这样当你单独启动一个Eureka服务器时,阈值为0。

回答by Abhijit Sarkar

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.

我创建了一个博客文章与尤里卡的细节在这里,在距离Spring文档或Netflix的一些失踪详细填写博客。这是几天调试和挖掘源代码的结果。我知道最好是复制粘贴而不是链接到外部 URL,但内容对于 SO 答案来说太大了。

回答by Vivek Sahu

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:

您可以尝试在您的尤里卡服务器属性中设置续订阈值限制。如果您有大约 3 到 4 个微服务要在 eureka 上注册,那么您可以将其设置为:

eureka.server.renewalPercentThreshold=0.33