Explorar el Código

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

xuxueli hace 7 años
padre
commit
b00ec32e16

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

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

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

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