You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trea1/ServiceMonitor.java

108 lines
3.7 KiB

package com.soa.monitoring;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
/**
* 服务监控组件 - 负责监控服务的性能、可用性和错误率
*/
public class ServiceMonitor {
private static final Logger logger = LogManager.getLogger(ServiceMonitor.class);
private static ServiceMonitor instance;
private final Map<String, ServiceMetrics> metricsMap = new ConcurrentHashMap<>();
private ServiceMonitor() {}
/**
* 获取监控器单例
*/
public static synchronized ServiceMonitor getInstance() {
if (instance == null) {
instance = new ServiceMonitor();
}
return instance;
}
/**
* 记录服务调用开始
*/
public long startMonitoring(String serviceName) {
return System.currentTimeMillis();
}
/**
* 记录服务调用结束
*/
public void endMonitoring(String serviceName, long startTime, boolean success) {
long executionTime = System.currentTimeMillis() - startTime;
ServiceMetrics metrics = metricsMap.computeIfAbsent(serviceName, k -> new ServiceMetrics());
metrics.recordCall(executionTime, success);
logger.info("Service: {} - Execution time: {}ms, Status: {}",
serviceName, executionTime, success ? "SUCCESS" : "FAILED");
// 检查性能阈值
if (executionTime > 1000) { // 1秒阈值
logger.warn("Service {} performance warning: execution time {}ms exceeds threshold",
serviceName, executionTime);
}
}
/**
* 获取服务指标
*/
public ServiceMetrics getServiceMetrics(String serviceName) {
return metricsMap.get(serviceName);
}
/**
* 打印所有服务的监控报告
*/
public void printMonitoringReport() {
logger.info("===== SERVICE MONITORING REPORT =====");
for (Map.Entry<String, ServiceMetrics> entry : metricsMap.entrySet()) {
String serviceName = entry.getKey();
ServiceMetrics metrics = entry.getValue();
logger.info("Service: {}", serviceName);
logger.info(" Total calls: {}", metrics.getTotalCalls());
logger.info(" Successful calls: {}", metrics.getSuccessfulCalls());
logger.info(" Failed calls: {}", metrics.getFailedCalls());
logger.info(" Avg response time: {}ms", metrics.getAvgResponseTime());
logger.info(" Error rate: {}%", metrics.getErrorRate());
}
logger.info("===================================");
}
/**
* 服务指标内部类
*/
public static class ServiceMetrics {
private long totalCalls = 0;
private long successfulCalls = 0;
private long failedCalls = 0;
private long totalResponseTime = 0;
public synchronized void recordCall(long executionTime, boolean success) {
totalCalls++;
totalResponseTime += executionTime;
if (success) {
successfulCalls++;
} else {
failedCalls++;
}
}
public long getTotalCalls() { return totalCalls; }
public long getSuccessfulCalls() { return successfulCalls; }
public long getFailedCalls() { return failedCalls; }
public double getAvgResponseTime() {
return totalCalls > 0 ? (double) totalResponseTime / totalCalls : 0;
}
public double getErrorRate() {
return totalCalls > 0 ? (double) failedCalls / totalCalls * 100 : 0;
}
}
}