Commit c37279e5 by Johannes Edmeier

Add option to ignore services from discovery

fixes #205
parent e1eeb038
......@@ -293,6 +293,10 @@ TIP: When the default conversion kicks in, you can use the `spring.boot.admin.di
| spring.boot.admin.discovery.converter.health-endpoint
| Will be appended to the management-url of the discovered service when the health-url is converted by the `DefaultServiceInstanceConverter`.
| `"health"`
| spring.boot.admin.discovery.ignored-services
| This services will be ignored when using discovery and not registered as application.
|
|===
[[hazelcast-support]]
......
......@@ -49,6 +49,7 @@ public class DiscoveryClientConfiguration {
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = "spring.boot.admin.discovery")
public ApplicationDiscoveryListener applicationDiscoveryListener(
ServiceInstanceConverter serviceInstanceConverter) {
ApplicationDiscoveryListener listener = new ApplicationDiscoveryListener(discoveryClient,
......
......@@ -15,6 +15,9 @@
*/
package de.codecentric.boot.admin.discovery;
import java.util.HashSet;
import java.util.Set;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
......@@ -32,12 +35,16 @@ import de.codecentric.boot.admin.registry.ApplicationRegistry;
* @author Johannes Edmeier
*/
public class ApplicationDiscoveryListener {
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.
*/
private Set<String> ignoredServices = new HashSet<>();
public ApplicationDiscoveryListener(DiscoveryClient discoveryClient,
ApplicationRegistry registry) {
this.discoveryClient = discoveryClient;
......@@ -68,7 +75,9 @@ public class ApplicationDiscoveryListener {
protected void discover() {
for (String serviceId : discoveryClient.getServices()) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
registry.register(convert(instance));
if (!ignoredServices.contains(serviceId)) {
registry.register(convert(instance));
}
}
}
}
......@@ -80,4 +89,8 @@ public class ApplicationDiscoveryListener {
public void setConverter(ServiceInstanceConverter converter) {
this.converter = converter;
}
public void setIgnoredServices(Set<String> ignoredServices) {
this.ignoredServices = ignoredServices;
}
}
\ No newline at end of file
......@@ -51,6 +51,18 @@ public class ApplicationDiscoveryListenerTest {
}
@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(Collections.singleton("service"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
assertEquals(0, registry.getApplications().size());
}
@Test
public void test_register_and_convert() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
......
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