Browse Source
Also adds a propertySource with those values: "springCloudClientHostInfo": { "spring.cloud.client.hostname": "myhostname", "spring.cloud.client.ipAddress": "192.168.2.112" }, fixes gh-47pull/52/merge
3 changed files with 122 additions and 0 deletions
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
package org.springframework.cloud.client; |
||||
|
||||
import java.util.LinkedHashMap; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor; |
||||
import org.springframework.boot.env.EnvironmentPostProcessor; |
||||
import org.springframework.cloud.util.InetUtils; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.core.env.ConfigurableEnvironment; |
||||
import org.springframework.core.env.MapPropertySource; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class HostInfoEnvironmentPostProcessor implements |
||||
EnvironmentPostProcessor, Ordered { |
||||
|
||||
// Before ConfigFileApplicationListener
|
||||
private int order = ConfigFileEnvironmentPostProcessor.DEFAULT_ORDER - 1; |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return order; |
||||
} |
||||
|
||||
@Override |
||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
||||
InetUtils.HostInfo hostInfo = InetUtils.getFirstNonLoopbackHostInfo(); |
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>(); |
||||
map.put("spring.cloud.client.hostname", hostInfo.getHostname()); |
||||
map.put("spring.cloud.client.ipAddress", hostInfo.getIpAddress()); |
||||
MapPropertySource propertySource = new MapPropertySource("springCloudClientHostInfo", map); |
||||
environment.getPropertySources().addLast(propertySource); |
||||
} |
||||
} |
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
package org.springframework.cloud.util; |
||||
|
||||
import lombok.Data; |
||||
import lombok.SneakyThrows; |
||||
import lombok.extern.apachecommons.CommonsLog; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.Inet4Address; |
||||
import java.net.InetAddress; |
||||
import java.net.NetworkInterface; |
||||
import java.net.UnknownHostException; |
||||
import java.util.Enumeration; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
@CommonsLog |
||||
public class InetUtils { |
||||
|
||||
/** |
||||
* Find the first non-loopback host info. |
||||
* If there were errors return a hostinfo with 'localhost' and '127.0.0.1' for hostname and ipAddress respectively. |
||||
*/ |
||||
public static HostInfo getFirstNonLoopbackHostInfo() { |
||||
InetAddress address = getFirstNonLoopbackAddress(); |
||||
if (address != null) { |
||||
return convert(address); |
||||
} |
||||
HostInfo hostInfo = new HostInfo(); |
||||
hostInfo.setHostname("localhost"); |
||||
hostInfo.setIpAddress("127.0.0.1"); |
||||
return hostInfo; |
||||
} |
||||
|
||||
/** |
||||
* Find the first non-loopback InetAddress |
||||
*/ |
||||
@SneakyThrows |
||||
public static InetAddress getFirstNonLoopbackAddress() { |
||||
try { |
||||
for (Enumeration<NetworkInterface> enumNic = NetworkInterface.getNetworkInterfaces(); |
||||
enumNic.hasMoreElements(); ) { |
||||
NetworkInterface ifc = enumNic.nextElement(); |
||||
if (ifc.isUp()) { |
||||
for (Enumeration<InetAddress> enumAddr = ifc.getInetAddresses(); |
||||
enumAddr.hasMoreElements(); ) { |
||||
InetAddress address = enumAddr.nextElement(); |
||||
if (address instanceof Inet4Address && !address.isLoopbackAddress()) { |
||||
return address; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
catch (IOException ex) { |
||||
log.error("Cannot get first non-loopback address", ex); |
||||
} |
||||
|
||||
try { |
||||
return InetAddress.getLocalHost(); |
||||
} catch (UnknownHostException e) { |
||||
log.warn("Unable to retrieve localhost"); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
public static HostInfo convert(InetAddress address) { |
||||
HostInfo hostInfo = new HostInfo(); |
||||
hostInfo.setHostname(address.getHostName()); |
||||
hostInfo.setIpAddress(address.getHostAddress()); |
||||
return hostInfo; |
||||
} |
||||
|
||||
@Data |
||||
public static final class HostInfo { |
||||
public boolean override; |
||||
private String ipAddress; |
||||
private String hostname; |
||||
} |
||||
} |
Loading…
Reference in new issue