Commit 21aac376 by Roi Ezra Committed by Johannes Edmeier

Support patterns spring.boot.admin.discovery.ignored-services

With this commit you can use simple patterns to match services to ignore. A pattern can look like this "foo*", "*foo", "foo*bar". closes #377
parent f6bcc878
...@@ -29,7 +29,7 @@ NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health ...@@ -29,7 +29,7 @@ NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health
| `"health"` | `"health"`
| spring.boot.admin.discovery.ignored-services | spring.boot.admin.discovery.ignored-services
| This services will be ignored when using discovery and not registered as application. | This services will be ignored when using discovery and not registered as application. Supports simple patterns (e.g. "foo*", "*bar", "foo*bar*").
| |
|=== |===
......
...@@ -27,6 +27,7 @@ import org.springframework.cloud.client.discovery.event.HeartbeatMonitor; ...@@ -27,6 +27,7 @@ import org.springframework.cloud.client.discovery.event.HeartbeatMonitor;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent; import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.util.PatternMatchUtils;
import de.codecentric.boot.admin.model.Application; import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationRegistry; import de.codecentric.boot.admin.registry.ApplicationRegistry;
...@@ -46,7 +47,8 @@ public class ApplicationDiscoveryListener { ...@@ -46,7 +47,8 @@ public class ApplicationDiscoveryListener {
private ServiceInstanceConverter converter = new DefaultServiceInstanceConverter(); private ServiceInstanceConverter converter = new DefaultServiceInstanceConverter();
/** /**
* Set of serviceIds to be ignored and not to be registered as application. * 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<>(); private Set<String> ignoredServices = new HashSet<>();
...@@ -80,7 +82,7 @@ public class ApplicationDiscoveryListener { ...@@ -80,7 +82,7 @@ public class ApplicationDiscoveryListener {
protected void discover() { protected void discover() {
final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry(); final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry();
for (String serviceId : discoveryClient.getServices()) { for (String serviceId : discoveryClient.getServices()) {
if (!ignoredServices.contains(serviceId)) { if (!ignoreService(serviceId)) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) { for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
String applicationId = register(instance); String applicationId = register(instance);
staleApplicationIds.remove(applicationId); staleApplicationIds.remove(applicationId);
...@@ -96,10 +98,19 @@ public class ApplicationDiscoveryListener { ...@@ -96,10 +98,19 @@ public class ApplicationDiscoveryListener {
} }
} }
protected boolean ignoreService(final String serviceId) {
for (String pattern: ignoredServices) {
if (PatternMatchUtils.simpleMatch(pattern, serviceId)) {
return true;
}
}
return false;
}
protected final Set<String> getAllApplicationIdsFromRegistry() { protected final Set<String> getAllApplicationIdsFromRegistry() {
Set<String> result = new HashSet<>(); Set<String> result = new HashSet<>();
for (Application application : registry.getApplications()) { for (Application application : registry.getApplications()) {
if (!ignoredServices.contains(application.getName()) if (!ignoreService(application.getName())
&& SOURCE.equals(application.getSource())) { && SOURCE.equals(application.getSource())) {
result.add(application.getId()); result.add(application.getId());
} }
......
...@@ -15,11 +15,14 @@ ...@@ -15,11 +15,14 @@
*/ */
package de.codecentric.boot.admin.discovery; 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.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.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
...@@ -65,6 +68,20 @@ public class ApplicationDiscoveryListenerTest { ...@@ -65,6 +68,20 @@ public class ApplicationDiscoveryListenerTest {
} }
@Test @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_register_and_convert() { public void test_register_and_convert() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service")); when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList( 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