Commit 69f85687 by Johannes Edmeier

Discover applications on ApplicationReadyEvent

Since there is no Heartbeat or InstanceRegisteredEvent when using the SimpleDiscoveryClient we need to listen on the ApplicationReadyEvent and register the configured instances closes #505
parent 7c3130b4
......@@ -44,7 +44,7 @@ See also the https://github.com/codecentric/spring-boot-admin/tree/master/spring
[[register-client-applications]]
=== Registering client applications ===
To register your application at the SBA Server you can either include the SBA Client or use http://projects.spring.io/spring-cloud/spring-cloud.html[Spring Cloud Discovery] (e.g. Eureka)
To register your application at the SBA Server you can either include the SBA Client or use http://projects.spring.io/spring-cloud/spring-cloud.html[Spring Cloud Discovery] (e.g. Eureka, Consul, ...). There is also a <<spring-cloud-discovery-static-config,simple option using a static configuration on the SBA Server side>>.
[[register-clients-via-spring-boot-admin]]
==== spring-boot-admin-starter-client ====
......
[[spring-cloud-discovery-support]]
=== Spring Cloud Discovery ===
The Spring Boot Admin Server can use Spring Clouds `DiscoveryClient` to discover applications. The advantage is that the clients don't have to include the `spring-boot-admin-starter-client`. You just have to add a DiscoveryClient to your admin server - everything else is done by AutoConfiguration.
The setup is explained <<discover-clients-via-spring-cloud-discovery,above>>.
==== ServiceInstanceConverter ====
The Spring Boot Admin Server can use Spring Clouds `DiscoveryClient` to discover applications. The advantage is that the clients don't have to include the `spring-boot-admin-starter-client`. You just have to add a `DiscoveryClient` implementation to your admin server - everything else is done by AutoConfiguration.
[[spring-cloud-discovery-static-config]]
==== SimpleDiscoveryClient configuration ====
Spring Boot Admin ships with the `SimpleDiscoveryClient` included. This allows you to specify client applications via configuration, without adding the SBA Client or a DiscoveryClient implementation to your monitored applications:
[source,yml]
.application.yml
----
spring:
cloud:
discovery:
client:
simple:
instances:
test:
- uri: http://instance1.intern:8080
metadata:
management.context-path: /actuator
- uri: http://instance2.intern:8080
metadata:
management.context-path: /actuator
----
==== Other DiscoveryClient implementations (Eureka, Zookeeper, Consul, ...) ====
Spring Boot Admin supports all other implementation of Spring Cloud's `DiscoveryClient`. You need to add it to the Spring Boot Admin Server and configure it properly.
An <<discover-clients-via-spring-cloud-discovery,example setup using Eureka>> is shown above.
==== Converting ServiceInstances into monitored applications ====
The information from the service registry are converted by the `ServiceInstanceConverter`. Spring Boot Admin ships with a default and Eureka converter implementation. The correct one is selected by AutoConfiguration.
......@@ -12,6 +38,28 @@ TIP: You can modify how the information from the registry is used to register th
NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health-checking, which can be set on your client using `eureka.instance.healthCheckUrl`.
.Instance metadata options
|===
| Key |Value |Default value
| user.name +
user.password
| Credentials being used to access the endpoints.
|
| management.port
| The port is substituted in the service URL and will be used for accessing the actuator endpoints.
|
| management.context-path
| The path is appended to the service URL and will be used for accessing the actuator endpoints.
| `${spring.boot.admin.discovery.converter.mangement-context-path}`
| health.path
| The path is appended to the service URL and will be used for the health-checking. Ignored by the `EurekaServiceInstanceConverter`.
| `${spring.boot.admin.discovery.converter.health-endpoint}`
|===
.Discovery configuration options
|===
| Property name |Description |Default value
......@@ -36,25 +84,3 @@ NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health
| This services will be included when using discovery and registered as application. Supports simple patterns (e.g. "foo*", "*bar", "foo*bar*").
| `"*"`
|===
.Instance metadata options
|===
| Key |Value |Default value
| user.name +
user.password
| Credentials being used to access the endpoints.
|
| management.port
| The port is substituted in the service URL and will be used for accessing the actuator endpoints.
|
| management.context-path
| The path is appended to the service URL and will be used for accessing the actuator endpoints.
| `${spring.boot.admin.discovery.converter.mangement-context-path}`
| health.path
| The path is appended to the service URL and will be used for the health-checking. Ignored by the `EurekaServiceInstanceConverter`.
| `${spring.boot.admin.discovery.converter.health-endpoint}`
|===
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
......@@ -15,12 +15,15 @@
*/
package de.codecentric.boot.admin.discovery;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
......@@ -30,142 +33,142 @@ import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.context.event.EventListener;
import org.springframework.util.PatternMatchUtils;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
/**
* Listener for Heartbeats events to publish all services to the application registry.
*
* @author Johannes Edmeier
*/
public class ApplicationDiscoveryListener {
private static final Logger LOGGER = LoggerFactory
.getLogger(ApplicationDiscoveryListener.class);
private static final String SOURCE = "discovery";
private final DiscoveryClient discoveryClient;
private final ApplicationRegistry registry;
private final HeartbeatMonitor monitor = new HeartbeatMonitor();
private ServiceInstanceConverter converter = new DefaultServiceInstanceConverter();
/**
* Set of serviceIds to be ignored and not to be registered as application. Supports simple
* patterns (e.g. "foo*", "*foo", "foo*bar").
*/
private Set<String> ignoredServices = new HashSet<>();
/**
* Set of serviceIds that has to match to be registered as application. Supports simple
* patterns (e.g. "foo*", "*foo", "foo*bar"). Default value is everything
*/
private Set<String> services = new HashSet<>(Collections.singletonList("*"));
public ApplicationDiscoveryListener(DiscoveryClient discoveryClient,
ApplicationRegistry registry) {
this.discoveryClient = discoveryClient;
this.registry = registry;
}
@EventListener
public void onInstanceRegistered(InstanceRegisteredEvent<?> event) {
discover();
}
@EventListener
public void onParentHeartbeat(ParentHeartbeatEvent event) {
discoverIfNeeded(event.getValue());
}
@EventListener
public void onApplicationEvent(HeartbeatEvent event) {
discoverIfNeeded(event.getValue());
}
private void discoverIfNeeded(Object value) {
if (this.monitor.update(value)) {
discover();
}
}
protected void discover() {
final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry();
for (String serviceId : discoveryClient.getServices()) {
if (!ignoreService(serviceId) && registerService(serviceId)) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
String applicationId = register(instance);
staleApplicationIds.remove(applicationId);
}
} else {
LOGGER.debug("Ignoring discovered service {}", serviceId);
}
}
for (String staleApplicationId : staleApplicationIds) {
LOGGER.info("Application ({}) missing in DiscoveryClient services ",
staleApplicationId);
registry.deregister(staleApplicationId);
}
}
protected boolean ignoreService(final String serviceId) {
return checkPatternIsMatching(serviceId, ignoredServices);
}
protected boolean registerService(final String serviceId) {
return checkPatternIsMatching(serviceId, services);
}
protected boolean checkPatternIsMatching(String serviceId, Set<String> patterns) {
for (String pattern : patterns) {
if(PatternMatchUtils.simpleMatch(pattern, serviceId)) {
return true;
}
}
return false;
}
protected final Set<String> getAllApplicationIdsFromRegistry() {
Set<String> result = new HashSet<>();
for (Application application : registry.getApplications()) {
if (!ignoreService(application.getName()) && registerService(application.getName())
&& SOURCE.equals(application.getSource())) {
result.add(application.getId());
}
}
return result;
}
protected String register(ServiceInstance instance) {
try {
Application application = converter.convert(instance);
application = Application.copyOf(application).withSource(SOURCE).build();
if (application != null) {
LOGGER.debug("Registering discovered application {}", 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) {
this.converter = converter;
}
public void setIgnoredServices(Set<String> ignoredServices) {
this.ignoredServices = ignoredServices;
}
public Set<String> getIgnoredServices() {
return ignoredServices;
}
public Set<String> getServices() {
return services;
}
public void setServices(Set<String> services) {
this.services = services;
}
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationDiscoveryListener.class);
private static final String SOURCE = "discovery";
private final DiscoveryClient discoveryClient;
private final ApplicationRegistry registry;
private final HeartbeatMonitor monitor = new HeartbeatMonitor();
private ServiceInstanceConverter converter = new DefaultServiceInstanceConverter();
/**
* Set of serviceIds to be ignored and not to be registered as application. Supports simple
* patterns (e.g. "foo*", "*foo", "foo*bar").
*/
private Set<String> ignoredServices = new HashSet<>();
/**
* Set of serviceIds that has to match to be registered as application. Supports simple
* patterns (e.g. "foo*", "*foo", "foo*bar"). Default value is everything
*/
private Set<String> services = new HashSet<>(Collections.singletonList("*"));
public ApplicationDiscoveryListener(DiscoveryClient discoveryClient, ApplicationRegistry registry) {
this.discoveryClient = discoveryClient;
this.registry = registry;
}
@EventListener
public void onInstanceRegistered(InstanceRegisteredEvent<?> event) {
discover();
}
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
discover();
}
@EventListener
public void onParentHeartbeat(ParentHeartbeatEvent event) {
discoverIfNeeded(event.getValue());
}
@EventListener
public void onHeartbeat(HeartbeatEvent event) {
discoverIfNeeded(event.getValue());
}
private void discoverIfNeeded(Object value) {
if (this.monitor.update(value)) {
discover();
}
}
protected void discover() {
final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry();
for (String serviceId : discoveryClient.getServices()) {
if (!ignoreService(serviceId) && registerService(serviceId)) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
String applicationId = register(instance);
staleApplicationIds.remove(applicationId);
}
} else {
LOGGER.debug("Ignoring discovered service {}", serviceId);
}
}
for (String staleApplicationId : staleApplicationIds) {
LOGGER.info("Application ({}) missing in DiscoveryClient services ", staleApplicationId);
registry.deregister(staleApplicationId);
}
}
protected boolean ignoreService(final String serviceId) {
return checkPatternIsMatching(serviceId, ignoredServices);
}
protected boolean registerService(final String serviceId) {
return checkPatternIsMatching(serviceId, services);
}
protected boolean checkPatternIsMatching(String serviceId, Set<String> patterns) {
for (String pattern : patterns) {
if (PatternMatchUtils.simpleMatch(pattern, serviceId)) {
return true;
}
}
return false;
}
protected final Set<String> getAllApplicationIdsFromRegistry() {
Set<String> result = new HashSet<>();
for (Application application : registry.getApplications()) {
if (!ignoreService(application.getName()) &&
registerService(application.getName()) &&
SOURCE.equals(application.getSource())) {
result.add(application.getId());
}
}
return result;
}
protected String register(ServiceInstance instance) {
try {
Application application = converter.convert(instance);
application = Application.copyOf(application).withSource(SOURCE).build();
if (application != null) {
LOGGER.debug("Registering discovered application {}", 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) {
this.converter = converter;
}
public void setIgnoredServices(Set<String> ignoredServices) {
this.ignoredServices = ignoredServices;
}
public Set<String> getIgnoredServices() {
return ignoredServices;
}
public Set<String> getServices() {
return services;
}
public void setServices(Set<String> services) {
this.services = services;
}
}
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
......@@ -15,174 +15,185 @@
*/
package de.codecentric.boot.admin.discovery;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
import de.codecentric.boot.admin.registry.HashingApplicationUrlIdGenerator;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.context.ApplicationEventPublisher;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
import de.codecentric.boot.admin.registry.HashingApplicationUrlIdGenerator;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ApplicationDiscoveryListenerTest {
private ApplicationDiscoveryListener listener;
private DiscoveryClient discovery;
private ApplicationRegistry registry;
@Before
public void setup() {
registry = new ApplicationRegistry(new SimpleApplicationStore(),
new HashingApplicationUrlIdGenerator());
registry.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
discovery = mock(DiscoveryClient.class);
listener = new ApplicationDiscoveryListener(discovery, registry);
}
@Test
public void test_ignore() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setIgnoredServices(singleton("service"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
assertEquals(0, registry.getApplications().size());
}
@Test
public void test_matching() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setServices(singleton("notService"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
assertEquals(0, registry.getApplications().size());
}
@Test
public void test_ignore_pattern() {
when(discovery.getServices()).thenReturn(asList("service", "rabbit-1", "rabbit-2"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setIgnoredServices(singleton("rabbit-*"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size());
assertEquals("service", applications.iterator().next().getName());
}
@Test
public void test_matching_pattern() {
when(discovery.getServices()).thenReturn(asList("service", "rabbit-1", "rabbit-2"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setServices(singleton("ser*"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size());
assertEquals("service", applications.iterator().next().getName());
}
@Test
public void test_matching_and_ignore_pattern() {
when(discovery.getServices()).thenReturn(asList("service-1", "service", "rabbit-1", "rabbit-2"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
when(discovery.getInstances("service-1")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service-1", "localhost", 80, false)));
listener.setServices(singleton("ser*"));
listener.setIgnoredServices(singleton("service-*"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size());
assertEquals("service", applications.iterator().next().getName());
}
@Test
public void test_register_and_convert() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
assertEquals(1, registry.getApplications().size());
Application application = registry.getApplications().iterator().next();
assertEquals("http://localhost:80/health", application.getHealthUrl());
assertEquals("http://localhost:80", application.getManagementUrl());
assertEquals("http://localhost:80", application.getServiceUrl());
assertEquals("service", application.getName());
}
@Test
public void single_discovery_for_same_heartbeat() {
Object heartbeat = new Object();
listener.onParentHeartbeat(new ParentHeartbeatEvent(new Object(), heartbeat));
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.onApplicationEvent(new HeartbeatEvent(new Object(), heartbeat));
assertEquals(0, registry.getApplications().size());
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
assertEquals(1, registry.getApplications().size());
}
@Test
public void deregister_removed_app() {
registry.register(Application.create("ignored").withHealthUrl("http://health")
.withId("abcdef").build());
registry.register(Application.create("different-source").withHealthUrl("http://health2")
.withId("abcdef").withSource("http-api").build());
listener.setIgnoredServices(singleton("ignored"));
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(), new Object()));
assertEquals(2, registry.getApplicationsByName("service").size());
assertEquals(1, registry.getApplicationsByName("ignored").size());
assertEquals(1, registry.getApplicationsByName("different-source").size());
instances.remove(0);
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
assertEquals(1, registry.getApplicationsByName("service").size());
assertEquals(1, registry.getApplicationsByName("ignored").size());
assertEquals(1, registry.getApplicationsByName("different-source").size());
}
private ApplicationDiscoveryListener listener;
private DiscoveryClient discovery;
private ApplicationRegistry registry;
@Before
public void setup() {
registry = new ApplicationRegistry(new SimpleApplicationStore(), new HashingApplicationUrlIdGenerator());
registry.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
discovery = mock(DiscoveryClient.class);
listener = new ApplicationDiscoveryListener(discovery, registry);
}
@Test
public void test_ignore() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setIgnoredServices(singleton("service"));
listener.onInstanceRegistered(null);
assertEquals(0, registry.getApplications().size());
}
@Test
public void test_application_ready() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setServices(singleton("notService"));
listener.onApplicationReady(null);
assertEquals(0, registry.getApplications().size());
}
@Test
public void test_matching() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setServices(singleton("notService"));
listener.onInstanceRegistered(null);
assertEquals(0, registry.getApplications().size());
}
@Test
public void test_ignore_pattern() {
when(discovery.getServices()).thenReturn(asList("service", "rabbit-1", "rabbit-2"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setIgnoredServices(singleton("rabbit-*"));
listener.onInstanceRegistered(null);
Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size());
assertEquals("service", applications.iterator().next().getName());
}
@Test
public void test_matching_pattern() {
when(discovery.getServices()).thenReturn(asList("service", "rabbit-1", "rabbit-2"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setServices(singleton("ser*"));
listener.onInstanceRegistered(null);
Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size());
assertEquals("service", applications.iterator().next().getName());
}
@Test
public void test_matching_and_ignore_pattern() {
when(discovery.getServices()).thenReturn(asList("service-1", "service", "rabbit-1", "rabbit-2"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
when(discovery.getInstances("service-1")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service-1", "localhost", 80, false)));
listener.setServices(singleton("ser*"));
listener.setIgnoredServices(singleton("service-*"));
listener.onInstanceRegistered(null);
Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size());
assertEquals("service", applications.iterator().next().getName());
}
@Test
public void test_register_and_convert() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.onInstanceRegistered(null);
assertEquals(1, registry.getApplications().size());
Application application = registry.getApplications().iterator().next();
assertEquals("http://localhost:80/health", application.getHealthUrl());
assertEquals("http://localhost:80", application.getManagementUrl());
assertEquals("http://localhost:80", application.getServiceUrl());
assertEquals("service", application.getName());
}
@Test
public void single_discovery_for_same_heartbeat() {
Object heartbeat = new Object();
listener.onParentHeartbeat(new ParentHeartbeatEvent(new Object(), heartbeat));
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.onHeartbeat(new HeartbeatEvent(new Object(), heartbeat));
assertEquals(0, registry.getApplications().size());
listener.onHeartbeat(new HeartbeatEvent(new Object(), new Object()));
assertEquals(1, registry.getApplications().size());
}
@Test
public void deregister_removed_app() {
registry.register(Application.create("ignored").withHealthUrl("http://health").withId("abcdef").build());
registry.register(Application.create("different-source")
.withHealthUrl("http://health2")
.withId("abcdef")
.withSource("http-api")
.build());
listener.setIgnoredServices(singleton("ignored"));
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.onHeartbeat(new HeartbeatEvent(new Object(), new Object()));
assertEquals(2, registry.getApplicationsByName("service").size());
assertEquals(1, registry.getApplicationsByName("ignored").size());
assertEquals(1, registry.getApplicationsByName("different-source").size());
instances.remove(0);
listener.onHeartbeat(new HeartbeatEvent(new Object(), new Object()));
assertEquals(1, registry.getApplicationsByName("service").size());
assertEquals(1, registry.getApplicationsByName("ignored").size());
assertEquals(1, registry.getApplicationsByName("different-source").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