Skip to content

Commit

Permalink
Use InetAddress.getLocalHost() on prefer-ip
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshiste committed Feb 25, 2016
1 parent 44afc3d commit b039f2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion spring-boot-admin-docs/src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`
|===

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@
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).
Expand Down Expand Up @@ -101,16 +99,17 @@ && getServiceUrl() != null) {
}

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());
}

Expand Down Expand Up @@ -140,12 +139,16 @@ public String getServiceUrl() {
}

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) {
Expand Down Expand Up @@ -187,9 +190,9 @@ private String append(String uri, String path) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void testSsl() {
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();
Expand All @@ -142,19 +142,20 @@ public void test_preferIpAddress_serveraddress_missing() {

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();
clientProperties.setPreferIp(true);
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
Expand Down

0 comments on commit b039f2a

Please sign in to comment.