From 25e1e60bc4138d6736d1a0c24640473d88172b0f Mon Sep 17 00:00:00 2001 From: iizmg Date: Mon, 27 Apr 2020 09:40:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/db/DbCacheServiceRegistration.java | 2 +- .../org/itkk/udf/starter/core/CoreUtil.java | 49 ++++++++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/udf-starter/udf-starter-cache-db/src/main/java/org/itkk/udf/starter/cache/db/DbCacheServiceRegistration.java b/udf-starter/udf-starter-cache-db/src/main/java/org/itkk/udf/starter/cache/db/DbCacheServiceRegistration.java index c5c1b54..97baf8c 100644 --- a/udf-starter/udf-starter-cache-db/src/main/java/org/itkk/udf/starter/cache/db/DbCacheServiceRegistration.java +++ b/udf-starter/udf-starter-cache-db/src/main/java/org/itkk/udf/starter/cache/db/DbCacheServiceRegistration.java @@ -96,7 +96,7 @@ public class DbCacheServiceRegistration implements IServiceRegistration { public void init() throws UnknownHostException, JsonProcessingException { //获得IP和端口 this.applicationName = env.getProperty("spring.application.name"); - this.ip = CoreUtil.getIpAddress(); + this.ip = CoreUtil.getLocalHostLANAddress().getHostAddress(); this.port = env.getProperty("server.port"); this.managementPort = env.getProperty("management.server.port"); //放入缓存 diff --git a/udf-starter/udf-starter-core/src/main/java/org/itkk/udf/starter/core/CoreUtil.java b/udf-starter/udf-starter-core/src/main/java/org/itkk/udf/starter/core/CoreUtil.java index bd35f01..814a3cc 100644 --- a/udf-starter/udf-starter-core/src/main/java/org/itkk/udf/starter/core/CoreUtil.java +++ b/udf-starter/udf-starter-core/src/main/java/org/itkk/udf/starter/core/CoreUtil.java @@ -21,9 +21,9 @@ import javax.servlet.http.HttpServletResponse; import javax.validation.ConstraintViolation; import javax.validation.Validation; import java.io.*; -import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; +import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.*; @@ -457,32 +457,45 @@ public class CoreUtil { } /** - * 获得IP地址 + * 获得局域网IP地址 * - * @return String + * @return InetAddress + * @throws UnknownHostException */ - public static String getIpAddress() { + public static InetAddress getLocalHostLANAddress() throws UnknownHostException { try { - Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces(); - InetAddress ip = null; - while (allNetInterfaces.hasMoreElements()) { - NetworkInterface netInterface = allNetInterfaces.nextElement(); - if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) { - continue; - } else { - Enumeration addresses = netInterface.getInetAddresses(); - while (addresses.hasMoreElements()) { - ip = addresses.nextElement(); - if (ip != null && ip instanceof Inet4Address) { - return ip.getHostAddress(); + InetAddress candidateAddress = null; + // 遍历所有的网络接口 + for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) { + NetworkInterface iface = (NetworkInterface) ifaces.nextElement(); + // 在所有的接口下再遍历IP + for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) { + InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); + if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址 + if (inetAddr.isSiteLocalAddress()) { + // 如果是site-local地址,就是它了 + return inetAddr; + } else if (candidateAddress == null) { + // site-local类型的地址未被发现,先记录候选地址 + candidateAddress = inetAddr; } } } } + if (candidateAddress != null) { + return candidateAddress; + } + // 如果没有发现 non-loopback地址.只能用最次选的方案 + InetAddress jdkSuppliedAddress = InetAddress.getLocalHost(); + if (jdkSuppliedAddress == null) { + throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null."); + } + return jdkSuppliedAddress; } catch (Exception e) { - throw new SystemRuntimeException(e); + UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address: " + e); + unknownHostException.initCause(e); + throw unknownHostException; } - return ""; }