You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/04/26 06:05:43 UTC

[GitHub] [shardingsphere-elasticjob] SkyMoreStars commented on a change in pull request #1069: Consider about improvement of get IP address inaccurate

SkyMoreStars commented on a change in pull request #1069:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/1069#discussion_r619995613



##########
File path: elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
##########
@@ -45,42 +52,93 @@ public static String getIp() {
         if (null != cachedIpAddress) {
             return cachedIpAddress;
         }
-        Enumeration<NetworkInterface> netInterfaces;
+        NetworkInterface networkInterface = findNetworkInterface();
+        if (networkInterface != null) {
+            Enumeration<InetAddress> ipAddresses = networkInterface.getInetAddresses();
+            while (ipAddresses.hasMoreElements()) {
+                InetAddress ipAddress = ipAddresses.nextElement();
+                if (isValidAddress(ipAddress)) {
+                    cachedIpAddress = ipAddress.getHostAddress();
+                    return cachedIpAddress;
+                }
+            }
+        }
+        return null;
+    }
+    
+    private static NetworkInterface findNetworkInterface() {
+        Enumeration<NetworkInterface> interfaces;
         try {
-            netInterfaces = NetworkInterface.getNetworkInterfaces();
+            interfaces = NetworkInterface.getNetworkInterfaces();
         } catch (final SocketException ex) {
             throw new HostException(ex);
         }
-        String localIpAddress = null;
-        while (netInterfaces.hasMoreElements()) {
-            NetworkInterface netInterface = netInterfaces.nextElement();
-            Enumeration<InetAddress> ipAddresses = netInterface.getInetAddresses();
-            while (ipAddresses.hasMoreElements()) {
-                InetAddress ipAddress = ipAddresses.nextElement();
-                if (isPublicIpAddress(ipAddress)) {
-                    String publicIpAddress = ipAddress.getHostAddress();
-                    cachedIpAddress = publicIpAddress;
-                    return publicIpAddress;
-                }
-                if (isLocalIpAddress(ipAddress)) {
-                    localIpAddress = ipAddress.getHostAddress();
+        List<NetworkInterface> validNetworkInterfaces = new LinkedList<>();
+        while (interfaces.hasMoreElements()) {
+            NetworkInterface networkInterface = interfaces.nextElement();
+            if (ignoreNetworkInterface(networkInterface)) {
+                continue;
+            }
+            validNetworkInterfaces.add(networkInterface);
+        }
+        NetworkInterface networkInterface = null;
+        for (NetworkInterface item : validNetworkInterfaces) {
+            if (isPreferredNetworkInterface(item)) {
+                networkInterface = item;
+                break;
+            }
+        }
+        if (networkInterface == null) {
+            networkInterface = getFirstNetworkInterface(validNetworkInterfaces);
+        }
+        return networkInterface;

Review comment:
       execuse me ! i network first position is llw0 ,but default network device is en0.so elastic-job can't find ipaddress.
   
   ```
   "validNetworkInterfaces":["llw0","awdl0","en5","en0"]
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org