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