Skip to content

Commit 7fe777d

Browse files
author
Johannes Stelzer
committed
Add spring.boot.admin.client.preferIp
In conjuntion with server.address / management.address this plays more nicely with interfaces with multiple ipAdrresses. Also remove spring.boot.admin.client.useAddressOf. fixes #112
1 parent 8bed0d1 commit 7fe777d

File tree

3 files changed

+107
-141
lines changed

3 files changed

+107
-141
lines changed

spring-boot-admin-starter-client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The main service that is used is a registrar that registeres the application at
2121
| spring.boot.admin.client.managementUrl | Client-management-URL to register with. Can be overriden in case the reachable URL is different (e.g. Docker). Must be unique in registry.<br>Default: is guessed based on serviceUrl management.port and management.context-path|
2222
| spring.boot.admin.client.healthUrl | Client-management-URL to register with. Can be overriden in case the reachable URL is different (e.g. Docker). Must be unique in registry.<br>Default: is guessed based on managementUrl and endpoints.health.id |
2323
| spring.boot.admin.client.name | Name to register with. Defaults to the ApplicationContexts name. Only set when it should differ.<br>Default: _${spring.application.name}_ if set, spring-boot-application otherwise. |
24-
| spring.boot.admin.client.useIpAddressOf | If an network-interface name is specified, its ip-address wil be used for the guessed url (instead of hostname).|
24+
| spring.boot.admin.client.preferIpAddress | Use the ip-address rather then the hostname in the guessed urls. It's required to set `server.address` and `management.address`respectively. |
2525

2626
### Other configuration properties
2727
Options from other spring boot features. These should be set to enable all features.

spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/config/AdminClientProperties.java

Lines changed: 44 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
package de.codecentric.boot.admin.config;
1717

1818
import java.net.InetAddress;
19-
import java.net.InterfaceAddress;
20-
import java.net.NetworkInterface;
21-
import java.net.SocketException;
2219
import java.net.UnknownHostException;
2320

2421
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,27 +31,27 @@
3431
import org.springframework.context.event.ContextRefreshedEvent;
3532
import org.springframework.core.Ordered;
3633
import org.springframework.core.annotation.Order;
34+
import org.springframework.util.Assert;
3735
import org.springframework.util.StringUtils;
3836

3937
@ConfigurationProperties(prefix = "spring.boot.admin.client", ignoreUnknownFields = false)
4038
@Order(Ordered.LOWEST_PRECEDENCE - 100)
4139
public class AdminClientProperties implements ApplicationListener<ApplicationEvent> {
42-
4340
/**
44-
* Client-management-URL to register with. Inferred at runtime, can be overriden in
45-
* case the reachable URL is different (e.g. Docker).
41+
* Client-management-URL to register with. Inferred at runtime, can be overriden in case the
42+
* reachable URL is different (e.g. Docker).
4643
*/
4744
private String managementUrl;
4845

4946
/**
50-
* Client-service-URL register with. Inferred at runtime, can be overriden in case the
51-
* reachable URL is different (e.g. Docker).
47+
* Client-service-URL register with. Inferred at runtime, can be overriden in case the reachable
48+
* URL is different (e.g. Docker).
5249
*/
5350
private String serviceUrl;
5451

5552
/**
56-
* Client-health-URL to register with. Inferred at runtime, can be overriden in case
57-
* the reachable URL is different (e.g. Docker). Must be unique in registry.
53+
* Client-health-URL to register with. Inferred at runtime, can be overriden in case the
54+
* reachable URL is different (e.g. Docker). Must be unique in registry.
5855
*/
5956
private String healthUrl;
6057

@@ -68,10 +65,9 @@ public class AdminClientProperties implements ApplicationListener<ApplicationEve
6865
private String healthEndpointId;
6966

7067
/**
71-
* If set, the address of the specified interface is used in url inference
72-
* instead of the hostname.
68+
* Should the registered urls be built with server.address or with hostname.
7369
*/
74-
private String useIpAddressOf = null;
70+
private boolean preferIp = false;
7571

7672
@Autowired
7773
private ManagementServerProperties management;
@@ -117,45 +113,55 @@ private boolean startedDeployedWar(ApplicationEvent event) {
117113
}
118114

119115
public String getManagementUrl() {
120-
if (managementUrl == null) {
121-
if (managementPort != -1) {
122-
return createLocalUri(managementPort,
123-
management.getContextPath());
124-
}
125-
else {
126-
return append(getServiceUrl(), management.getContextPath());
127-
}
116+
if (managementUrl != null) {
117+
return managementUrl;
128118
}
119+
if (managementPort == -1) {
120+
return append(getServiceUrl(), management.getContextPath());
121+
}
122+
123+
if (preferIp) {
124+
Assert.notNull(management.getAddress(),
125+
"management.address must be set when using preferIp");
126+
return append(createLocalUri(management.getAddress().getHostAddress(), managementPort),
127+
management.getContextPath());
129128

130-
return managementUrl;
129+
}
130+
return append(createLocalUri(getHostname(), managementPort), management.getContextPath());
131131
}
132132

133133
public void setManagementUrl(String managementUrl) {
134134
this.managementUrl = managementUrl;
135135
}
136136

137137
public String getHealthUrl() {
138-
if (healthUrl == null) {
139-
return append(getManagementUrl(), healthEndpointId);
138+
if (healthUrl != null) {
139+
return healthUrl;
140140
}
141-
return healthUrl;
141+
return append(getManagementUrl(), healthEndpointId);
142142
}
143143

144144
public void setHealthUrl(String healthUrl) {
145145
this.healthUrl = healthUrl;
146146
}
147147

148148
public String getServiceUrl() {
149-
if (serviceUrl == null) {
150-
if (serverPort != -1){
151-
return createLocalUri(serverPort, server.getContextPath());
152-
} else {
153-
throw new IllegalStateException(
154-
"EmbeddedServletContainer has not been initialized yet!");
155-
}
149+
if (serviceUrl != null) {
150+
return serviceUrl;
156151
}
157152

158-
return serviceUrl;
153+
if (serverPort == -1) {
154+
throw new IllegalStateException(
155+
"EmbeddedServletContainer has not been initialized yet!");
156+
}
157+
158+
if (preferIp) {
159+
Assert.notNull(server.getAddress(), "server.address must be set when using preferIp");
160+
return append(createLocalUri(server.getAddress().getHostAddress(), serverPort),
161+
server.getContextPath());
162+
163+
}
164+
return append(createLocalUri(getHostname(), serverPort), server.getContextPath());
159165
}
160166

161167
public void setServiceUrl(String serviceUrl) {
@@ -174,14 +180,13 @@ public void setName(String name) {
174180
this.name = name;
175181
}
176182

177-
public void setUseIpAddressOf(String useIpAddressOf) {
178-
this.useIpAddressOf = useIpAddressOf;
183+
public void setPreferIp(boolean preferIp) {
184+
this.preferIp = preferIp;
179185
}
180186

181-
private String createLocalUri(int port, String path) {
182-
String scheme = server.getSsl() != null && server.getSsl().isEnabled() ? "https"
183-
: "http";
184-
return append(scheme + "://" + getHost() + ":" + port, path);
187+
private String createLocalUri(String host, int port) {
188+
String scheme = server.getSsl() != null && server.getSsl().isEnabled() ? "https" : "http";
189+
return scheme + "://" + host + ":" + port;
185190
}
186191

187192
private String append(String uri, String path) {
@@ -194,15 +199,6 @@ private String append(String uri, String path) {
194199
return baseUri + "/" + normPath;
195200
}
196201

197-
private String getHost() {
198-
if (useIpAddressOf == null) {
199-
return getHostname();
200-
} else {
201-
return getHostIp();
202-
203-
}
204-
}
205-
206202
private String getHostname() {
207203
try {
208204
return InetAddress.getLocalHost().getCanonicalHostName();
@@ -211,47 +207,4 @@ private String getHostname() {
211207
}
212208
}
213209

214-
private String getHostIp() {
215-
NetworkInterface nic;
216-
try {
217-
nic = NetworkInterface.getByName(useIpAddressOf);
218-
} catch (SocketException ex) {
219-
throw new IllegalArgumentException(ex.getMessage(), ex);
220-
}
221-
222-
if (nic != null) {
223-
InetAddress address = findIp(nic);
224-
if (address != null) {
225-
return address.getHostAddress();
226-
}
227-
228-
throw new IllegalStateException(
229-
"Couldn't determin InetAdress for network interface '"
230-
+ useIpAddressOf + "'");
231-
} else {
232-
throw new IllegalArgumentException(
233-
"Network interface"
234-
+ useIpAddressOf
235-
+ " not found! Please specify correct interface for spring.boot.admin.client.useIpAddressOf");
236-
}
237-
}
238-
239-
private InetAddress findIp(NetworkInterface nic) {
240-
InetAddress candidate = null;
241-
242-
for (InterfaceAddress address : nic.getInterfaceAddresses()) {
243-
if (!address.getAddress().isLoopbackAddress()) {
244-
if (address.getAddress().isSiteLocalAddress()) {
245-
return address.getAddress();
246-
}
247-
candidate = address.getAddress();
248-
}
249-
}
250-
251-
if (candidate != null) {
252-
return candidate;
253-
}
254-
255-
return null;
256-
}
257210
}

0 commit comments

Comments
 (0)