Commit b039f2a9 by Johannes Edmeier

Use InetAddress.getLocalHost() on prefer-ip

In case prefer-ip is true and no server.address is set use the IP address from InetAddress.getLocalHost(); like it is done in Eureka.
parent 44afc3d7
......@@ -228,7 +228,7 @@ spring.boot.admin.password
| `${spring.application.name}` if set, `"spring-boot-application"` otherwise.
| spring.boot.admin.client.prefer-ip
| Use the ip-address rather then the hostname in the guessed urls. It's required to set `server.address` and `management.address`respectively.
| Use the ip-address rather then the hostname in the guessed urls. If `server.address` / `management.address` is set they get used otherwise the IP address returned from `InetAddress.getLocalHost()` gets used.
| `false`
|===
......
......@@ -25,12 +25,10 @@ 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.Assert;
import org.springframework.util.StringUtils;
@ConfigurationProperties(prefix = "spring.boot.admin.client")
public class AdminClientProperties {
/**
* Client-management-URL to register with. Inferred at runtime, can be overriden in case the
* reachable URL is different (e.g. Docker).
......@@ -101,16 +99,17 @@ public class AdminClientProperties {
}
if (preferIp) {
Assert.notNull(management.getAddress(),
"management.address must be set when using preferIp");
return append(
append(createLocalUri(management.getAddress().getHostAddress(), managementPort),
server.getContextPath()),
management.getContextPath());
InetAddress address = management.getAddress();
if (address == null) {
address = getHostAddress();
}
return append(append(createLocalUri(address.getHostAddress(), managementPort),
server.getContextPath()), management.getContextPath());
}
return append(
append(createLocalUri(getHostname(), managementPort), server.getContextPath()),
append(createLocalUri(getHostAddress().getCanonicalHostName(), managementPort),
server.getContextPath()),
management.getContextPath());
}
......@@ -140,12 +139,16 @@ public class AdminClientProperties {
}
if (preferIp) {
Assert.notNull(server.getAddress(), "server.address must be set when using preferIp");
return append(createLocalUri(server.getAddress().getHostAddress(), serverPort),
InetAddress address = server.getAddress();
if (address == null) {
address = getHostAddress();
}
return append(createLocalUri(address.getHostAddress(), serverPort),
server.getContextPath());
}
return append(createLocalUri(getHostname(), serverPort), server.getContextPath());
return append(createLocalUri(getHostAddress().getCanonicalHostName(), serverPort),
server.getContextPath());
}
public void setServiceUrl(String serviceUrl) {
......@@ -187,9 +190,9 @@ public class AdminClientProperties {
return baseUri + "/" + normPath;
}
private String getHostname() {
private InetAddress getHostAddress() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
return InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
......
......@@ -133,7 +133,7 @@ public class AdminClientPropertiesTest {
assertThat(clientProperties.getServiceUrl(), is("https://" + getHostname() + ":8080"));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void test_preferIpAddress_serveraddress_missing() {
load("local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
......@@ -142,10 +142,11 @@ public class AdminClientPropertiesTest {
publishApplicationReadyEvent(clientProperties);
clientProperties.getServiceUrl();
assertTrue(clientProperties.getServiceUrl()
.matches("http://\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}:8080"));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void test_preferIpAddress_managementaddress_missing() {
load("local.server.port=8080", "local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
......@@ -153,8 +154,8 @@ public class AdminClientPropertiesTest {
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishApplicationReadyEvent(clientProperties);
clientProperties.getManagementUrl();
assertTrue(clientProperties.getManagementUrl()
.matches("http://\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}:8081"));
}
@Test
......
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