Commit 91e79dea by Johannes Edmeier

Polish AdminClientProperties

parent e8d80584
...@@ -15,17 +15,20 @@ ...@@ -15,17 +15,20 @@
*/ */
package de.codecentric.boot.admin.config; package de.codecentric.boot.admin.config;
import static org.springframework.util.StringUtils.trimLeadingCharacter;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties; import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.util.StringUtils; import org.springframework.web.util.UriComponentsBuilder;
@ConfigurationProperties(prefix = "spring.boot.admin.client") @ConfigurationProperties(prefix = "spring.boot.admin.client")
public class AdminClientProperties { public class AdminClientProperties {
...@@ -53,15 +56,15 @@ public class AdminClientProperties { ...@@ -53,15 +56,15 @@ public class AdminClientProperties {
@Value("${spring.application.name:spring-boot-application}") @Value("${spring.application.name:spring-boot-application}")
private String name; private String name;
@Value("${endpoints.health.id:health}")
private String healthEndpointId;
/** /**
* Should the registered urls be built with server.address or with hostname. * Should the registered urls be built with server.address or with hostname.
*/ */
private boolean preferIp = false; private boolean preferIp = false;
@Autowired @Autowired
private HealthMvcEndpoint healthEndpoint;
@Autowired
private ManagementServerProperties management; private ManagementServerProperties management;
@Autowired @Autowired
...@@ -79,68 +82,50 @@ public class AdminClientProperties { ...@@ -79,68 +82,50 @@ public class AdminClientProperties {
.getProperty("local.management.port", Integer.class, serverPort); .getProperty("local.management.port", Integer.class, serverPort);
} }
public String getManagementUrl() { public String getServiceUrl() {
if (managementUrl != null) { if (serviceUrl != null) {
return managementUrl; return serviceUrl;
}
if ((managementPort == null || managementPort.equals(serverPort))
&& getServiceUrl() != null) {
return append(append(getServiceUrl(), server.getServletPrefix()),
management.getContextPath());
} }
if (managementPort == null) { if (serverPort == null) {
throw new IllegalStateException( throw new IllegalStateException(
"serviceUrl must be set when deployed to servlet-container"); "serviceUrl must be set when deployed to servlet-container");
} }
InetAddress address = management.getAddress(); return UriComponentsBuilder.newInstance().scheme(getScheme()).host(getServiceHost())
if (address == null) { .port(serverPort).path(server.getContextPath()).toUriString();
address = getHostAddress(); }
public String getManagementUrl() {
if (managementUrl != null) {
return managementUrl;
} }
if (preferIp) {
return append(append(createLocalUri(address.getHostAddress(), managementPort),
server.getContextPath()), management.getContextPath());
if (managementPort == null || managementPort.equals(serverPort)) {
return UriComponentsBuilder.fromHttpUrl(getServiceUrl())
.pathSegment(server.getServletPrefix())
.pathSegment(trimLeadingCharacter(management.getContextPath(), '/'))
.toUriString();
} }
return append(createLocalUri(address.getCanonicalHostName(), managementPort),
management.getContextPath());
}
public void setManagementUrl(String managementUrl) { return UriComponentsBuilder.newInstance().scheme(getScheme()).host(getManagementHost())
this.managementUrl = managementUrl; .port(managementPort).path(management.getContextPath()).toUriString();
} }
public String getHealthUrl() { public String getHealthUrl() {
if (healthUrl != null) { if (healthUrl != null) {
return healthUrl; return healthUrl;
} }
return append(getManagementUrl(), healthEndpointId); return UriComponentsBuilder.fromHttpUrl(getManagementUrl())
.pathSegment(trimLeadingCharacter(healthEndpoint.getPath(), '/')).toUriString();
} }
public void setHealthUrl(String healthUrl) { public void setManagementUrl(String managementUrl) {
this.healthUrl = healthUrl; this.managementUrl = managementUrl;
} }
public String getServiceUrl() { public void setHealthUrl(String healthUrl) {
if (serviceUrl != null) { this.healthUrl = healthUrl;
return serviceUrl;
}
if (serverPort == null) {
throw new IllegalStateException(
"serviceUrl must be set when deployed to servlet-container");
}
InetAddress address = getHostAddress();
if (preferIp) {
return append(createLocalUri(address.getHostAddress(), serverPort),
server.getContextPath());
}
return append(createLocalUri(address.getCanonicalHostName(), serverPort),
server.getContextPath());
} }
public void setServiceUrl(String serviceUrl) { public void setServiceUrl(String serviceUrl) {
...@@ -163,27 +148,36 @@ public class AdminClientProperties { ...@@ -163,27 +148,36 @@ public class AdminClientProperties {
return preferIp; return preferIp;
} }
private String createLocalUri(String host, int port) { private String getScheme() {
String scheme = server.getSsl() != null && server.getSsl().isEnabled() ? "https" : "http"; return server.getSsl() != null && server.getSsl().isEnabled() ? "https" : "http";
return scheme + "://" + host + ":" + port; }
private String getHost(InetAddress address) {
return preferIp ? address.getHostAddress() : address.getCanonicalHostName();
} }
private String append(String uri, String path) { private String getServiceHost() {
String baseUri = uri.replaceFirst("/+$", ""); InetAddress address = server.getAddress();
if (StringUtils.isEmpty(path)) { if (address == null) {
return baseUri; address = getLocalHost();
} }
return getHost(address);
}
String normPath = path.replaceFirst("^/+", "").replaceFirst("/+$", ""); private String getManagementHost() {
return baseUri + "/" + normPath; InetAddress address = management.getAddress();
if (address != null) {
return getHost(address);
}
return getServiceHost();
} }
private InetAddress getHostAddress() { private InetAddress getLocalHost() {
try { try {
InetAddress address = server.getAddress(); return InetAddress.getLocalHost();
return address != null ? address : InetAddress.getLocalHost();
} catch (UnknownHostException ex) { } catch (UnknownHostException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex); throw new IllegalArgumentException(ex.getMessage(), ex);
} }
} }
} }
...@@ -4,6 +4,8 @@ import static org.junit.Assert.assertTrue; ...@@ -4,6 +4,8 @@ import static org.junit.Assert.assertTrue;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
...@@ -45,6 +47,8 @@ public class SpringBootAdminClientAutoConfigurationTest { ...@@ -45,6 +47,8 @@ public class SpringBootAdminClientAutoConfigurationTest {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(ServerPropertiesAutoConfiguration.class); applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(ManagementServerPropertiesAutoConfiguration.class); applicationContext.register(ManagementServerPropertiesAutoConfiguration.class);
applicationContext.register(EndpointAutoConfiguration.class);
applicationContext.register(EndpointWebMvcManagementContextConfiguration.class);
applicationContext.register(SpringBootAdminClientAutoConfiguration.class); applicationContext.register(SpringBootAdminClientAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment); EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh(); applicationContext.refresh();
......
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