From b00ec32e160736cdaf7571eca68abb251b28a7ca Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Wed, 24 Oct 2018 14:32:46 +0800 Subject: [PATCH] =?UTF-8?q?IP=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E4=BC=98=E5=85=88=E9=81=8D=E5=8E=86=E7=BD=91?= =?UTF-8?q?=E5=8D=A1=E6=9D=A5=E8=8E=B7=E5=8F=96=E5=8F=AF=E7=94=A8IP?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/XXL-JOB官方文档.md | 1 + .../java/com/xxl/job/core/util/IpUtil.java | 55 ++++++++++--------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/doc/XXL-JOB官方文档.md b/doc/XXL-JOB官方文档.md index 929f93d0..12033f08 100644 --- a/doc/XXL-JOB官方文档.md +++ b/doc/XXL-JOB官方文档.md @@ -1318,6 +1318,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段 - 7、[迭代中]docker镜像,并且推送docker镜像到中央仓库,更进一步实现产品开箱即用; - 8、[迭代中]cron在线生成工具,如 "cronboot/cron.qqe2"; - 9、[迭代中]原生提供通用命令行任务Handler(Bean任务,"CommandJobHandler");业务方只需要提供命令行即可,可执行任意命令; +- 10、IP获取逻辑优化,优先遍历网卡来获取可用IP; ### TODO LIST - 1、任务分片路由:分片采用一致性Hash算法计算出尽量稳定的分片顺序,即使注册机器存在波动也不会引起分批分片顺序大的波动;目前采用IP自然排序,可以满足需求,待定; 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 5c5af97a..4de5a851 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,19 +1,17 @@ package com.xxl.job.core.util; -import java.net.Inet6Address; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + 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; - /** * get ip + * * @author xuxueli 2016-5-22 11:38:05 */ public class IpUtil { @@ -23,16 +21,17 @@ public class IpUtil { private static final String LOCALHOST = "127.0.0.1"; public static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); - private static volatile InetAddress LOCAL_ADDRESS = null; + private static volatile String LOCAL_ADDRESS = null; /** * valid address * @param address - * @return + * @return boolean */ private static boolean isValidAddress(InetAddress address) { - if (address == null || address.isLoopbackAddress()) + if (address == null || address.isLoopbackAddress() || address.isLinkLocalAddress()) { return false; + } String name = address.getHostAddress(); return (name != null && ! ANYHOST.equals(name) @@ -42,9 +41,12 @@ public class IpUtil { /** * get first valid addredd - * @return + * + * @return InetAddress */ private static InetAddress getFirstValidAddress() { + + // NetworkInterface address try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); if (interfaces != null) { @@ -72,11 +74,10 @@ public class IpUtil { } catch (Throwable e) { logger.error("Failed to retriving ip address, " + e.getMessage(), e); } - - - InetAddress localAddress = null; + + // getLocalHost address try { - localAddress = InetAddress.getLocalHost(); + InetAddress localAddress = InetAddress.getLocalHost(); if (isValidAddress(localAddress)) { return localAddress; } @@ -85,38 +86,38 @@ public class IpUtil { } logger.error("Could not get local host ip address, will use 127.0.0.1 instead."); - return localAddress; + return null; } /** * get address - * @return + * + * @return String */ - private static InetAddress getAddress() { - if (LOCAL_ADDRESS != null) + private static String getAddress() { + if (LOCAL_ADDRESS != null) { return LOCAL_ADDRESS; + } InetAddress localAddress = getFirstValidAddress(); - LOCAL_ADDRESS = localAddress; - return localAddress; + LOCAL_ADDRESS = localAddress.getHostAddress(); + return LOCAL_ADDRESS; } /** * get ip - * @return + * + * @return String */ public static String getIp(){ - InetAddress address = getAddress(); - if (address==null) { - return null; - } - return address.getHostAddress(); + return getAddress(); } /** * get ip:port + * * @param port - * @return + * @return String */ public static String getIpPort(int port){ String ip = getIp();