diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java index 3b12748888..ba8b0c4921 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java @@ -1000,4 +1000,9 @@ public final class Constants { public static final String DATASOURCE_ENCRYPTION_SALT_DEFAULT = "!@#$%^&*"; public static final String DATASOURCE_ENCRYPTION_ENABLE = "datasource.encryption.enable"; public static final String DATASOURCE_ENCRYPTION_SALT = "datasource.encryption.salt"; + + /** + * Network IP gets priority, default inner outer + */ + public static final String NETWORK_PRIORITY_STRATEGY = "dolphin.scheduler.network.priority.strategy"; } diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java index b001825ce1..ddb29730b7 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java @@ -21,6 +21,8 @@ import static org.apache.dolphinscheduler.common.Constants.DOLPHIN_SCHEDULER_PRE import static java.util.Collections.emptyList; +import org.apache.dolphinscheduler.common.Constants; + import java.io.IOException; import java.net.Inet6Address; import java.net.InetAddress; @@ -42,22 +44,20 @@ import org.slf4j.LoggerFactory; */ public class NetUtils { - private NetUtils() { - throw new UnsupportedOperationException("Construct NetUtils"); - } - - private static Logger logger = LoggerFactory.getLogger(NetUtils.class); - private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); - - private static String ANY_HOST_VALUE = "0.0.0.0"; - - private static String LOCAL_HOST_VALUE = "127.0.0.1"; - + private static final String NETWORK_PRIORITY_DEFAULT = "default"; + private static final String NETWORK_PRIORITY_INNER = "inner"; + private static final String NETWORK_PRIORITY_OUTER = "outer"; + private static final Logger logger = LoggerFactory.getLogger(NetUtils.class); + private static final String ANY_HOST_VALUE = "0.0.0.0"; + private static final String LOCAL_HOST_VALUE = "127.0.0.1"; private static InetAddress LOCAL_ADDRESS = null; - private static volatile String HOST_ADDRESS; + private NetUtils() { + throw new UnsupportedOperationException("Construct NetUtils"); + } + public static String getHost() { if (HOST_ADDRESS != null) { return HOST_ADDRESS; @@ -87,24 +87,27 @@ public class NetUtils { if (null != LOCAL_ADDRESS) { return LOCAL_ADDRESS; } + InetAddress localAddress = null; - NetworkInterface networkInterface = findNetworkInterface(); - Enumeration addresses = networkInterface.getInetAddresses(); - while (addresses.hasMoreElements()) { - Optional addressOp = toValidAddress(addresses.nextElement()); - if (addressOp.isPresent()) { - try { - if (addressOp.get().isReachable(100)) { - LOCAL_ADDRESS = addressOp.get(); - return LOCAL_ADDRESS; + try { + NetworkInterface networkInterface = findNetworkInterface(); + if (networkInterface != null) { + Enumeration addresses = networkInterface.getInetAddresses(); + while (addresses.hasMoreElements()) { + Optional addressOp = toValidAddress(addresses.nextElement()); + if (addressOp.isPresent()) { + try { + if (addressOp.get().isReachable(100)) { + LOCAL_ADDRESS = addressOp.get(); + return LOCAL_ADDRESS; + } + } catch (IOException e) { + logger.warn("test address id reachable io exception", e); + } } - } catch (IOException e) { - logger.warn("test address id reachable io exception", e); } } - } - try { localAddress = InetAddress.getLocalHost(); } catch (UnknownHostException e) { logger.warn("InetAddress get LocalHost exception", e); @@ -190,7 +193,7 @@ public class NetUtils { if (null != result) { return result; } - return validNetworkInterfaces.get(0); + return findAddress(validNetworkInterfaces); } /** @@ -227,4 +230,70 @@ public class NetUtils { String preferredNetworkInterface = System.getProperty(DOLPHIN_SCHEDULER_PREFERRED_NETWORK_INTERFACE); return Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface); } + + private static NetworkInterface findAddress(List validNetworkInterfaces) { + if (validNetworkInterfaces.isEmpty()) { + return null; + } + String networkPriority = PropertyUtils.getString(Constants.NETWORK_PRIORITY_STRATEGY, NETWORK_PRIORITY_DEFAULT); + if (NETWORK_PRIORITY_DEFAULT.equalsIgnoreCase(networkPriority)) { + return findAddressByDefaultPolicy(validNetworkInterfaces); + } else if (NETWORK_PRIORITY_INNER.equalsIgnoreCase(networkPriority)) { + return findInnerAddress(validNetworkInterfaces); + } else if (NETWORK_PRIORITY_OUTER.equalsIgnoreCase(networkPriority)) { + return findOuterAddress(validNetworkInterfaces); + } else { + logger.error("There is no matching network card acquisition policy!"); + return null; + } + } + + private static NetworkInterface findAddressByDefaultPolicy(List validNetworkInterfaces) { + NetworkInterface networkInterface; + networkInterface = findInnerAddress(validNetworkInterfaces); + if (networkInterface == null) { + networkInterface = findOuterAddress(validNetworkInterfaces); + if (networkInterface == null) { + networkInterface = validNetworkInterfaces.get(0); + } + } + return networkInterface; + } + + /** + * Get the Intranet IP + * + * @return If no {@link NetworkInterface} is available , return null + */ + private static NetworkInterface findInnerAddress(List validNetworkInterfaces) { + + NetworkInterface networkInterface = null; + for (NetworkInterface ni : validNetworkInterfaces) { + Enumeration address = ni.getInetAddresses(); + while (address.hasMoreElements()) { + InetAddress ip = address.nextElement(); + if (ip.isSiteLocalAddress() + && !ip.isLoopbackAddress()) { + networkInterface = ni; + } + } + } + return networkInterface; + } + + private static NetworkInterface findOuterAddress(List validNetworkInterfaces) { + NetworkInterface networkInterface = null; + for (NetworkInterface ni : validNetworkInterfaces) { + Enumeration address = ni.getInetAddresses(); + while (address.hasMoreElements()) { + InetAddress ip = address.nextElement(); + if (!ip.isSiteLocalAddress() + && !ip.isLoopbackAddress()) { + networkInterface = ni; + } + } + } + return networkInterface; + } + } diff --git a/dolphinscheduler-common/src/main/resources/common.properties b/dolphinscheduler-common/src/main/resources/common.properties index a75f964fa2..b8c21c853b 100644 --- a/dolphinscheduler-common/src/main/resources/common.properties +++ b/dolphinscheduler-common/src/main/resources/common.properties @@ -72,3 +72,6 @@ kerberos.expire.time=2 # datasource encryption salt datasource.encryption.enable=false datasource.encryption.salt=!@#$%^&* + +# Network IP gets priority, default inner outer +#dolphin.scheduler.network.priority.strategy=default