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();
} }
if (preferIp) {
return append(append(createLocalUri(address.getHostAddress(), managementPort),
server.getContextPath()), management.getContextPath());
public String getManagementUrl() {
if (managementUrl != null) {
return managementUrl;
} }
return append(createLocalUri(address.getCanonicalHostName(), managementPort),
management.getContextPath()); if (managementPort == null || managementPort.equals(serverPort)) {
return UriComponentsBuilder.fromHttpUrl(getServiceUrl())
.pathSegment(server.getServletPrefix())
.pathSegment(trimLeadingCharacter(management.getContextPath(), '/'))
.toUriString();
} }
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) {
this.healthUrl = healthUrl;
}
public String getServiceUrl() {
if (serviceUrl != null) {
return serviceUrl;
} }
if (serverPort == null) { public void setManagementUrl(String managementUrl) {
throw new IllegalStateException( this.managementUrl = managementUrl;
"serviceUrl must be set when deployed to servlet-container");
} }
InetAddress address = getHostAddress(); public void setHealthUrl(String healthUrl) {
if (preferIp) { this.healthUrl = healthUrl;
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 append(String uri, String path) { private String getHost(InetAddress address) {
String baseUri = uri.replaceFirst("/+$", ""); return preferIp ? address.getHostAddress() : address.getCanonicalHostName();
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("/+$", ""); 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);
} }
} }
} }
package de.codecentric.boot.admin.config; package de.codecentric.boot.admin.config;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import java.net.InetAddress; import java.net.InetAddress;
...@@ -11,11 +13,15 @@ import java.net.UnknownHostException; ...@@ -11,11 +13,15 @@ import java.net.UnknownHostException;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.SpringApplication; 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.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class AdminClientPropertiesTest { public class AdminClientPropertiesTest {
...@@ -33,8 +39,7 @@ public class AdminClientPropertiesTest { ...@@ -33,8 +39,7 @@ public class AdminClientPropertiesTest {
public void test_mgmtPortPath() { public void test_mgmtPortPath() {
load("management.contextPath=/admin", "endpoints.health.id=alive", "local.server.port=8080", load("management.contextPath=/admin", "endpoints.health.id=alive", "local.server.port=8080",
"local.management.port=8081"); "local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -48,8 +53,7 @@ public class AdminClientPropertiesTest { ...@@ -48,8 +53,7 @@ public class AdminClientPropertiesTest {
@Test @Test
public void test_contextPath_mgmtPath() { public void test_contextPath_mgmtPath() {
load("server.context-path=app", "management.context-path=/admin", "local.server.port=8080"); load("server.context-path=app", "management.context-path=/admin", "local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -64,8 +68,7 @@ public class AdminClientPropertiesTest { ...@@ -64,8 +68,7 @@ public class AdminClientPropertiesTest {
public void test_contextPatht_mgmtPortPath() { public void test_contextPatht_mgmtPortPath() {
load("server.context-path=app", "management.context-path=/admin", "local.server.port=8080", load("server.context-path=app", "management.context-path=/admin", "local.server.port=8080",
"local.management.port=8081"); "local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -79,8 +82,7 @@ public class AdminClientPropertiesTest { ...@@ -79,8 +82,7 @@ public class AdminClientPropertiesTest {
@Test @Test
public void test_contextPath() { public void test_contextPath() {
load("server.context-path=app", "local.server.port=80"); load("server.context-path=app", "local.server.port=80");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -93,8 +95,7 @@ public class AdminClientPropertiesTest { ...@@ -93,8 +95,7 @@ public class AdminClientPropertiesTest {
@Test @Test
public void test_servletPath() { public void test_servletPath() {
load("server.servlet-path=app", "server.context-path=srv", "local.server.port=80"); load("server.servlet-path=app", "server.context-path=srv", "local.server.port=80");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -108,8 +109,7 @@ public class AdminClientPropertiesTest { ...@@ -108,8 +109,7 @@ public class AdminClientPropertiesTest {
@Test @Test
public void test_default() { public void test_default() {
load("local.server.port=8080"); load("local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -119,11 +119,10 @@ public class AdminClientPropertiesTest { ...@@ -119,11 +119,10 @@ public class AdminClientPropertiesTest {
} }
@Test @Test
public void testSsl() { public void test_ssl() {
load("server.ssl.key-store=somefile.jks", "server.ssl.key-store-password=password", load("server.ssl.key-store=somefile.jks", "server.ssl.key-store-password=password",
"local.server.port=8080"); "local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -135,10 +134,8 @@ public class AdminClientPropertiesTest { ...@@ -135,10 +134,8 @@ public class AdminClientPropertiesTest {
@Test @Test
public void test_preferIpAddress_serveraddress_missing() { public void test_preferIpAddress_serveraddress_missing() {
load("local.server.port=8080"); load("spring.boot.admin.client.prefer-ip=true", "local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
clientProperties.setPreferIp(true);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -148,10 +145,9 @@ public class AdminClientPropertiesTest { ...@@ -148,10 +145,9 @@ public class AdminClientPropertiesTest {
@Test @Test
public void test_preferIpAddress_managementaddress_missing() { public void test_preferIpAddress_managementaddress_missing() {
load("local.server.port=8080", "local.management.port=8081"); load("spring.boot.admin.client.prefer-ip=true", "local.server.port=8080",
AdminClientProperties clientProperties = new AdminClientProperties(); "local.management.port=8081");
clientProperties.setPreferIp(true); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
assertTrue(clientProperties.getManagementUrl() assertTrue(clientProperties.getManagementUrl()
...@@ -160,11 +156,10 @@ public class AdminClientPropertiesTest { ...@@ -160,11 +156,10 @@ public class AdminClientPropertiesTest {
@Test @Test
public void test_preferIpAddress() { 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"); "local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
clientProperties.setPreferIp(true);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -176,8 +171,7 @@ public class AdminClientPropertiesTest { ...@@ -176,8 +171,7 @@ public class AdminClientPropertiesTest {
@Test @Test
public void test_serveraddress() { public void test_serveraddress() {
load("server.address=127.0.0.2", "local.server.port=8080"); load("server.address=127.0.0.2", "local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -190,8 +184,7 @@ public class AdminClientPropertiesTest { ...@@ -190,8 +184,7 @@ public class AdminClientPropertiesTest {
public void test_managementaddress() { public void test_managementaddress() {
load("server.address=127.0.0.2", "management.address=127.0.0.3", "local.server.port=8080", load("server.address=127.0.0.2", "management.address=127.0.0.3", "local.server.port=8080",
"local.management.port=8081"); "local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties(); AdminClientProperties clientProperties = context.getBean(AdminClientProperties.class);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties); publishApplicationReadyEvent(clientProperties);
...@@ -200,6 +193,48 @@ public class AdminClientPropertiesTest { ...@@ -200,6 +193,48 @@ public class AdminClientPropertiesTest {
assertThat(clientProperties.getHealthUrl(), is("http://127.0.0.3:8081/health")); 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() { private String getHostname() {
try { try {
return InetAddress.getLocalHost().getCanonicalHostName(); return InetAddress.getLocalHost().getCanonicalHostName();
...@@ -218,9 +253,17 @@ public class AdminClientPropertiesTest { ...@@ -218,9 +253,17 @@ public class AdminClientPropertiesTest {
applicationContext.register(PropertyPlaceholderAutoConfiguration.class); applicationContext.register(PropertyPlaceholderAutoConfiguration.class);
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(TesConfig.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment); EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh(); applicationContext.refresh();
this.context = applicationContext; this.context = applicationContext;
} }
@Configuration
@EnableConfigurationProperties({ AdminClientProperties.class })
public static class TesConfig {
}
} }
...@@ -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