diff --git a/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/PowerJobDKey.java b/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/PowerJobDKey.java index 3bc2c32d..b849b9e6 100644 --- a/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/PowerJobDKey.java +++ b/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/PowerJobDKey.java @@ -1,5 +1,7 @@ package com.github.kfcfans.powerjob.common; +import java.net.NetworkInterface; + /** * 通过 JVM 启动参数传入的配置信息 * @@ -9,6 +11,14 @@ package com.github.kfcfans.powerjob.common; */ public class PowerJobDKey { + /** + * The property name for {@link NetworkInterface#getDisplayName() the name of network interface} that the PowerJob application prefers + */ public static final String PREFERRED_NETWORK_INTERFACE = "powerjob.network.interface.preferred"; + /** + * Java regular expressions for network interfaces that will be ignored. + */ + public static final String IGNORED_NETWORK_INTERFACE_REGEX = "powerjob.network.interface.ignored"; + } diff --git a/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/utils/NetUtils.java b/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/utils/NetUtils.java index d26230f9..8b8c4817 100644 --- a/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/utils/NetUtils.java +++ b/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/utils/NetUtils.java @@ -17,6 +17,7 @@ limitations under the License. import com.github.kfcfans.powerjob.common.PowerJobDKey; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.net.*; @@ -235,6 +236,10 @@ public class NetUtils { if (ignoreNetworkInterface(networkInterface)) { // ignore continue; } + // 根据用户 -D 参数忽略网卡 + if (ignoreInterfaceByConfig(networkInterface.getDisplayName())) { + continue; + } validNetworkInterfaces.add(networkInterface); } return validNetworkInterfaces; @@ -285,4 +290,16 @@ public class NetUtils { String preferredNetworkInterface = System.getProperty(PowerJobDKey.PREFERRED_NETWORK_INTERFACE); return Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface); } + + static boolean ignoreInterfaceByConfig(String interfaceName) { + String regex = System.getProperty(PowerJobDKey.IGNORED_NETWORK_INTERFACE_REGEX); + if (StringUtils.isBlank(regex)) { + return false; + } + if (interfaceName.matches(regex)) { + log.info("[Net] ignore network interface: {} by regex({})", interfaceName, regex); + return true; + } + return false; + } } diff --git a/powerjob-common/src/test/java/NetUtilsTest.java b/powerjob-common/src/test/java/NetUtilsTest.java index a586453e..410c1f06 100644 --- a/powerjob-common/src/test/java/NetUtilsTest.java +++ b/powerjob-common/src/test/java/NetUtilsTest.java @@ -21,4 +21,10 @@ public class NetUtilsTest { System.out.println(NetUtils.getLocalHost()); } + @Test + public void testIgnoredNetworkInterface() { + System.setProperty(PowerJobDKey.IGNORED_NETWORK_INTERFACE_REGEX, "utun.|llw."); + System.out.println(NetUtils.getLocalHost()); + } + } diff --git a/powerjob-worker/src/test/java/com/github/kfcfans/powerjob/UtilsTest.java b/powerjob-worker/src/test/java/com/github/kfcfans/powerjob/UtilsTest.java deleted file mode 100644 index 329f0ee5..00000000 --- a/powerjob-worker/src/test/java/com/github/kfcfans/powerjob/UtilsTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.github.kfcfans.powerjob; - -import com.github.kfcfans.powerjob.worker.common.utils.SystemInfoUtils; -import org.junit.jupiter.api.Test; - -/** - * 测试工具类 - * - * @author tjq - * @since 2020/3/24 - */ -public class UtilsTest { - - - @Test - public void testSystemInfoUtils() { - System.out.println(SystemInfoUtils.getSystemMetrics()); - } - -}