in common/src/main/java/org/apache/seata/common/util/NetUtil.java [236:287]
private static InetAddress getLocalAddress0(String... preferredNetworks) {
InetAddress localAddress = null;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces != null) {
while (interfaces.hasMoreElements()) {
try {
NetworkInterface network = interfaces.nextElement();
if (network.isUp()) {
Enumeration<InetAddress> addresses = network.getInetAddresses();
while (addresses.hasMoreElements()) {
try {
InetAddress address = addresses.nextElement();
if (isValidAddress(address)) {
if (null == localAddress) {
localAddress = address;
}
//check preferredNetworks
if (preferredNetworks.length > 0) {
String ip = address.getHostAddress();
for (String regex : preferredNetworks) {
if (StringUtils.isBlank(regex)) {
continue;
}
if (ip.matches(regex) || ip.startsWith(regex)) {
return address;
}
}
} else {
return address;
}
}
} catch (Throwable e) {
LOGGER.warn("Failed to retrieving ip address, {}", e.getMessage(), e);
}
}
}
} catch (Throwable e) {
LOGGER.warn("Failed to retrieving ip address, {}", e.getMessage(), e);
}
}
}
} catch (Throwable e) {
LOGGER.warn("Failed to retrieving ip address, {}", e.getMessage(), e);
}
if (localAddress == null) {
LOGGER.error("Could not get local host ip address, will use 127.0.0.1 instead.");
} else {
LOGGER.error("Could not match ip by preferredNetworks:{}, will use default first ip {} instead.", Arrays.toString(preferredNetworks), localAddress.getHostAddress());
}
return localAddress;
}