Mastering Spring Cloud
上QQ阅读APP看书,第一时间看更新

Refreshing the registry

Let's back up for a moment to the previous sample. Self-preservation mode has been disabled, but it still takes a long time to wait on the lease cancellation by the server. There are several reasons for this. The first is that every client service sends heartbeats to the server every 30 seconds (default value), which is configurable with the eureka.instance.leaseRenewalIntervalInSeconds property. If the server doesn't receive a heartbeat, it waits 90 seconds before removing the instance from the registry and thereby cutting off traffic sent to that instance. It is configurable with the eureka.instance.leaseExpirationDurationInSeconds property. Those two parameters are set on the client side. For testing purposes, we define small values in seconds:

eureka:
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2

There is also one property that should be changed on the server side. Eureka runs the evict task in the background, which is responsible for checking whether heartbeats from the client are still being received. By default, it is fired every 60 seconds. So even if the interval of lease renewal and the duration of lease expiration are set to relatively low values, the service instance might be removed at worst after 60 seconds. The delay between the subsequent timer ticks can be configured using the evictionIntervalTimerInMs property, which is set, in contrast to properties discussed previously, in milliseconds:

eureka:
server:
enableSelfPreservation: false
evictionIntervalTimerInMs: 3000

All of the required parameters have been defined on both the client and server side. Now, we can run the discovery server again and then three instances of the client application on ports 8081, 8082, and 8083 using the -DPORT VM argument. After that, we will shut down the instances on ports 8081 and 8082 one by one, just by killing their processes. What is the result? The disabled instances are almost immediately removed from Eureka registry. Here's the log fragment from the Eureka Server:

There is still one instance available running on port 8083. The appropriate warning related to the deactivation of the self-preservation mode will be printed out on the UI dashboard. Some additional information such as lease expiration status or the number of renews during the last minute may also be interesting. By manipulating all of those properties, we are able to customize the maintenance of the expired lease removal procedure. However, it is important to ensure that defined settings would not lack the performance of a system. There are some other elements sensitive to the changes of configuration, like load balancers, gateways, and circuit breakers. Eureka prints a warning message if you disable the self-preservation mode, you can see it in the following screenshot: