使用zookeeper监听系统运行占用资源情况
- pom文件,关键包zkClient连接zookeeper,oshi-core获取系统信息 打包插件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.jaminye</groupId>
<artifactId>zookeeper_agent</artifactId>
<version>1.0-SNAPSHOT</version>
<name>zookeeper_agent</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 工具类-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.6</version>
</dependency>
<!-- 工具类依赖-->
<!-- https://mvnrepository.com/artifact/com.github.oshi/oshi-core -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>5.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<!-- zookeeper连接-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifestEntries>
<Project-name>${project.name}</Project-name>
<Project-version>${project.version}</Project-version>
<Premain-Class>cn.jaminye.Agent</Premain-Class>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
-
系统情况javaBean
package cn.jaminye; import lombok.Data; import java.util.Date; /** * 系统信息 * * @author jamin * @date 2020/10/24 6:16 下午 */ @Data public class OsBean { /** * pid */ private String pid; /** * 使用内存情况 */ private Long usedMemorySize; /** * 可用内存 */ private Long usableMemorySize; /** * ip */ private String ip; /** * cpu */ private Double cpu; /** * 最新更新时间 */ private Date lastUpdateTime; }
-
主类方法
package cn.jaminye; import cn.hutool.system.oshi.OshiUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.I0Itec.zkclient.ZkClient; import java.lang.instrument.Instrumentation; import java.lang.management.ManagementFactory; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Date; /** * @author jamin * @date 2020/10/24 5:47 下午 */ @Slf4j public class Agent { ZkClient zkClient; private static Agent ourInstance = new Agent(); /** * root节点 */ private final String ROOT_PATH = "/monitor"; /** * 服务地址 */ private final String SERVICE_PATH = ROOT_PATH + "/service"; private Thread stateThread; public static Agent getInstance() { return ourInstance; } /** * 临时节点 */ private String nodePath; public static void premain(String args, Instrumentation instrumentation) { Agent.getInstance().init(); } /** * 初始化 * * @author jamin * @date 2020/10/28 11:19 下午 */ public void init() { // 建立连接 zkClient = new ZkClient("10.211.55.4:2181", 5000, 10000); log.info("zookeeper连接成功"); //创建主目录 buildRoot(); //创建临时节点 createNode(); stateThread = new Thread(() -> { while (true) { // 更新数据 updateNode(); try { Thread.sleep(5000); System.out.println("线程休眠"); } catch (InterruptedException e) { e.printStackTrace(); } } }, "zk_stateThread"); stateThread.setDaemon(true); stateThread.start(); } /** * 修改临时节点数据 * * @author jamin * @date 2020/10/28 11:19 下午 */ private void updateNode() { zkClient.writeData(nodePath, getOsInfo()); } /** * 创建临时节点 * * @author jamin * @date 2020/10/28 11:20 下午 */ private void createNode() { nodePath = zkClient.createEphemeralSequential(SERVICE_PATH, getOsInfo()); log.info("创建服务节点"); } /** * 获取系统信息 * * @return {@link String} * @author jamin * @date 2020/10/28 11:20 下午 */ private String getOsInfo() { OsBean osBean = new OsBean(); osBean.setLastUpdateTime(new Date()); osBean.setIp(getLocalIp()); osBean.setCpu(OshiUtil.getCpuInfo().getUsed()); osBean.setUsedMemorySize((OshiUtil.getMemory().getTotal() - OshiUtil.getMemory().getAvailable()) / 1024 / 1024); osBean.setUsableMemorySize(OshiUtil.getMemory().getAvailable()); osBean.setPid(ManagementFactory.getRuntimeMXBean().getName()); ObjectMapper objectMapper = new ObjectMapper(); try { return objectMapper.writeValueAsString(osBean); } catch (JsonProcessingException e) { throw new RuntimeException(e); } } /** * 获取本机地址 * * @return {@link String} * @author jamin * @date 2020/10/24 6:28 下午 */ private String getLocalIp() { InetAddress inetAddress = null; try { inetAddress = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } assert inetAddress != null; return inetAddress.getHostAddress(); } /** * 创建根节点 * * @author jamin * @date 2020/10/24 6:06 下午 */ private void buildRoot() { if (!zkClient.exists(ROOT_PATH)) { zkClient.createPersistent(ROOT_PATH); } } }
-
使用package打包,找一个方法测试,方法无关只要程序一直在运行
package cn.jaminye.zookeeper.util; import lombok.extern.slf4j.Slf4j; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Id; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @author Jamin * @date 2020/9/15 18:05 */ @Slf4j public class Main { ZooKeeper zooKeeper; @Before public void init() throws IOException { zooKeeper = new ZooKeeper("10.211.55.4:2181", 10000, new Watcher() { @Override public void process(WatchedEvent event) { System.out.println(event.getPath()); System.out.println(event); } }); } @Test public void create() throws KeeperException, InterruptedException { byte[] data = zooKeeper.getData("/jamin", false, null); String s = new String(data); Thread.sleep(Integer.MAX_VALUE); System.out.println(s); } }
-
配置上传
javaagent:后面跟随我们刚才打的包 -
效果
- 控制台输出线程休眠
- 登陆zookeeper 可以看见系统数据已经保存
- 控制台输出线程休眠