From d0f622ab6f2a3ded8ed2b30a1c8fa53ee6a93fbb Mon Sep 17 00:00:00 2001 From: CaptainB Date: Mon, 13 Dec 2021 18:05:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E8=B0=83=E8=AF=95=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/utils/LocalAddressUtils.java | 41 +++++++++++++++++++ .../engine/docker/DockerTestEngine.java | 11 +++-- backend/src/main/java/io/metersphere/xpack | 2 +- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/commons/utils/LocalAddressUtils.java diff --git a/backend/src/main/java/io/metersphere/commons/utils/LocalAddressUtils.java b/backend/src/main/java/io/metersphere/commons/utils/LocalAddressUtils.java new file mode 100644 index 0000000000..de1f50bea1 --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/utils/LocalAddressUtils.java @@ -0,0 +1,41 @@ +package io.metersphere.commons.utils; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +public class LocalAddressUtils { + + /** + * 这个过滤了 是回路的地址 + */ + public static Map getIpAddresses() { + Enumeration netInterfaces; + Map result = new HashMap<>(); + try { + // 拿到所有网卡 + netInterfaces = NetworkInterface.getNetworkInterfaces(); + InetAddress ip; + // 遍历每个网卡,拿到ip + while (netInterfaces.hasMoreElements()) { + NetworkInterface ni = netInterfaces.nextElement(); + Enumeration addresses = ni.getInetAddresses(); + while (addresses.hasMoreElements()) { + ip = addresses.nextElement(); + if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(':') == -1) { + result.put(ni.getName(), ip.getHostAddress()); + } + } + } + } catch (Exception e) { + } + return result; + } + + public static String getIpAddress(String name) { + return getIpAddresses().get(name); + } + +} diff --git a/backend/src/main/java/io/metersphere/performance/engine/docker/DockerTestEngine.java b/backend/src/main/java/io/metersphere/performance/engine/docker/DockerTestEngine.java index 611c91533b..8ae1a52b89 100644 --- a/backend/src/main/java/io/metersphere/performance/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/performance/engine/docker/DockerTestEngine.java @@ -9,6 +9,7 @@ import io.metersphere.commons.constants.ReportKeys; import io.metersphere.commons.constants.ResourceStatusEnum; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; +import io.metersphere.commons.utils.LocalAddressUtils; import io.metersphere.commons.utils.UrlTestUtils; import io.metersphere.config.KafkaProperties; import io.metersphere.controller.ResultHolder; @@ -85,10 +86,14 @@ public class DockerTestEngine extends AbstractEngine { if (baseInfo != null) { metersphereUrl = baseInfo.getUrl(); } + // docker 不能从 localhost 中下载文件, 本地开发 + if (StringUtils.contains(metersphereUrl, "http://localhost") || + StringUtils.contains(metersphereUrl, "http://127.0.0.1")) { + metersphereUrl = "http://" + LocalAddressUtils.getIpAddress("en0") + ":8081"; + } + String jmeterPingUrl = metersphereUrl + "/jmeter/ping"; // 检查下载地址是否正确 - // docker 不能从 localhost 中下载文件 - if (StringUtils.contains(metersphereUrl, "http://localhost") - || !UrlTestUtils.testUrlWithTimeOut(jmeterPingUrl, 1000)) { + if (!UrlTestUtils.testUrlWithTimeOut(jmeterPingUrl, 1000)) { MSException.throwException(Translator.get("run_load_test_file_init_error")); } diff --git a/backend/src/main/java/io/metersphere/xpack b/backend/src/main/java/io/metersphere/xpack index a99681fe94..8bd3730267 160000 --- a/backend/src/main/java/io/metersphere/xpack +++ b/backend/src/main/java/io/metersphere/xpack @@ -1 +1 @@ -Subproject commit a99681fe94107d42ff340f4e8dcdca95195cf884 +Subproject commit 8bd3730267a6c1b383a781b3c4ef8526e5414d82