Bläddra i källkod

IP工具类优化,本地IP地址静态缓存;

xueli.xue 9 år sedan
förälder
incheckning
c86d7d1c8e

+ 4 - 3
README.md Visa fil

@@ -700,6 +700,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
700 700
 - 4、Jetty升级版本至8.2.0.v20160908;
701 701
 - 5、已推送V1.5.0和V1.5.1至Maven中央仓库;
702 702
 
703
+#### 6.10 版本 V1.5.2 特性
704
+- 1、IP工具类优化,IP静态缓存;
703 705
 
704 706
 #### 规划中
705 707
 - 1、集群执行器选择规则自定义:单点=选择第一个,随机=随机选择一个;
@@ -712,9 +714,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
712 714
 - 5、兼容oracle;
713 715
 - 6、跨天日志write问题修复;
714 716
 - 7、rolling日志;
715
-- 8、IP工具类优化,IP静态缓存;
716
-- 9、执行器,server启动,注册逻辑调整;
717
-- 10、通过listener或stop方法,容器销毁时销毁线程;Daemon方式有时不太理想;
717
+- 8、执行器,server启动,注册逻辑调整;
718
+- 9、通过listener或stop方法,容器销毁时销毁线程;Daemon方式有时不太理想;
718 719
 
719 720
 ## 七、其他
720 721
 

+ 2 - 3
xxl-job-admin/src/main/java/com/xxl/job/admin/core/callback/XxlJobLogCallbackServer.java Visa fil

@@ -24,10 +24,9 @@ public class XxlJobLogCallbackServer {
24 24
     
25 25
     Server server = null;
26 26
     public void start(int callBackPort) throws Exception {
27
+
27 28
     	// init address
28
-    	
29
-    	String ip = IpUtil.getIp();
30
-    	trigger_log_address = ip.concat(":").concat(String.valueOf(callBackPort));
29
+        trigger_log_address = IpUtil.getIpPort(callBackPort);
31 30
 		
32 31
     	final int port = Integer.valueOf(callBackPort);
33 32
         new Thread(new Runnable() {

+ 1 - 1
xxl-job-core/src/main/java/com/xxl/job/core/executor/jetty/XxlJobExecutor.java Visa fil

@@ -94,7 +94,7 @@ public class XxlJobExecutor implements ApplicationContextAware {
94 94
             public void run() {
95 95
                 while (true) {
96 96
                     try {
97
-                        String address = IpUtil.getIp().concat(":").concat(String.valueOf(port));
97
+                        String address = IpUtil.getIpPort(port);
98 98
                         registHelper.registry(RegistHelper.RegistType.EXECUTOR.name(), appName, address);
99 99
                         TimeUnit.SECONDS.sleep(RegistHelper.TIMEOUT);
100 100
                     } catch (Exception e) {

+ 93 - 16
xxl-job-core/src/main/java/com/xxl/job/core/util/IpUtil.java Visa fil

@@ -7,6 +7,7 @@ import java.net.InetAddress;
7 7
 import java.net.NetworkInterface;
8 8
 import java.net.UnknownHostException;
9 9
 import java.util.Enumeration;
10
+import java.util.regex.Pattern;
10 11
 
11 12
 /**
12 13
  * get ip
@@ -15,36 +16,112 @@ import java.util.Enumeration;
15 16
 public class IpUtil {
16 17
 	private static final Logger logger = LoggerFactory.getLogger(IpUtil.class);
17 18
 
19
+	public static final String ANYHOST = "0.0.0.0";
20
+	public static final String LOCALHOST = "127.0.0.1";
21
+	private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
22
+
23
+	private static volatile InetAddress LOCAL_ADDRESS = null;
24
+
25
+	/**
26
+	 * valid address
27
+	 * @param address
28
+	 * @return
29
+	 */
30
+	private static boolean isValidAddress(InetAddress address) {
31
+		if (address == null || address.isLoopbackAddress())
32
+			return false;
33
+		String name = address.getHostAddress();
34
+		return (name != null
35
+				&& ! ANYHOST.equals(name)
36
+				&& ! LOCALHOST.equals(name)
37
+				&& IP_PATTERN.matcher(name).matches());
38
+	}
39
+
18 40
 	/**
19
-	 * 获取本机ip
20
-	 * @return ip
41
+	 * get first valid addredd
42
+	 * @return
21 43
 	 */
22
-	public static String getIp() {
44
+	private static InetAddress getFirstValidAddress() {
45
+		InetAddress localAddress = null;
46
+		try {
47
+			localAddress = InetAddress.getLocalHost();
48
+			if (isValidAddress(localAddress)) {
49
+				return localAddress;
50
+			}
51
+		} catch (Throwable e) {
52
+			logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
53
+		}
23 54
 		try {
24 55
 			Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
25
-			InetAddress address = null;
26
-			while (interfaces.hasMoreElements()) {
27
-				NetworkInterface ni = interfaces.nextElement();
28
-				Enumeration<InetAddress> addresses = ni.getInetAddresses();
29
-				while (addresses.hasMoreElements()) {
30
-					address = addresses.nextElement();
31
-					if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1) {
32
-						return address.getHostAddress();
56
+			if (interfaces != null) {
57
+				while (interfaces.hasMoreElements()) {
58
+					try {
59
+						NetworkInterface network = interfaces.nextElement();
60
+						Enumeration<InetAddress> addresses = network.getInetAddresses();
61
+						if (addresses != null) {
62
+							while (addresses.hasMoreElements()) {
63
+								try {
64
+									InetAddress address = addresses.nextElement();
65
+									if (isValidAddress(address)) {
66
+										return address;
67
+									}
68
+								} catch (Throwable e) {
69
+									logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
70
+								}
71
+							}
72
+						}
73
+					} catch (Throwable e) {
74
+						logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
33 75
 					}
34 76
 				}
35 77
 			}
36
-			logger.info("xxl job getHostAddress fail");
78
+		} catch (Throwable e) {
79
+			logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
80
+		}
81
+		logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
82
+		return localAddress;
83
+	}
84
+
85
+	/**
86
+	 * get address
87
+	 * @return
88
+	 */
89
+	private static InetAddress getAddress() {
90
+		if (LOCAL_ADDRESS != null)
91
+			return LOCAL_ADDRESS;
92
+		InetAddress localAddress = getFirstValidAddress();
93
+		LOCAL_ADDRESS = localAddress;
94
+		return localAddress;
95
+	}
96
+
97
+	/**
98
+	 * get ip
99
+	 * @return
100
+	 */
101
+	public static String getIp(){
102
+		InetAddress address = getAddress();
103
+		if (address==null) {
37 104
 			return null;
38
-		} catch (Throwable t) {
39
-			logger.error("xxl job getHostAddress error, {}", t);
105
+		}
106
+		return address.getHostAddress();
107
+	}
108
+
109
+	/**
110
+	 * get ip:port
111
+	 * @param port
112
+	 * @return
113
+	 */
114
+	public static String getIpPort(int port){
115
+		String ip = getIp();
116
+		if (ip==null) {
40 117
 			return null;
41 118
 		}
119
+		return ip.concat(":").concat(String.valueOf(port));
42 120
 	}
43 121
 
44 122
 	public static void main(String[] args) throws UnknownHostException {
45
-		System.out.println(InetAddress.getLocalHost().getCanonicalHostName());
46
-		System.out.println(InetAddress.getLocalHost().getHostName());
47 123
 		System.out.println(getIp());
124
+		System.out.println(getIpPort(8080));
48 125
 	}
49 126
 
50 127
 }