1. Overview
1.概述
In this tutorial, we’re going to learn about Eureka Self Preservation and Renewal.
在本教程中,我们将学习Eureka自我保存和更新。
We’ll start by creating a Eureka server alongside multiple Eureka client instances.
我们将首先创建一个Eureka服务器和多个Eureka客户端实例。
Then, we’ll register these clients with our Eureka server to show how self-preservation works.
然后,我们将在我们的Eureka服务器上注册这些客户端,以展示自我保护的工作方式。
2. Eureka Self-Preservation
2.尤里卡的自我保护
Before talking about self-preservation, let’s understand how the Eureka server maintains the client instance registry.
在谈论自我保护之前,让我们了解一下Eureka服务器是如何维护客户端实例注册表的。
During the start-up, the clients trigger a REST call with the Eureka server to self-register to the server’s instance registry. When a graceful shutdown occurs after use, the clients trigger another REST call so that the server can wipe out all the data related to the caller.
在启动过程中,客户端触发了一个与Eureka服务器的REST调用,以便在服务器的实例注册处进行自我注册。当使用后发生优雅关闭时,客户端会触发另一个REST调用,以便服务器能够抹去与调用者有关的所有数据。
To handle ungraceful client shutdowns the server expects heartbeats from the client at specific intervals. This is called renewal. If the server stops receiving the heartbeat for a specified duration, then it will start evicting the stale instances.
为了处理不体面的客户机关闭,服务器希望客户机在特定的时间间隔内发出心跳声。这被称为renewal。如果服务器在指定的时间内停止接收心跳,那么它将开始驱逐陈旧的实例。
The mechanism that stops evicting the instances when the heartbeats are below the expected threshold is called self-preservation. This might happen in the case of a poor network partition, where the instances are still up, but just can’t be reached for a moment or in the case of an abrupt client shutdown.
当心跳低于预期阈值时,停止驱逐实例的机制被称为自我保留。这可能发生在网络分区不佳的情况下,即实例仍在运行,只是一时无法联系上,或者在客户端突然关闭的情况下。
And when the server activates self-preservation mode it holds the instance eviction until the renewal rate is back above the expected threshold.
而当服务器激活自我保护模式时,它将保持实例的驱逐,直到更新率恢复到预期阈值以上。
Let’s see this in action.
让我们看看这个行动。
3. Creating the Server
3.创建服务器
Let’s first, create the Eureka server by annotating our Spring Boot main class with @EnableEurekaServer:
首先,让我们用@EnableEurekaServer注释我们的Spring Boot主类,从而创建Eureka服务器。
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
But now, let’s add the basic configurations for us to start up the server:
但现在,让我们添加基本配置,以便我们启动服务器。
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.instance.hostname=localhost
Since we don’t want our Eureka server to register with itself, we’ve set the property eureka.client.registerWithEureka as false. Here the property eureka.instance.hostname=localhost is particularly important since we’re running it in a local machine. Otherwise, we may end up creating an unavailable replica within the Eureka server – messing up the client’s heartbeat counts.
因为我们不希望我们的Eureka服务器向自己注册,所以我们将属性eureka.client.registerWithEureka设置为false。这里的属性eureka.instance.hostname=localhost特别重要,因为我们在本地机器上运行它。否则,我们可能最终会在Eureka服务器中创建一个不可用的副本–扰乱客户端的心跳计数。
Now let’s take a look at all the configuration and its relevance in the context of self-preservation in the next section.
现在让我们在下一节中看看所有的配置及其在自我保护方面的相关性。
3.1. Self-Preservation Configurations
3.1.自我保留的配置
By default, Eureka servers run with self-preservation enabled.
默认情况下,Eureka服务器在运行时启用自我保护功能。
However, for the sake of our understanding, let’s go through each of these configurations on the server-side.
然而,为了便于我们理解,让我们在服务器端逐一查看这些配置。
- eureka.server.enable-self-preservation: Configuration for disabling self-preservation – the default value is true
- eureka.server.expected-client-renewal-interval-seconds: The server expects client heartbeats at an interval configured with this property – the default value is 30
- eureka.instance.lease-expiration-duration-in-seconds: Indicates the time in seconds that the Eureka server waits since it received the last heartbeat from a client before it can remove that client from its registry – the default value is 90
- eureka.server.eviction-interval-timer-in-ms: This property tells the Eureka server to run a job at this frequency to evict the expired clients – the default value is 60 seconds
- eureka.server.renewal-percent-threshold: Based on this property, the server calculates the expected heartbeats per minute from all the registered clients – the default value is 0.85
- eureka.server.renewal-threshold-update-interval-ms: This property tells the Eureka server to run a job at this frequency to calculate the expected heartbeats from all the registered clients at this minute – the default value is 15 minutes
In most cases, the default configuration is sufficient. But for specific requirements, we might want to change these configurations. Utmost care needs to be taken in those cases to avoid unexpected consequences like wrong renew threshold calculation or delayed self-preservation mode activation.
在大多数情况下,默认配置是足够的。但对于特定的要求,我们可能想改变这些配置。在这些情况下需要非常小心,以避免出现意外的后果,如错误的更新阈值计算或延迟的自保模式激活。
4. Registering Clients
4.注册客户
Now, let’s create a Eureka Client and spin up six instances:
现在,让我们创建一个Eureka客户端并启动六个实例。
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
Here are the client’s configurations:
以下是客户的配置。
spring.application.name=Eurekaclient
server.port=${PORT:0}
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.instance.preferIpAddress=true
eureka.instance.lease-renewal-interval-in-seconds=30
This configuration allows us to spin up multiple instances of the same client with the PORT program argument. The configuration eureka.instance.lease-renewal-interval-in-seconds indicates the interval of heartbeats that the client sends to the server. The default value is 30 seconds which means that the client will send one heartbeat every 30 seconds.
这个配置允许我们用PORT程序参数来启动同一个客户端的多个实例。 配置eureka.instance.lease-renewal-interval-in-seconds表示客户端向服务器发送心跳的时间间隔。默认值是30秒,这意味着客户端将每30秒发送一次心跳。
Let’s now start these six client instances with the port numbers starting from 8081 to 8086 and navigate to http://localhost:8761 to inspect whether these instances are registered with the Eureka server.
现在让我们启动这六个客户端实例,端口号从8081到8086开始,并导航到http://localhost:8761以检查这些实例是否在Eureka服务器上注册。
From the screenshot, we can see that our Eureka server has six registered client instances and the total renewal threshold is 11. The threshold calculation is based on three factors:
从截图中,我们可以看到,我们的Eureka服务器有6个注册的客户实例,总的更新阈值是11。阈值的计算是基于三个因素的。
- Total number of registered client instances – 6
- Configured client renewal interval – 30 seconds
- The configured renewal percentage threshold – 0.85
Considering all these factors, in our case, the threshold is 11.
考虑到所有这些因素,在我们的案例中,门槛是11。
5. Testing Self-Preservation
5 测试自我保护能力
In order to simulate a temporary network problem, let’s set the property eureka.client.should-unregister-on-shutdown as false at the client-side and stop one of our client instances. Because we set the should-unregister-on-shutdown flag as false, the client won’t invoke the unregister call and the server assumes that this is an ungraceful shutdown.
为了模拟一个临时的网络问题,让我们在客户端将属性eureka.client.should-unregister-on-shutdown设置为false并停止我们的一个客户端实例。因为我们将should-unregister-on-shutdown标志设置为false,客户端不会调用unregister调用,服务器会认为这是一次不体面的关闭。
Let’s now wait for 90 seconds, set by our eureka.instance.lease-expiration-duration-in-seconds property, and navigate again to http://localhost:8761. The red bold text indicates that the Eureka Server is now in self-preservation mode and stopped evicting instances.
现在让我们等待90秒,由我们的eureka.instance.lease-expiration-duration-in-seconds属性设置,并再次导航到http://localhost:8761。红色粗体字表示Eureka服务器现在处于自我保护模式,并停止驱逐实例。
Let’s now inspect the registered instances section to see if the stopped instance is still available or not. As we can see, it is available but with the status as DOWN:
现在让我们检查一下注册的实例部分,看看这个停止的实例是否仍然可用。我们可以看到,它是可用的,但是状态是DOWN。
The only way the server can get out of self-preservation mode is either by starting the stopped instance or by disabling self-preservation itself. If we repeat the same steps by setting the flag eureka.server.enable-self-preservation as false, then the Eureka server will evict the stopped instance from the registry after the configured lease expiration duration property.
服务器可以脱离自我保护模式的唯一方法是启动已停止的实例或禁用自我保护本身。如果我们重复同样的步骤,将标志eureka.server.enable-self-preservation设置为false,那么Eureka服务器将在配置的租赁到期期限属性后从注册表中驱逐已停止的实例。
6. Conclusion
6.结论
In this tutorial, we’ve learned how Eureka self-preservation works and how can we configure different options related to self-preservation.
在本教程中,我们已经了解了Eureka自我保护的工作原理,以及如何配置与自我保护有关的不同选项。
All the examples we’ve demonstrated here can be found over on GitHub.
我们在这里演示的所有例子都可以在GitHub上找到over。