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