|
@@ -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();
|