From 75b5cceb1922367be3dcafdf1c319a601451c679 Mon Sep 17 00:00:00 2001 From: youbeiwuhuan Date: Tue, 23 Oct 2018 16:25:21 +0800 Subject: [PATCH] =?UTF-8?q?InetAddress.getLocalHost()=E5=9C=A8=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E7=BD=91=E7=BB=9C=E4=B8=8B=E6=98=AF=E4=B8=8D=E5=8F=AF?= =?UTF-8?q?=E4=BF=A1=E7=9A=84,=E6=89=80=E4=BB=A5=E4=BC=98=E5=85=88?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E7=BD=91=E5=8D=A1=E6=9D=A5=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=8F=AF=E7=94=A8=E7=9A=84=E5=A4=96?= =?UTF-8?q?=E7=BD=91=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xxl/job/core/util/IpUtil.java | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/util/IpUtil.java b/xxl-job-core/src/main/java/com/xxl/job/core/util/IpUtil.java index c29fb2e2..5c5af97a 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/util/IpUtil.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/util/IpUtil.java @@ -1,14 +1,17 @@ package com.xxl.job.core.util; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.regex.Pattern; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.UnknownHostException; -import java.util.Enumeration; -import java.util.regex.Pattern; - /** * get ip * @author xuxueli 2016-5-22 11:38:05 @@ -42,15 +45,6 @@ public class IpUtil { * @return */ private static InetAddress getFirstValidAddress() { - InetAddress localAddress = null; - try { - localAddress = InetAddress.getLocalHost(); - if (isValidAddress(localAddress)) { - return localAddress; - } - } catch (Throwable e) { - logger.error("Failed to retriving ip address, " + e.getMessage(), e); - } try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); if (interfaces != null) { @@ -78,9 +72,22 @@ public class IpUtil { } catch (Throwable e) { logger.error("Failed to retriving ip address, " + e.getMessage(), e); } + + + InetAddress localAddress = null; + try { + localAddress = InetAddress.getLocalHost(); + if (isValidAddress(localAddress)) { + return localAddress; + } + } catch (Throwable e) { + logger.error("Failed to retriving ip address, " + e.getMessage(), e); + } + logger.error("Could not get local host ip address, will use 127.0.0.1 instead."); return localAddress; } + /** * get address