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 @@

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 @@
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 @@
}

EurekaHttpClient getEurekaHttpClient() {
if (this.eurekaHttpClient.get() == null) {
if (eurekaHttpClient.get() == null) {
try {
Object eurekaTransport = this.eurekaTransportField.get(this);
Object eurekaTransport = eurekaTransportField.get(this);

Check warning on line 100 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L100

Added line #L100 was not covered by tests
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));

Check warning on line 104 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L104

Added line #L104 was not covered by tests
}
catch (IllegalAccessException e) {
log.error("error getting EurekaHttpClient", e);
}
}
return this.eurekaHttpClient.get();
return eurekaHttpClient.get();

Check warning on line 110 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L110

Added line #L110 was not covered by tests
}

public void setStatus(InstanceStatus newStatus, InstanceInfo info) {
Expand All @@ -114,12 +118,37 @@
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();

Check warning on line 123 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L123

Added line #L123 was not covered by tests
log.trace("onCacheRefreshed called with count: " + newCount);
this.publisher.publishEvent(new HeartbeatEvent(this, newCount));
publisher.publishEvent(new HeartbeatEvent(this, newCount));

Check warning on line 125 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L125

Added line #L125 was not covered by tests
}
}

Check warning on line 127 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L127

Added line #L127 was not covered by tests

@Override
public void start() {
synchronized (lifecycleMonitor) {

Check warning on line 131 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L131

Added line #L131 was not covered by tests
if (!isRunning()) {
running = true;

Check warning on line 133 in spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java

View check run for this annotation

Codecov / codecov/patch

spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java#L133

Added line #L133 was not covered by tests
}
}
}

@Override
public void stop() {
synchronized (lifecycleMonitor) {
if (isRunning()) {
applicationInfoManager.refreshLeaseInfoIfRequired();
shutdown();
running = false;
}
}
}

@Override
public boolean isRunning() {
return running;
}

}