[dev] update NetUtils, support ignore network interface now

This commit is contained in:
tjq 2020-08-08 17:27:27 +08:00
parent 756adce633
commit ae18c5fd7c
4 changed files with 33 additions and 20 deletions

View File

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

View File

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

View File

@ -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());
}
}

View File

@ -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());
}
}