From e91e3590417a6eef1780f39f926607878539f8fe Mon Sep 17 00:00:00 2001 From: gaoxq <376340421@qq.com> Date: Mon, 10 Nov 2025 18:03:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mini/capi/api/biz/erpController.java | 2 +- .../com/mini/capi/api/job/jobController.java | 2 +- .../com/mini/capi/utils/NetworkUtils.java | 133 +++++------------- 3 files changed, 35 insertions(+), 102 deletions(-) diff --git a/src/main/java/com/mini/capi/api/biz/erpController.java b/src/main/java/com/mini/capi/api/biz/erpController.java index 795fe4a..adc46af 100644 --- a/src/main/java/com/mini/capi/api/biz/erpController.java +++ b/src/main/java/com/mini/capi/api/biz/erpController.java @@ -38,7 +38,7 @@ public class erpController { /** * 流水交易记账 */ - @GetMapping("getErpTranFlow") + @GetMapping("getTranFlow") public ApiResult getErpTranFlow(String flowId) { UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("flow_id", flowId); diff --git a/src/main/java/com/mini/capi/api/job/jobController.java b/src/main/java/com/mini/capi/api/job/jobController.java index b7cbc1b..c008ebe 100644 --- a/src/main/java/com/mini/capi/api/job/jobController.java +++ b/src/main/java/com/mini/capi/api/job/jobController.java @@ -32,7 +32,7 @@ public class jobController { /** * 主机在线状态检测 */ - @GetMapping("getJobMonitHostStatus") + @GetMapping("getMonitHostStatus") public ApiResult getJobMonitHostStatus() { List monitorHosts = bizMonitorHostService.list(); List> futures = new ArrayList<>(monitorHosts.size()); diff --git a/src/main/java/com/mini/capi/utils/NetworkUtils.java b/src/main/java/com/mini/capi/utils/NetworkUtils.java index 3f399ff..35159e5 100644 --- a/src/main/java/com/mini/capi/utils/NetworkUtils.java +++ b/src/main/java/com/mini/capi/utils/NetworkUtils.java @@ -1,125 +1,58 @@ package com.mini.capi.utils; -import java.io.IOException; import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.regex.Pattern; +import java.net.InetSocketAddress; +import java.net.Socket; /** * 网络连通性检测工具类(支持IPv4和域名) */ public class NetworkUtils { - // IPv4地址校验正则(支持带前后空白) - private static final Pattern IPV4_PATTERN = Pattern.compile( - "^\\s*((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\s*$" - ); - // Ping命令配置常量 - private static final int PING_COUNT = 2; - private static final int WINDOWS_TIMEOUT_MS = 3000; - private static final int LINUX_TIMEOUT_SEC = 3; - private static final int PROCESS_WAIT_TIMEOUT_SEC = 6; + // 默认检查端口(保持80作为默认,兼容通用场景) + private static final int DEFAULT_PORT = 80; + // 默认超时时间(3秒,可根据需求调整) + private static final int DEFAULT_TIMEOUT_MS = 3000; - // 私有构造器:禁止实例化 private NetworkUtils() { - throw new AssertionError("工具类不允许实例化"); + throw new UnsupportedOperationException("工具类不允许实例化"); } /** - * 检测本地网络到目标IP或域名的连通性 + * 检查目标主机(IP/域名)的连通性(使用默认端口80) + * + * @param target 目标IP或域名(不可为null/空) + * @return true:可达;false:不可达(含参数无效/检查失败) */ public static boolean isNetworkReachable(String target) { - if (target == null || target.trim().isEmpty()) { + // 调用重载方法,传入默认端口 + return isNetworkReachable(target, DEFAULT_PORT); + } + /** + * 检查目标主机(IP/域名)的连通性(自定义端口) + */ + public static boolean isNetworkReachable(String target, int port) { + // 参数校验:目标为空或端口无效,直接返回不可达 + if (target == null || target.trim().isEmpty() || port < 1 || port > 65535) { return false; } - target = target.trim(); - - List ipList = new ArrayList<>(); - // 区分IP和域名,解析目标为IP列表 - if (isValidIpv4(target)) { - ipList.add(target); // 是IP,直接加入 - } else { - // 是域名,尝试解析为IP - try { - InetAddress[] addresses = InetAddress.getAllByName(target); - for (InetAddress addr : addresses) { - ipList.add(addr.getHostAddress()); - } - if (ipList.isEmpty()) { - return false; // 域名解析无结果 - } - } catch (UnknownHostException e) { - return false; // 域名无法解析(如不存在、网络故障) - } - } - - // 对每个IP执行ping检测,有一个成功则返回true - for (String ip : ipList) { - if (executePingCommand(buildPingCommand(ip))) { - return true; - } - } - return false; - } - - /** - * 校验是否为合法的IPv4地址(支持带前后空白) - */ - private static boolean isValidIpv4(String ip) { - return IPV4_PATTERN.matcher(ip).matches(); - } - - /** - * 构建跨系统的ping命令 - */ - private static List buildPingCommand(String ip) { - List command = new ArrayList<>(5); - command.add("ping"); - - String osName = System.getProperty("os.name", "").toLowerCase(); - if (osName.contains("windows")) { - command.add("-n"); - command.add(String.valueOf(PING_COUNT)); - command.add("-w"); - command.add(String.valueOf(WINDOWS_TIMEOUT_MS)); - } else { - command.add("-c"); - command.add(String.valueOf(PING_COUNT)); - command.add("-W"); - command.add(String.valueOf(LINUX_TIMEOUT_SEC)); - } - - command.add(ip); - return command; - } - - /** - * 执行ping命令并返回执行结果 - */ - private static boolean executePingCommand(List command) { - Process process = null; + String normalizedTarget = target.trim(); try { - ProcessBuilder processBuilder = new ProcessBuilder(command); - processBuilder.redirectErrorStream(true); - process = processBuilder.start(); - - boolean isFinished = process.waitFor(PROCESS_WAIT_TIMEOUT_SEC, TimeUnit.SECONDS); - if (!isFinished) { - process.destroyForcibly(); - return false; + // 先解析目标地址(只解析一次,避免重复操作) + InetAddress address = InetAddress.getByName(normalizedTarget); + // 优先尝试 Socket 连接指定端口 + try (Socket socket = new Socket()) { + socket.connect(new InetSocketAddress(address, port), DEFAULT_TIMEOUT_MS); + return true; // 端口连接成功,返回可达 + } catch (Exception e) { + // Socket 连接失败,不中断流程,继续执行主机可达性检查 } - return process.exitValue() == 0; - - } catch (IOException | InterruptedException e) { + // Socket 失败后,降级检查主机基础可达性(ICMP) + return address.isReachable(DEFAULT_TIMEOUT_MS); + } catch (Exception e) { + // 任何异常(地址解析失败、ICMP 检查失败等)均返回不可达 return false; - } finally { - if (process != null) { - process.destroyForcibly(); - } } } } \ No newline at end of file