From e298dcf83c7e4027ee5f5cdbe23382b415de8da2 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Mon, 8 Dec 2025 16:09:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=B8=8B=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=95=B0=E6=8D=AE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ServerAlertServiceImpl.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/ServerAlertServiceImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/ServerAlertServiceImpl.java index 5dcf540c..0995710c 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/ServerAlertServiceImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/ServerAlertServiceImpl.java @@ -61,19 +61,43 @@ public class ServerAlertServiceImpl implements IServerAlertService { return; } - // 过滤并检查适用于该服务器的规则 + // 按照告警类型分组规则,实现专属规则覆盖全局规则 + Map effectiveRules = new HashMap<>(); + for (ServerAlertRule rule : allRules) { - // 过滤:只检查全局规则或匹配的服务器规则 + // 跳过其他服务器的专属规则 if (rule.getServerId() != null && !rule.getServerId().equals(serverId)) { continue; } - // 只检查启用的规则 + // 跳过禁用的规则 if (!Boolean.TRUE.equals(rule.getEnabled())) { continue; } - // 根据告警类型检查 + MonitorMetricEnum alertType = rule.getAlertType(); + ServerAlertRule existingRule = effectiveRules.get(alertType); + + if (existingRule == null) { + // 该类型还没有规则,直接使用 + effectiveRules.put(alertType, rule); + } else { + // 该类型已有规则,专属规则优先(覆盖全局规则) + boolean isExistingGlobal = existingRule.getServerId() == null; + boolean isCurrentSpecific = rule.getServerId() != null; + + if (isCurrentSpecific && isExistingGlobal) { + // 当前是专属规则,已有是全局规则 → 用专属规则覆盖 + effectiveRules.put(alertType, rule); + log.debug("服务器专属规则覆盖全局规则: serverId={}, alertType={}, ruleId={}", + serverId, alertType, rule.getId()); + } + // 否则保持原有规则(专属优先,或者先到先得) + } + } + + // 使用筛选后的有效规则进行检查 + for (ServerAlertRule rule : effectiveRules.values()) { checkSingleRule(serverId, monitorData, rule, config); } }