Commit 11ca47c6 by Maxime RIVIERE Committed by Johannes Edmeier

Add option to whitelist services from discovery

A new property `spring.boot.admin.discovery.services` is added. It can be used to restrict the services registered via discovery to a whitelist (supporting simple patterns). Until now only blacklistin was supported. The default whitelist permits all services.
parent 71444a15
......@@ -31,6 +31,10 @@ NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health
| spring.boot.admin.discovery.ignored-services
| This services will be ignored when using discovery and not registered as application. Supports simple patterns (e.g. "foo*", "*bar", "foo*bar*").
|
| spring.boot.admin.discovery.services
| This services will be included when using discovery and registered as application. Supports simple patterns (e.g. "foo*", "*bar", "foo*bar*").
| `"*"`
|===
.Instance metadata options
......
......@@ -17,6 +17,7 @@ package de.codecentric.boot.admin.discovery;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -52,6 +53,12 @@ public class ApplicationDiscoveryListener {
*/
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;
......@@ -82,7 +89,7 @@ public class ApplicationDiscoveryListener {
protected void discover() {
final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry();
for (String serviceId : discoveryClient.getServices()) {
if (!ignoreService(serviceId)) {
if (!ignoreService(serviceId) && registerService(serviceId)) {
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
String applicationId = register(instance);
staleApplicationIds.remove(applicationId);
......@@ -99,8 +106,16 @@ public class ApplicationDiscoveryListener {
}
protected boolean ignoreService(final String serviceId) {
for (String pattern: ignoredServices) {
if (PatternMatchUtils.simpleMatch(pattern, 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;
}
}
......@@ -110,7 +125,7 @@ public class ApplicationDiscoveryListener {
protected final Set<String> getAllApplicationIdsFromRegistry() {
Set<String> result = new HashSet<>();
for (Application application : registry.getApplications()) {
if (!ignoreService(application.getName())
if (!ignoreService(application.getName()) && registerService(application.getName())
&& SOURCE.equals(application.getSource())) {
result.add(application.getId());
}
......@@ -145,4 +160,12 @@ public class ApplicationDiscoveryListener {
public Set<String> getIgnoredServices() {
return ignoredServices;
}
public Set<String> getServices() {
return services;
}
public void setServices(Set<String> services) {
this.services = services;
}
}
......@@ -61,7 +61,19 @@ public class ApplicationDiscoveryListenerTest {
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setIgnoredServices(Collections.singleton("service"));
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());
......@@ -82,6 +94,37 @@ public class ApplicationDiscoveryListenerTest {
}
@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(
......@@ -120,7 +163,7 @@ public class ApplicationDiscoveryListenerTest {
.withId("abcdef").build());
registry.register(Application.create("different-source").withHealthUrl("http://health2")
.withId("abcdef").withSource("http-api").build());
listener.setIgnoredServices(Collections.singleton("ignored"));
listener.setIgnoredServices(singleton("ignored"));
List<ServiceInstance> instances = new ArrayList<>();
instances.add(new DefaultServiceInstance("service", "localhost", 80, false));
......
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