Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shut down client on stop. #4213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -57,6 +58,10 @@ public class CloudEurekaClient extends DiscoveryClient {

private final AtomicReference<EurekaHttpClient> 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);
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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;
}

}