Ver código fonte

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

xuxueli 6 anos atrás
pai
commit
b00ec32e16

+ 1 - 0
doc/XXL-JOB官方文档.md Ver arquivo

@@ -1318,6 +1318,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
1318 1318
 - 7、[迭代中]docker镜像,并且推送docker镜像到中央仓库,更进一步实现产品开箱即用;
1319 1319
 - 8、[迭代中]cron在线生成工具,如 "cronboot/cron.qqe2";
1320 1320
 - 9、[迭代中]原生提供通用命令行任务Handler(Bean任务,"CommandJobHandler");业务方只需要提供命令行即可,可执行任意命令;
1321
+- 10、IP获取逻辑优化,优先遍历网卡来获取可用IP;
1321 1322
 
1322 1323
 ### TODO LIST
1323 1324
 - 1、任务分片路由:分片采用一致性Hash算法计算出尽量稳定的分片顺序,即使注册机器存在波动也不会引起分批分片顺序大的波动;目前采用IP自然排序,可以满足需求,待定;

+ 28 - 27
xxl-job-core/src/main/java/com/xxl/job/core/util/IpUtil.java Ver arquivo

@@ -1,19 +1,17 @@
1 1
 package com.xxl.job.core.util;
2 2
 
3
-import java.net.Inet6Address;
3
+import org.slf4j.Logger;
4
+import org.slf4j.LoggerFactory;
5
+
4 6
 import java.net.InetAddress;
5 7
 import java.net.NetworkInterface;
6
-import java.net.SocketException;
7 8
 import java.net.UnknownHostException;
8
-import java.util.ArrayList;
9 9
 import java.util.Enumeration;
10 10
 import java.util.regex.Pattern;
11 11
 
12
-import org.slf4j.Logger;
13
-import org.slf4j.LoggerFactory;
14
-
15 12
 /**
16 13
  * get ip
14
+ *
17 15
  * @author xuxueli 2016-5-22 11:38:05
18 16
  */
19 17
 public class IpUtil {
@@ -23,16 +21,17 @@ public class IpUtil {
23 21
 	private static final String LOCALHOST = "127.0.0.1";
24 22
 	public static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
25 23
 
26
-	private static volatile InetAddress LOCAL_ADDRESS = null;
24
+	private static volatile String LOCAL_ADDRESS = null;
27 25
 
28 26
 	/**
29 27
 	 * valid address
30 28
 	 * @param address
31
-	 * @return
29
+	 * @return boolean
32 30
 	 */
33 31
 	private static boolean isValidAddress(InetAddress address) {
34
-		if (address == null || address.isLoopbackAddress())
32
+		if (address == null || address.isLoopbackAddress() || address.isLinkLocalAddress()) {
35 33
 			return false;
34
+		}
36 35
 		String name = address.getHostAddress();
37 36
 		return (name != null
38 37
 				&& ! ANYHOST.equals(name)
@@ -42,9 +41,12 @@ public class IpUtil {
42 41
 
43 42
 	/**
44 43
 	 * get first valid addredd
45
-	 * @return
44
+	 *
45
+	 * @return InetAddress
46 46
 	 */
47 47
 	private static InetAddress getFirstValidAddress() {
48
+
49
+		// NetworkInterface address
48 50
 		try {
49 51
 			Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
50 52
 			if (interfaces != null) {
@@ -72,11 +74,10 @@ public class IpUtil {
72 74
 		} catch (Throwable e) {
73 75
 			logger.error("Failed to retriving ip address, " + e.getMessage(), e);
74 76
 		}
75
-		
76
-		
77
-		InetAddress localAddress = null;
77
+
78
+		// getLocalHost address
78 79
 		try {
79
-			localAddress = InetAddress.getLocalHost();
80
+			InetAddress localAddress = InetAddress.getLocalHost();
80 81
 			if (isValidAddress(localAddress)) {
81 82
 				return localAddress;
82 83
 			}
@@ -85,38 +86,38 @@ public class IpUtil {
85 86
 		}
86 87
 		
87 88
 		logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
88
-		return localAddress;
89
+		return null;
89 90
 	}
90 91
 	
91 92
 
92 93
 	/**
93 94
 	 * get address
94
-	 * @return
95
+	 *
96
+	 * @return String
95 97
 	 */
96
-	private static InetAddress getAddress() {
97
-		if (LOCAL_ADDRESS != null)
98
+	private static String getAddress() {
99
+		if (LOCAL_ADDRESS != null) {
98 100
 			return LOCAL_ADDRESS;
101
+		}
99 102
 		InetAddress localAddress = getFirstValidAddress();
100
-		LOCAL_ADDRESS = localAddress;
101
-		return localAddress;
103
+		LOCAL_ADDRESS = localAddress.getHostAddress();
104
+		return LOCAL_ADDRESS;
102 105
 	}
103 106
 
104 107
 	/**
105 108
 	 * get ip
106
-	 * @return
109
+	 *
110
+	 * @return String
107 111
 	 */
108 112
 	public static String getIp(){
109
-		InetAddress address = getAddress();
110
-		if (address==null) {
111
-			return null;
112
-		}
113
-		return address.getHostAddress();
113
+		return getAddress();
114 114
 	}
115 115
 
116 116
 	/**
117 117
 	 * get ip:port
118
+	 *
118 119
 	 * @param port
119
-	 * @return
120
+	 * @return String
120 121
 	 */
121 122
 	public static String getIpPort(int port){
122 123
 		String ip = getIp();