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
| `"health"`
| 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;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
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;
......@@ -46,7 +47,8 @@ public class ApplicationDiscoveryListener {
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<>();
......@@ -80,7 +82,7 @@ public class ApplicationDiscoveryListener {
protected void discover() {
final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry();
for (String serviceId : discoveryClient.getServices()) {
if (!ignoredServices.contains(serviceId)) {
if (!ignoreService(serviceId)) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
String applicationId = register(instance);
staleApplicationIds.remove(applicationId);
......@@ -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() {
Set<String> result = new HashSet<>();
for (Application application : registry.getApplications()) {
if (!ignoredServices.contains(application.getName())
if (!ignoreService(application.getName())
&& SOURCE.equals(application.getSource())) {
result.add(application.getId());
}
......
......@@ -15,11 +15,14 @@
*/
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 java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
......@@ -65,6 +68,20 @@ public class ApplicationDiscoveryListenerTest {
}
@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() {
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