Commit 880f10f0 by Johannes Edmeier

Merge branch '1.3.x'

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