SpringBootAdmin-clinet自定义监控CPU、内存、磁盘等health
直接上代码:
cpu、内存使用的包 引入一下。
<dependency>
<groupId>com.pixel.base</groupId>
<artifactId>base-admin-client-starter</artifactId>
<version>1.0.1-gaiya-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.17.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.17.0</version>
</dependency>
自定义监控扩展
package com.pixel.monitorclient;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.*;
import oshi.util.Util;
import java.io.File;
import java.util.concurrent.atomic.AtomicLong;
/**
* @program: hangjizhe-cloud
* @description: 测试 HealthIndicator
* @author: <林发和-694204477@qq.com>
* @create: 2025-03-17 11:29
**/
@Component
@Slf4j
public class CpuMemoryHealthIndicator extends AbstractHealthIndicator {
private static final AtomicLong count = new AtomicLong(0);
private static final SystemInfo SYSTEM_INFO = new SystemInfo();
private static final CentralProcessor cpu = SYSTEM_INFO.getHardware().getProcessor();
private static final GlobalMemory memory = SYSTEM_INFO.getHardware().getMemory();
// private static final HardwareAbstractionLayer hardware = SYSTEM_INFO.getHardware();
// private static final OperatingSystem os = SYSTEM_INFO.getOperatingSystem();
// private static final CentralProcessor processor = SYSTEM_INFO.getHardware().getProcessor();
// 获取 CPU 使用率(%)
//--- CPU 监控 ---
public static double getCpuUsage() {
long[] prevTicks = cpu.getSystemCpuLoadTicks();
Util.sleep(1000); // 计算 1 秒内的差值
return Math.round(cpu.getSystemCpuLoadBetweenTicks(prevTicks) * 10000.0) / 100.0;
}
// 获取内存使用率(%)
//--- 内存监控 ---
public static double getMemoryUsage() {
long total = memory.getTotal();
long available = memory.getAvailable();
return Math.round((total - available) * 10000.0 / total) / 100.0;
}
// 获取磁盘使用率(示例路径为 "/")
public static double getDiskUsage() {
File root = new File("/");
long total = root.getTotalSpace();
long free = root.getFreeSpace();
return Math.round((total - free) * 10000.0 / total) / 100.0;
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
log.info("TestHealthIndicator 1: doHealthCheck: cpuUsage: {}, memoryUsage: {}", getCpuUsage(), getMemoryUsage());
long get = count.addAndGet(1);
log.info("TestHealthIndicator : doHealthCheck: {} ,get % 2 : {}", get, get % 2);
// if (get % 2 == 0) {
// builder.up();
// } else {
// builder.down();
// }
builder.withDetail("countA", count)
.withDetail("cpuUsage", getCpuUsage())
.withDetail("memoryUsage", getMemoryUsage())
// .withDetail("disk.usage", getDiskUsage())
.withDetail("countB", count);
builder.up();
}
public static void main(String[] args) {
System.out.println(getDiskUsage());
Health.Builder builder = new Health.Builder();
builder.withDetail("cpu.usage", getCpuUsage())
.withDetail("memory.usage", getMemoryUsage())
.withDetail("disk.usage", getDiskUsage());
Health build = builder.build();
System.out.println(JSON.toJSONString(build));
}
}