Bläddra i källkod

IP获取逻辑优化,优先遍历网卡来获取可用IP;

xuxueli 6 år sedan
förälder
incheckning
b00ec32e16

+ 1 - 0
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自然排序,可以满足需求,待定;

+ 28 - 27
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<NetworkInterface> 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();