|
|
@@ -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
|
}
|