diff --git a/spring-boot-admin-docs/src/main/asciidoc/index.adoc b/spring-boot-admin-docs/src/main/asciidoc/index.adoc index b58bbdfae94..65348339422 100644 --- a/spring-boot-admin-docs/src/main/asciidoc/index.adoc +++ b/spring-boot-admin-docs/src/main/asciidoc/index.adoc @@ -281,7 +281,7 @@ The setup is explained <>. The informations from the discovered services are converted by the `ServiceInstanceConverter`. Spring Boot Admin ships with a default and Eureka converter implementation. The correct one is selected by AutoConfiguration. You can use your own conversion by implementing the interface and adding the bean to your application context. -TIP: If you want to customize the default conversion of services you can either add `health.path` and/or `mangament.context-path` entries to the services metadata. This allows you to set the health or management path per application. In case you want to configure this for all of your discovered services, you can use the `spring.boot.admin.discovery.converter.*` properties for your Spring Boot Admin Server configuration. The services' metadata takes precedence over the server configuration. For the health-url the `EurekaServiceInstanceConverter` uses the healthCheckUrl registered in Eureka, which can be set for your client via `eureka.instance.healthCheckUrl`. +TIP: If you want to customize the default conversion of services you can either add `health.path`, `management.port` and/or `mangament.context-path` entries to the services metadata. This allows you to set the health or management path per application. In case you want to configure this for all of your discovered services, you can use the `spring.boot.admin.discovery.converter.*` properties for your Spring Boot Admin Server configuration. The services' metadata takes precedence over the server configuration. For the health-url the `EurekaServiceInstanceConverter` uses the healthCheckUrl registered in Eureka, which can be set for your client via `eureka.instance.healthCheckUrl`. .Discovery configuration options |=== diff --git a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListener.java b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListener.java index 02b92b57f08..5d6c8529714 100644 --- a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListener.java +++ b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListener.java @@ -96,7 +96,9 @@ protected void discover() { protected final Set getAllApplicationIdsFromRegistry() { Set result = new HashSet<>(); for (Application application : registry.getApplications()) { - result.add(application.getId()); + if (!ignoredServices.contains(application.getName())) { + result.add(application.getId()); + } } return result; } diff --git a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/DefaultServiceInstanceConverter.java b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/DefaultServiceInstanceConverter.java index 34757195b2f..dbe1df8804d 100644 --- a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/DefaultServiceInstanceConverter.java +++ b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/DefaultServiceInstanceConverter.java @@ -29,11 +29,13 @@ * Converts any {@link ServiceInstance}s to {@link Application}s. To customize the health- or * management-url for all applications you can set healthEndpointPath or managementContextPath * respectively. If you want to influence the url per service you can add - * management.context-path or health.path to the instances metadata. + * management.context-path or management.port or health.path + * to the instances metadata. * * @author Johannes Edmeier */ public class DefaultServiceInstanceConverter implements ServiceInstanceConverter { + private static final String KEY_MANAGEMENT_PORT = "management.port"; private static final String KEY_MANAGEMENT_PATH = "management.context-path"; private static final String KEY_HEALTH_PATH = "health.path"; private String managementContextPath = ""; @@ -61,8 +63,8 @@ public Application convert(ServiceInstance instance) { } protected URI getHealthUrl(ServiceInstance instance) { - String healthPath = defaultIfEmpty( - instance.getMetadata().get(KEY_HEALTH_PATH), healthEndpointPath); + String healthPath = defaultIfEmpty(instance.getMetadata().get(KEY_HEALTH_PATH), + healthEndpointPath); healthPath = stripStart(healthPath, "/"); return UriComponentsBuilder.fromUri(getManagementUrl(instance)).pathSegment(healthPath) @@ -70,12 +72,16 @@ protected URI getHealthUrl(ServiceInstance instance) { } protected URI getManagementUrl(ServiceInstance instance) { - String managamentPath = defaultIfEmpty( - instance.getMetadata().get(KEY_MANAGEMENT_PATH), managementContextPath); + String managamentPath = defaultIfEmpty(instance.getMetadata().get(KEY_MANAGEMENT_PATH), + managementContextPath); managamentPath = stripStart(managamentPath, "/"); - return UriComponentsBuilder.fromUri(getServiceUrl(instance)).pathSegment(managamentPath) - .build().toUri(); + URI serviceUrl = getServiceUrl(instance); + String managamentPort = defaultIfEmpty(instance.getMetadata().get(KEY_MANAGEMENT_PORT), + String.valueOf(serviceUrl.getPort())); + + return UriComponentsBuilder.fromUri(serviceUrl).port(managamentPort) + .pathSegment(managamentPath).build().toUri(); } protected URI getServiceUrl(ServiceInstance instance) { diff --git a/spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListenerTest.java b/spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListenerTest.java index 402932ed684..67d1af7c3c7 100644 --- a/spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListenerTest.java +++ b/spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListenerTest.java @@ -99,7 +99,9 @@ public void single_discovery_for_same_heartbeat() { @Test public void deregister_removed_app() { - Object heartbeat = new Object(); + registry.register(Application.create("ignored").withHealthUrl("http://health") + .withId("abcdef").build()); + listener.setIgnoredServices(Collections.singleton("ignored")); List instances = new ArrayList<>(); instances.add(new DefaultServiceInstance("service", "localhost", 80, false)); @@ -108,13 +110,15 @@ public void deregister_removed_app() { when(discovery.getServices()).thenReturn(Collections.singletonList("service")); when(discovery.getInstances("service")).thenReturn(instances); - listener.onApplicationEvent(new HeartbeatEvent(new Object(), heartbeat)); - assertEquals(2, registry.getApplications().size()); + listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object())); + assertEquals(2, registry.getApplicationsByName("service").size()); + assertEquals(1, registry.getApplicationsByName("ignored").size()); instances.remove(0); listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object())); - assertEquals(1, registry.getApplications().size()); + assertEquals(1, registry.getApplicationsByName("service").size()); + assertEquals(1, registry.getApplicationsByName("ignored").size()); } } diff --git a/spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/DefaultServiceInstanceConverterTest.java b/spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/DefaultServiceInstanceConverterTest.java index 01809666ed4..446255bf60c 100644 --- a/spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/DefaultServiceInstanceConverterTest.java +++ b/spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/DefaultServiceInstanceConverterTest.java @@ -45,14 +45,15 @@ public void test_convert_with_metadata() { ServiceInstance service = new DefaultServiceInstance("test", "localhost", 80, false); service.getMetadata().put("health.path", "ping"); service.getMetadata().put("management.context-path", "mgmt"); + service.getMetadata().put("management.port", "1234"); Application application = new DefaultServiceInstanceConverter().convert(service); assertThat(application.getId(), nullValue()); assertThat(application.getName(), is("test")); assertThat(application.getServiceUrl(), is("http://localhost:80")); - assertThat(application.getManagementUrl(), is("http://localhost:80/mgmt")); - assertThat(application.getHealthUrl(), is("http://localhost:80/mgmt/ping")); + assertThat(application.getManagementUrl(), is("http://localhost:1234/mgmt")); + assertThat(application.getHealthUrl(), is("http://localhost:1234/mgmt/ping")); } }