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 2020/07/15 09:47:10 UTC

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

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



##########
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 (null != networkInterface) {
+            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 result = null;
+        for (NetworkInterface item : validNetworkInterfaces) {
+            if (isPreferredNetworkInterface(item)) {
+                result = item;
+                break;
+            }
+        }
+        if (null == result) {
+            result = getFirstNetworkInterface(validNetworkInterfaces);
+        }
+        return result;
+    }
+    
+    private static NetworkInterface getFirstNetworkInterface(final List<NetworkInterface> validNetworkInterfaces) {
+        NetworkInterface result = null;
+        for (NetworkInterface item : validNetworkInterfaces) {

Review comment:
       The temporary variables‘ name `item`, should be `each` for the List loop.

##########
File path: elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
##########
@@ -34,6 +39,8 @@
     
     public static final String IP_REGEX = "((\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)){3})";
     
+    public static final String PREFERRED_NETWORK_INTERFACE = "elasticjob.preferred.network.interface";

Review comment:
       why the `PREFERRED_NETWORK_INTERFACE ` is public?  maybe `private`?




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