#init
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.maibu.memory;
|
||||
|
||||
import com.fastbee.control.DiffSteer;
|
||||
import com.fastbee.netty.NettyClient;
|
||||
import io.vertx.core.Vertx;
|
||||
import lombok.Data;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Component
|
||||
@Data
|
||||
public class MiddleGlobalMemory {
|
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MiddleGlobalMemory.class);
|
||||
/**
|
||||
* -- GETTER --
|
||||
* 获取Vert.x实例
|
||||
*/
|
||||
public static Vertx vertx;
|
||||
|
||||
// 存储所有在线WebSocket连接
|
||||
// key: test:web:token
|
||||
public static final ConcurrentHashMap<String, WebSocket> onlineSockets = new ConcurrentHashMap<>();
|
||||
|
||||
// 当前账号控制中的设备 value: test:web:token key deviceId
|
||||
public static final ConcurrentHashMap<String, String> controlMap = new ConcurrentHashMap<>();
|
||||
|
||||
public static final ConcurrentHashMap<String, NettyClient> nettyClientMap = new ConcurrentHashMap<>();
|
||||
|
||||
public static DiffSteer diffSteer = null;
|
||||
|
||||
public static void initDiff() {
|
||||
if (diffSteer == null) {
|
||||
diffSteer = new DiffSteer();
|
||||
diffSteer.init(3000, 3000, 1000, 1000, 3000);
|
||||
diffSteer.setDirPolarity(
|
||||
DiffSteer.Polarity_Low,
|
||||
DiffSteer.Polarity_High,
|
||||
DiffSteer.Polarity_Low,
|
||||
DiffSteer.Polarity_High
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// web端操作的权限只能有一个web 端操作设备 key deviceId,value userName:web:token
|
||||
// // todo 发送请求 请求成功下发,一端下线后才能解锁
|
||||
// public static final ConcurrentHashMap<String, String> controlLockMap = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
//删除web端操作的控制锁
|
||||
// public static void removeLockByMaster(String value) {
|
||||
// if(!CollectionUtils.isEmpty(controlLockMap)){
|
||||
// controlLockMap.keySet().removeIf(controlLock -> controlLockMap.get(controlLock).equals(value));
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 关闭资源
|
||||
*/
|
||||
public void vertxClose() {
|
||||
if (vertx != null) {
|
||||
vertx.close(res -> {
|
||||
if (res.succeeded()) {
|
||||
logger.info("Vert.x 关闭成功");
|
||||
} else {
|
||||
logger.error("Vert.x 关闭失败: {}", res.cause().getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static String findBySocket(WebSocket socket) {
|
||||
return onlineSockets.entrySet().stream()
|
||||
.filter(entry -> Objects.equals(entry.getValue(), socket))
|
||||
.map(Map.Entry::getKey)
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
public static String findNettyClient(NettyClient nettyClient) {
|
||||
return nettyClientMap.entrySet().stream()
|
||||
.filter(entry -> Objects.equals(entry.getValue(), nettyClient))
|
||||
.map(Map.Entry::getKey)
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package com.maibu.memory;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.OperatingSystemMXBean;
|
||||
import java.lang.management.ThreadMXBean;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
// 系统监控类
|
||||
@Component
|
||||
public class SystemMonitor {
|
||||
private final Logger logger = LoggerFactory.getLogger(SystemMonitor.class);
|
||||
|
||||
// 启动监控任务
|
||||
@PostConstruct
|
||||
public void startMonitoring() {
|
||||
// JVM 内存监控
|
||||
startMemoryMonitor();
|
||||
|
||||
// CPU 使用率监控
|
||||
startCpuMonitor();
|
||||
|
||||
// 线程状态监控
|
||||
startThreadMonitor();
|
||||
|
||||
// 连接数监控
|
||||
startConnectionMonitor();
|
||||
}
|
||||
|
||||
// 内存监控
|
||||
private void startMemoryMonitor() {
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleAtFixedRate(() -> {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
long totalMemory = runtime.totalMemory() / (1024 * 1024);
|
||||
long freeMemory = runtime.freeMemory() / (1024 * 1024);
|
||||
long usedMemory = totalMemory - freeMemory;
|
||||
long maxMemory = runtime.maxMemory() / (1024 * 1024);
|
||||
|
||||
double memoryUsage = (double) usedMemory / maxMemory * 100;
|
||||
|
||||
logger.info("内存使用情况 - 总内存: {}MB, 已用内存: {}MB, 空闲内存: {}MB, 使用率: {}",
|
||||
totalMemory, usedMemory, freeMemory, String.format("%.2f", memoryUsage));
|
||||
|
||||
if (memoryUsage > 80) {
|
||||
logger.warn("内存使用率过高: {}", String.format("%.2f", memoryUsage));
|
||||
// 可以添加告警逻辑
|
||||
}
|
||||
}, 1, 5, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
// CPU 监控
|
||||
private void startCpuMonitor() {
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
// 使用 OperatingSystemMXBean 获取 CPU 使用率
|
||||
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
|
||||
if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
|
||||
com.sun.management.OperatingSystemMXBean sunOsBean =
|
||||
(com.sun.management.OperatingSystemMXBean) osBean;
|
||||
double cpuUsage = sunOsBean.getSystemCpuLoad() * 100;
|
||||
|
||||
logger.info("CPU 使用率: {}", String.format("%.2f", cpuUsage));
|
||||
|
||||
if (cpuUsage > 80) {
|
||||
logger.warn("CPU 使用率过高: {}", String.format("%.2f", cpuUsage));
|
||||
// 可以添加告警逻辑
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("CPU 监控异常: {}", e.getMessage());
|
||||
}
|
||||
}, 1, 5, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
// 线程监控
|
||||
private void startThreadMonitor() {
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleAtFixedRate(() -> {
|
||||
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
|
||||
int threadCount = threadBean.getThreadCount();
|
||||
int peakThreadCount = threadBean.getPeakThreadCount();
|
||||
long daemonThreadCount = threadBean.getDaemonThreadCount();
|
||||
|
||||
logger.info("线程状态 - 当前线程数: {}, 峰值线程数: {}, 守护线程数: {}",
|
||||
threadCount, peakThreadCount, daemonThreadCount);
|
||||
|
||||
// 检查死锁
|
||||
long[] deadlockedThreads = threadBean.findDeadlockedThreads();
|
||||
if (deadlockedThreads != null && deadlockedThreads.length > 0) {
|
||||
logger.error("发现死锁线程: {}", Arrays.toString(deadlockedThreads));
|
||||
// 可以添加告警逻辑
|
||||
}
|
||||
}, 1, 10, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
// 连接数监控
|
||||
private void startConnectionMonitor() {
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleAtFixedRate(() -> {
|
||||
int webSocketCount = MiddleGlobalMemory.onlineSockets.size();
|
||||
int nettyClientCount = MiddleGlobalMemory.nettyClientMap.size();
|
||||
|
||||
logger.info("连接数统计 - WebSocket连接数: {}, Netty客户端数: {}",
|
||||
webSocketCount, nettyClientCount);
|
||||
}, 1, 5, TimeUnit.MINUTES);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user