Commit 91e79dea by Johannes Edmeier

Polish AdminClientProperties

parent e8d80584
......@@ -15,17 +15,20 @@
*/
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.util.UriComponentsBuilder;
@ConfigurationProperties(prefix = "spring.boot.admin.client")
public class AdminClientProperties {
......@@ -53,15 +56,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
......@@ -79,68 +82,50 @@ public class AdminClientProperties {
.getProperty("local.management.port", Integer.class, serverPort);
}
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");
}
InetAddress address = management.getAddress();
if (address == null) {
address = getHostAddress();
return UriComponentsBuilder.newInstance().scheme(getScheme()).host(getServiceHost())
.port(serverPort).path(server.getContextPath()).toUriString();
}
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) {
this.managementUrl = managementUrl;
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");
}
InetAddress address = getHostAddress();
if (preferIp) {
return append(createLocalUri(address.getHostAddress(), serverPort),
server.getContextPath());
}
return append(createLocalUri(address.getCanonicalHostName(), serverPort),
server.getContextPath());
public void setHealthUrl(String healthUrl) {
this.healthUrl = healthUrl;
}
public void setServiceUrl(String serviceUrl) {
......@@ -163,27 +148,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 getHost(InetAddress address) {
return preferIp ? address.getHostAddress() : address.getCanonicalHostName();
}
private String append(String uri, String path) {
String baseUri = uri.replaceFirst("/+$", "");
if (StringUtils.isEmpty(path)) {
return baseUri;
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 {
InetAddress address = server.getAddress();
return address != null ? address : InetAddress.getLocalHost();
return InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
}
}
package de.codecentric.boot.admin.config;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import java.net.InetAddress;
......@@ -11,11 +13,15 @@ import java.net.UnknownHostException;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
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.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class AdminClientPropertiesTest {
......@@ -33,8 +39,7 @@ public class AdminClientPropertiesTest {
public void test_mgmtPortPath() {
load("management.contextPath=/admin", "endpoints.health.id=alive", "local.server.port=8080",
"local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -48,8 +53,7 @@ public class AdminClientPropertiesTest {
@Test
public void test_contextPath_mgmtPath() {
load("server.context-path=app", "management.context-path=/admin", "local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -64,8 +68,7 @@ public class AdminClientPropertiesTest {
public void test_contextPatht_mgmtPortPath() {
load("server.context-path=app", "management.context-path=/admin", "local.server.port=8080",
"local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -79,8 +82,7 @@ public class AdminClientPropertiesTest {
@Test
public void test_contextPath() {
load("server.context-path=app", "local.server.port=80");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -93,8 +95,7 @@ public class AdminClientPropertiesTest {
@Test
public void test_servletPath() {
load("server.servlet-path=app", "server.context-path=srv", "local.server.port=80");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -108,8 +109,7 @@ public class AdminClientPropertiesTest {
@Test
public void test_default() {
load("local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -119,11 +119,10 @@ public class AdminClientPropertiesTest {
}
@Test
public void testSsl() {
public void test_ssl() {
load("server.ssl.key-store=somefile.jks", "server.ssl.key-store-password=password",
"local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -135,10 +134,8 @@ public class AdminClientPropertiesTest {
@Test
public void test_preferIpAddress_serveraddress_missing() {
load("local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setPreferIp(true);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
load("spring.boot.admin.client.prefer-ip=true", "local.server.port=8080");
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -148,10 +145,9 @@ public class AdminClientPropertiesTest {
@Test
public void test_preferIpAddress_managementaddress_missing() {
load("local.server.port=8080", "local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setPreferIp(true);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
load("spring.boot.admin.client.prefer-ip=true", "local.server.port=8080",
"local.management.port=8081");
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
assertTrue(clientProperties.getManagementUrl()
......@@ -160,11 +156,10 @@ public class AdminClientPropertiesTest {
@Test
public void test_preferIpAddress() {
load("server.address=127.0.0.1", "management.address=127.0.0.2", "local.server.port=8080",
load("spring.boot.admin.client.prefer-ip=true", "server.address=127.0.0.1",
"management.address=127.0.0.2", "local.server.port=8080",
"local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setPreferIp(true);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -176,8 +171,7 @@ public class AdminClientPropertiesTest {
@Test
public void test_serveraddress() {
load("server.address=127.0.0.2", "local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -190,8 +184,7 @@ public class AdminClientPropertiesTest {
public void test_managementaddress() {
load("server.address=127.0.0.2", "management.address=127.0.0.3", "local.server.port=8080",
"local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
......@@ -200,6 +193,48 @@ public class AdminClientPropertiesTest {
assertThat(clientProperties.getHealthUrl(), is("http://127.0.0.3:8081/health"));
}
@Test
public void test_allcustom() {
load("spring.boot.admin.client.service-url=http://service",
"spring.boot.admin.client.management-url=http://management",
"spring.boot.admin.client.health-url=http://health");
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
publishApplicationReadyEvent(clientProperties);
assertThat(clientProperties.getServiceUrl(), is("http://service"));
assertThat(clientProperties.getManagementUrl(), is("http://management"));
assertThat(clientProperties.getHealthUrl(), is("http://health"));
}
@Test
public void test_missingports() {
load();
AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
try {
clientProperties.getServiceUrl();
fail("IllegalStateException expected");
} catch (IllegalStateException ex) {
assertThat(ex.getMessage(), containsString("serviceUrl"));
}
try {
clientProperties.getManagementUrl();
fail("IllegalStateException expected");
} catch (IllegalStateException ex) {
assertThat(ex.getMessage(), containsString("serviceUrl"));
}
try {
clientProperties.getHealthUrl();
fail("IllegalStateException expected");
} catch (IllegalStateException ex) {
assertThat(ex.getMessage(), containsString("serviceUrl"));
}
}
private String getHostname() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
......@@ -218,9 +253,17 @@ public class AdminClientPropertiesTest {
applicationContext.register(PropertyPlaceholderAutoConfiguration.class);
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(ManagementServerPropertiesAutoConfiguration.class);
applicationContext.register(EndpointAutoConfiguration.class);
applicationContext.register(EndpointWebMvcManagementContextConfiguration.class);
applicationContext.register(TesConfig.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh();
this.context = applicationContext;
}
@Configuration
@EnableConfigurationProperties({ AdminClientProperties.class })
public static class TesConfig {
}
}
......@@ -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.test.EnvironmentTestUtils;
......@@ -45,6 +47,8 @@ public class SpringBootAdminClientAutoConfigurationTest {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(ServerPropertiesAutoConfiguration.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