Use non-loopback address

This commit is contained in:
Spencer Gibb
2015-08-28 14:01:15 -06:00
parent a6b2519475
commit 63aa7c38d0
4 changed files with 43 additions and 13 deletions

View File

@@ -16,7 +16,7 @@
package org.springframework.cloud.consul.discovery;
import static org.springframework.cloud.consul.discovery.Utils.getCatalogServiceHost;
import static org.springframework.cloud.consul.discovery.IpAddressUtils.getCatalogServiceHost;
import java.util.ArrayList;
import java.util.List;

View File

@@ -16,18 +16,19 @@
package org.springframework.cloud.consul.discovery;
import static org.springframework.cloud.consul.discovery.IpAddressUtils.getFirstNonLoopbackAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -78,13 +79,9 @@ public class ConsulDiscoveryProperties {
private String[] initHostInfo() {
String[] info = new String[2];
try {
info[0] = InetAddress.getLocalHost().getHostAddress();
info[1] = InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException ex) {
log.error("Cannot get host info", ex);
}
InetAddress address = getFirstNonLoopbackAddress();
info[0] = address.getHostAddress();
info[1] = address.getHostName();
return info;
}
}

View File

@@ -16,7 +16,7 @@
package org.springframework.cloud.consul.discovery;
import static org.springframework.cloud.consul.discovery.Utils.getCatalogServiceHost;
import static org.springframework.cloud.consul.discovery.IpAddressUtils.getCatalogServiceHost;
import com.ecwid.consul.v1.catalog.model.CatalogService;
import com.netflix.loadbalancer.Server;

View File

@@ -16,13 +16,24 @@
package org.springframework.cloud.consul.discovery;
import com.ecwid.consul.v1.catalog.model.CatalogService;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import lombok.SneakyThrows;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.util.StringUtils;
import com.ecwid.consul.v1.catalog.model.CatalogService;
/**
* @author Spencer Gibb
*/
public class Utils {
@CommonsLog
public class IpAddressUtils {
public static String getCatalogServiceHost(CatalogService service, boolean preferAddress) {
if (preferAddress) {
@@ -35,4 +46,26 @@ public class Utils {
return service.getNode();
}
@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 host info", ex);
}
return InetAddress.getLocalHost();
}
}