Commit d1879b19 by Johannes Edmeier

Merge branch '1.3.x'

parents e8d11a2e 991119e9
...@@ -77,26 +77,42 @@ public class ApplicationDiscoveryListener { ...@@ -77,26 +77,42 @@ public class ApplicationDiscoveryListener {
} }
protected void discover() { protected void discover() {
final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry();
for (String serviceId : discoveryClient.getServices()) { for (String serviceId : discoveryClient.getServices()) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) { if (!ignoredServices.contains(serviceId)) {
if (!ignoredServices.contains(serviceId)) { for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
register(instance); 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 { try {
Application application = converter.convert(instance); Application application = converter.convert(instance);
if (application != null) { if (application != null) {
registry.register(application); return registry.register(application).getId();
} else { } else {
LOGGER.warn("No application for service {} registered", instance); LOGGER.warn("No application for service {} registered", instance);
} }
} catch (Exception ex) { } catch (Exception ex) {
LOGGER.error("Couldn't register application for service {}", instance, ex); LOGGER.error("Couldn't register application for service {}", instance, ex);
} }
return null;
} }
public void setConverter(ServiceInstanceConverter converter) { public void setConverter(ServiceInstanceConverter converter) {
......
...@@ -19,7 +19,9 @@ import static org.junit.Assert.assertEquals; ...@@ -19,7 +19,9 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -95,4 +97,24 @@ public class ApplicationDiscoveryListenerTest { ...@@ -95,4 +97,24 @@ public class ApplicationDiscoveryListenerTest {
assertEquals(1, registry.getApplications().size()); 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