Commit 991119e9 by Niklas Herder Committed by Johannes Edmeier

Remove stale apps from registry.

Checks the DiscoveryClient's services on each heartbeat and removes all services, which are no longer registered.
parent c9226b46
......@@ -77,26 +77,42 @@ public class ApplicationDiscoveryListener {
}
protected void discover() {
final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry();
for (String serviceId : discoveryClient.getServices()) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
if (!ignoredServices.contains(serviceId)) {
register(instance);
if (!ignoredServices.contains(serviceId)) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
String applicationId = register(instance);
staleApplicationIds.remove(applicationId);
}
}
}
for (String staleApplicationId : staleApplicationIds) {
LOGGER.info("Application ({}) missing in DiscoveryClient services ",
staleApplicationId);
registry.deregister(staleApplicationId);
}
}
protected final Set<String> getAllApplicationIdsFromRegistry() {
Set<String> result = new HashSet<>();
for (Application application : registry.getApplications()) {
result.add(application.getId());
}
return result;
}
protected void register(ServiceInstance instance) {
protected String register(ServiceInstance instance) {
try {
Application application = converter.convert(instance);
if (application != null) {
registry.register(application);
return registry.register(application).getId();
} else {
LOGGER.warn("No application for service {} registered", instance);
}
} catch (Exception ex) {
LOGGER.error("Couldn't register application for service {}", instance, ex);
}
return null;
}
public void setConverter(ServiceInstanceConverter converter) {
......
......@@ -19,7 +19,9 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
......@@ -95,4 +97,24 @@ public class ApplicationDiscoveryListenerTest {
assertEquals(1, registry.getApplications().size());
}
@Test
public void deregister_removed_app() {
Object heartbeat = new Object();
List<ServiceInstance> instances = new ArrayList<>();
instances.add(new DefaultServiceInstance("service", "localhost", 80, false));
instances.add(new DefaultServiceInstance("service", "example.net", 80, false));
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(instances);
listener.onApplicationEvent(new HeartbeatEvent(new Object(), heartbeat));
assertEquals(2, registry.getApplications().size());
instances.remove(0);
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
assertEquals(1, registry.getApplications().size());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment