Commit 37323531 by Gabor Botka Committed by Johannes Edmeier

Discovery: specify management.port via metadata

With this commit you can specify the management.port for a discovered service via the metadatamap.
parent ba760e20
......@@ -276,7 +276,7 @@ The setup is explained <<discover-clients-via-spring-cloud-discovery,above>>.
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
|===
......
......@@ -29,11 +29,13 @@ import de.codecentric.boot.admin.model.Application;
* 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
* <code>management.context-path</code> or <code>health.path</code> to the instances metadata.
* <code>management.context-path</code> or <code>management.port</code> or <code>health.path</code>
* 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 class DefaultServiceInstanceConverter implements ServiceInstanceConverter
}
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 @@ public class DefaultServiceInstanceConverter implements ServiceInstanceConverter
}
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) {
......
......@@ -45,14 +45,15 @@ public class DefaultServiceInstanceConverterTest {
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"));
}
}
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