mtr探测优化

This commit is contained in:
gaoyutao
2025-11-28 16:20:07 +08:00
parent 8cf95456a5
commit aa4af48c7a
8 changed files with 54 additions and 62 deletions
@@ -12,27 +12,6 @@ import java.util.Map;
*/ */
@Data @Data
public class MtrPolicyConfigEO { public class MtrPolicyConfigEO {
private static final long serialVersionUID = 1L;
/** 主键ID */
private Long id;
/** 策略名称 */
private String policyName;
/** 优先级 */
private Long priority;
/** MTR客户端ID */
private String mtrClientId;
/** 服务器集合(换行符分割) */
private String serverGroup;
/** 探测目标ip集合 */
private String serveripGroup;
/** 是否探测(0-否,1-是) */
private Long probeFlag;
/** 开始时间 */ /** 开始时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@@ -0,0 +1,16 @@
package com.tongran.agent.client.core.vo;
import lombok.Data;
@Data
public class HopInfoVO {
private int hopNumber; // 跳数
private String ipAddress; // IP地址
private String hostname; // 主机名(如果有)
private double lossPercent; // 丢包率
private double avgLatency; // 平均延迟(ms)
private String country; // 国家
private String province; // 省份
private String city; // 城市
private String isp; // 运营商
}
@@ -3,6 +3,7 @@ package com.tongran.agent.client.core.vo;
import lombok.Data; import lombok.Data;
import java.util.Date; import java.util.Date;
import java.util.List;
@Data @Data
public class MtrVO { public class MtrVO {
@@ -14,4 +15,5 @@ public class MtrVO {
private long timestamp; // 探测时间戳 private long timestamp; // 探测时间戳
private boolean hasRetry; // 是否重试 private boolean hasRetry; // 是否重试
private String errorMsg; // 错误信息 private String errorMsg; // 错误信息
} private List<HopInfoVO> hopInfos; // 每跳路由信息列表
}
@@ -92,6 +92,8 @@ public class AppInitializer implements CommandLineRunner {
// 检测监控策略配置 // 检测监控策略配置
AssertLog.info("检测监控策略配置"); AssertLog.info("检测监控策略配置");
agentService.checkMonitor(); agentService.checkMonitor();
AssertLog.info("检测mtr探测策略配置");
agentService.checkMtrDelect();
}else{ }else{
//未注册,发送注册 //未注册,发送注册
try { try {
@@ -844,6 +844,17 @@ public class BusinessTasks {
long milli = AgentUtil.getMillisToNextMinute() + 60000; long milli = AgentUtil.getMillisToNextMinute() + 60000;
AssertLog.info("启动更新策略定时任务 - 延迟: {}ms, 间隔: {}ms", milli, 60000); AssertLog.info("启动更新策略定时任务 - 延迟: {}ms, 间隔: {}ms", milli, 60000);
dynamicTaskService.scheduleTask("policy", businessTasks::policyTask, milli, 60000); dynamicTaskService.scheduleTask("policy", businessTasks::policyTask, milli, 60000);
// 创建多网IP探测上报
AssertLog.info("启动多网IP探测定时任务 - 延迟: {}ms, 间隔: {}ms", milli, 300000);
dynamicTaskService.scheduleTask("networkDetect", businessTasks::networkDetectTask, milli, 300000);
// 检测监控策略配置
AssertLog.info("检测监控策略配置");
agentService.checkMonitor();
AssertLog.info("检测mtr探测策略配置");
agentService.checkMtrDelect();
}else{ }else{
//未注册,发送注册 //未注册,发送注册
try { try {
@@ -867,7 +878,7 @@ public class BusinessTasks {
//设置注册定时任务,收注册成功后取消该定时任务 //设置注册定时任务,收注册成功后取消该定时任务
dynamicTaskService.scheduleTask("register", businessTasks::registerTask, 0, 300000); dynamicTaskService.scheduleTask("register", businessTasks::registerTask, 0, 300000);
} }
dynamicTaskService.cancelTask("connectionTask"); dynamicTaskService.cancelTask("connection");
} }
AssertLog.info("建立连接重试定时任务执行 - task #{} completed", count); AssertLog.info("建立连接重试定时任务执行 - task #{} completed", count);
} }
@@ -30,4 +30,6 @@ public interface AgentService {
String getLogicalNode(); String getLogicalNode();
void checkMonitor(); void checkMonitor();
void checkMtrDelect();
} }
@@ -836,8 +836,8 @@ public class AgentServiceImpl implements AgentService {
} }
if(jsonObject.containsKey("mtrPolicys")){ if(jsonObject.containsKey("mtrPolicys")){
String mtrPolicys = jsonObject.getString("mtrPolicys"); String mtrPolicys = jsonObject.getString("mtrPolicys");
AssertLog.info("接收的mtrPolicys原始值: {}", mtrPolicys); AssertLog.debug("接收的mtrPolicys原始值: {}", mtrPolicys);
AssertLog.info("GlobalConfig.MTRPOLICYMSG当前值: {}", GlobalConfig.MTRPOLICYMSG); AssertLog.debug("GlobalConfig.MTRPOLICYMSG当前值: {}", GlobalConfig.MTRPOLICYMSG);
if(!mtrPolicys.equals(GlobalConfig.MTRPOLICYMSG)){ if(!mtrPolicys.equals(GlobalConfig.MTRPOLICYMSG)){
// 准备写入的内容 // 准备写入的内容
String[] lines = { String[] lines = {
@@ -1515,6 +1515,7 @@ public class AgentServiceImpl implements AgentService {
} }
@Override
public void checkMtrDelect(){ public void checkMtrDelect(){
File mtrFile = new File(properties.getConfPath() + "/mtrdelect.conf"); File mtrFile = new File(properties.getConfPath() + "/mtrdelect.conf");
if (mtrFile.exists()) { if (mtrFile.exists()) {
@@ -42,16 +42,20 @@ public class MtrServiceImpl implements MtrService {
if (!isPolicyTimeValid(policy, timestamp)) { if (!isPolicyTimeValid(policy, timestamp)) {
continue; continue;
} }
for (Map.Entry<String, List<String>> entry : policy.getClientIdToIpsMap().entrySet()) {
String[] targetIps = policy.getServeripGroup().split(";"); String clientId = entry.getKey();
for (String targetIp : targetIps) { List<String> ips = entry.getValue();
String ip = targetIp.trim(); if (ips != null) {
if (!ip.isEmpty()) { for (String ip : ips) {
// 异步执行每个IP的探测 String targetIp = ip.trim();
CompletableFuture<MtrVO> future = CompletableFuture.supplyAsync(() -> if (!targetIp.isEmpty()) {
executeSingleMtrProbe(ip, policy, timestamp) // 异步执行每个IP的探测
); CompletableFuture<MtrVO> future = CompletableFuture.supplyAsync(() ->
futures.add(future); executeSingleMtrProbe(targetIp, clientId, timestamp)
);
futures.add(future);
}
}
} }
} }
} }
@@ -101,38 +105,14 @@ public class MtrServiceImpl implements MtrService {
} }
} }
/**
* 预先构建IP到clientId的反向映射
*/
private Map<String, String> buildIpToClientIdMap(Map<String, List<String>> clientIdToIpMap) {
Map<String, String> ipToClientIdMap = new HashMap<>();
if (clientIdToIpMap == null) {
return ipToClientIdMap;
}
for (Map.Entry<String, List<String>> entry : clientIdToIpMap.entrySet()) {
String clientId = entry.getKey();
List<String> ips = entry.getValue();
if (ips != null) {
for (String ip : ips) {
ipToClientIdMap.put(ip.trim(), clientId);
}
}
}
return ipToClientIdMap;
}
/** /**
* 执行单个IP的MTR探测 * 执行单个IP的MTR探测
*/ */
private MtrVO executeSingleMtrProbe(String targetIp, MtrPolicyConfigEO policy, long timestamp) { private MtrVO executeSingleMtrProbe(String targetIp, String clientId, long timestamp) {
// 构建IP到clientId的反向映射 // 构建IP到clientId的反向映射
Map<String, String> ipToClientIdMap = buildIpToClientIdMap(policy.getClientIdToIpsMap());
MtrVO mtrVO = new MtrVO(); MtrVO mtrVO = new MtrVO();
mtrVO.setClientId(ipToClientIdMap.getOrDefault(targetIp, "unknown")); mtrVO.setClientId(clientId);
mtrVO.setTargetIp(targetIp); mtrVO.setTargetIp(targetIp);
mtrVO.setPolicyId(policy.getId());
mtrVO.setTimestamp(timestamp); mtrVO.setTimestamp(timestamp);
try { try {
@@ -158,7 +138,6 @@ public class MtrServiceImpl implements MtrService {
mtrVO.setHasRetry(false); mtrVO.setHasRetry(false);
AssertLog.info("目标IP {} 丢包率 {}%,无需重试", targetIp, firstLossPercent); AssertLog.info("目标IP {} 丢包率 {}%,无需重试", targetIp, firstLossPercent);
} }
return mtrVO; return mtrVO;
} catch (Exception e) { } catch (Exception e) {