From dbbde33e3cec1377952b921ab439eb27bc0b0e2d Mon Sep 17 00:00:00 2001 From: Olga MaciaszekSharma Date: Thu, 19 Oct 2023 19:54:45 +0200 Subject: [PATCH] Shut down client on stop. --- .../netflix/eureka/CloudEurekaClient.java | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java index a430feb3bb..f49a9e3290 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java @@ -34,6 +34,7 @@ import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.Lifecycle; import org.springframework.http.HttpStatus; import org.springframework.util.ReflectionUtils; @@ -43,7 +44,7 @@ * * @author Spencer Gibb */ -public class CloudEurekaClient extends DiscoveryClient { +public class CloudEurekaClient extends DiscoveryClient implements Lifecycle { private static final Log log = LogFactory.getLog(CloudEurekaClient.class); @@ -57,6 +58,10 @@ public class CloudEurekaClient extends DiscoveryClient { private final AtomicReference eurekaHttpClient = new AtomicReference<>(); + private final Object lifecycleMonitor = new Object(); + + private volatile boolean running = true; + public CloudEurekaClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, TransportClientFactories transportClientFactories, ApplicationEventPublisher publisher) { this(applicationInfoManager, config, transportClientFactories, null, publisher); @@ -68,8 +73,8 @@ public CloudEurekaClient(ApplicationInfoManager applicationInfoManager, EurekaCl super(applicationInfoManager, config, transportClientFactories, args); this.applicationInfoManager = applicationInfoManager; this.publisher = publisher; - this.eurekaTransportField = ReflectionUtils.findField(DiscoveryClient.class, "eurekaTransport"); - ReflectionUtils.makeAccessible(this.eurekaTransportField); + eurekaTransportField = ReflectionUtils.findField(DiscoveryClient.class, "eurekaTransport"); + ReflectionUtils.makeAccessible(eurekaTransportField); } public ApplicationInfoManager getApplicationInfoManager() { @@ -90,20 +95,19 @@ public InstanceInfo getInstanceInfo(String appname, String instanceId) { } EurekaHttpClient getEurekaHttpClient() { - if (this.eurekaHttpClient.get() == null) { + if (eurekaHttpClient.get() == null) { try { - Object eurekaTransport = this.eurekaTransportField.get(this); + Object eurekaTransport = eurekaTransportField.get(this); Field registrationClientField = ReflectionUtils.findField(eurekaTransport.getClass(), "registrationClient"); ReflectionUtils.makeAccessible(registrationClientField); - this.eurekaHttpClient.compareAndSet(null, - (EurekaHttpClient) registrationClientField.get(eurekaTransport)); + eurekaHttpClient.compareAndSet(null, (EurekaHttpClient) registrationClientField.get(eurekaTransport)); } catch (IllegalAccessException e) { log.error("error getting EurekaHttpClient", e); } } - return this.eurekaHttpClient.get(); + return eurekaHttpClient.get(); } public void setStatus(InstanceStatus newStatus, InstanceInfo info) { @@ -114,12 +118,37 @@ public void setStatus(InstanceStatus newStatus, InstanceInfo info) { protected void onCacheRefreshed() { super.onCacheRefreshed(); - if (this.cacheRefreshedCount != null) { // might be called during construction and + if (cacheRefreshedCount != null) { // might be called during construction and // will be null - long newCount = this.cacheRefreshedCount.incrementAndGet(); + long newCount = cacheRefreshedCount.incrementAndGet(); log.trace("onCacheRefreshed called with count: " + newCount); - this.publisher.publishEvent(new HeartbeatEvent(this, newCount)); + publisher.publishEvent(new HeartbeatEvent(this, newCount)); + } + } + + @Override + public void start() { + synchronized (lifecycleMonitor) { + if (!isRunning()) { + running = true; + } } } + @Override + public void stop() { + synchronized (lifecycleMonitor) { + if (isRunning()) { + applicationInfoManager.refreshLeaseInfoIfRequired(); + shutdown(); + running = false; + } + } + } + + @Override + public boolean isRunning() { + return running; + } + }