Browse Source

Adds non-loopback hostname and ipAddress lookup.

Also adds a propertySource with those values:
"springCloudClientHostInfo": {
    "spring.cloud.client.hostname": "myhostname",
    "spring.cloud.client.ipAddress": "192.168.2.112"
},

fixes gh-47
pull/52/merge
Spencer Gibb 10 years ago
parent
commit
433ea0870a
  1. 36
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/HostInfoEnvironmentPostProcessor.java
  2. 81
      spring-cloud-commons/src/main/java/org/springframework/cloud/util/InetUtils.java
  3. 5
      spring-cloud-commons/src/main/resources/META-INF/spring.factories

36
spring-cloud-commons/src/main/java/org/springframework/cloud/client/HostInfoEnvironmentPostProcessor.java

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

81
spring-cloud-commons/src/main/java/org/springframework/cloud/util/InetUtils.java

@ -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;
}
}

5
spring-cloud-commons/src/main/resources/META-INF/spring.factories

@ -3,3 +3,8 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ @@ -3,3 +3,8 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.client.CommonsClientAutoConfiguration,\
org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration,\
org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.cloud.client.HostInfoEnvironmentPostProcessor

Loading…
Cancel
Save