Browse Source

Use the first non-loopback address for host/ip addr

fixes gh-468
pull/6/head
Spencer Gibb 10 years ago
parent
commit
9e40714e3c
  1. 35
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java
  2. 10
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java

35
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java

@ -16,8 +16,11 @@
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -38,6 +41,7 @@ import com.netflix.appinfo.UniqueIdentifier;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Spencer Gibb
*/ */
@Data @Data
@ConfigurationProperties("eureka.instance") @ConfigurationProperties("eureka.instance")
@ -114,14 +118,35 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
private HostInfo initHostInfo() { private HostInfo initHostInfo() {
this.hostInfo = this.hostInfo == null ? new HostInfo() : this.hostInfo; this.hostInfo = this.hostInfo == null ? new HostInfo() : this.hostInfo;
InetAddress address = getFirstNonLoopbackAddress();
this.hostInfo.ipAddress = address.getHostAddress();
this.hostInfo.hostname = address.getHostName();
return this.hostInfo;
}
//TODO: move this method to s-c-commons
static InetAddress getFirstNonLoopbackAddress() {
try { try {
this.hostInfo.ipAddress = InetAddress.getLocalHost().getHostAddress(); for (Enumeration<NetworkInterface> enumNic = NetworkInterface.getNetworkInterfaces();
this.hostInfo.hostname = InetAddress.getLocalHost().getHostName(); 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 (UnknownHostException ex) { catch (IOException ex) {
logger.error("Cannot get host info", ex); logger.error("Cannot get host info", ex);
} }
return this.hostInfo; return null;
} }
public void setHostname(String hostname) { public void setHostname(String hostname) {

10
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java

@ -20,9 +20,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment; import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -46,12 +43,7 @@ public class EurekaInstanceConfigBeanTests {
@Before @Before
public void init() { public void init() {
try { this.hostName = EurekaInstanceConfigBean.getFirstNonLoopbackAddress().getHostName();
this.hostName = InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException e) {
// Ignore (test must be running in a restricted environment)
}
} }
@After @After

Loading…
Cancel
Save