標籤:import logs 應用 maven har depend put github void
maven 引用
<dependency> <groupId>com.github.dblock</groupId> <artifactId>oshi-core</artifactId> <version>LATEST</version></dependency>
取得 cpu資訊 樣本
import oshi.SystemInfo;import oshi.hardware.CentralProcessor;import oshi.hardware.HardwareAbstractionLayer;import oshi.software.os.OperatingSystem;public class ComputerIdentifier{ public static String generateLicenseKey() throws Exception { SystemInfo systemInfo = new SystemInfo(); OperatingSystem operatingSystem = systemInfo.getOperatingSystem(); HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware(); CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor(); String vendor = operatingSystem.getManufacturer(); String processorSerialNumber = centralProcessor.getSystemSerialNumber(); String processorIdentifier = centralProcessor.getIdentifier(); int processors = centralProcessor.getLogicalProcessorCount(); String delimiter = "#"; return vendor + delimiter + processorSerialNumber + delimiter + processorIdentifier + delimiter + processors; } public static void main(String[] arguments) throws Exception { String identifier = generateLicenseKey(); System.out.println(identifier); }}
擷取jvm資料
jvm資料是監控應用很重要的一系列參數,一般本地開發的時候可以通過jconsole來連到對應的進程上面,查看相關指標資料,但是線上上環境就不適合通過jconsole來查看了, 所以我們現在使用通過java代碼來獲得資料,然後上報出去,然後在外部通過展示。
那麼如何通過java代碼來得到這些參數呢?
獲得jvm的堆記憶體代碼
MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();System.out.println("jvm.heap.init is " + (heapMemoryUsage.getInit()));System.out.println("jvm.heap.used is " + (heapMemoryUsage.getUsed()));System.out.println("jvm.heap.committed is " + (heapMemoryUsage.getCommitted()));System.out.println("jvm.heap.max is " + (heapMemoryUsage.getMax()));
獲得jvm的非堆記憶體代碼
MemoryUsage nonHeapMemoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();System.out.println("jvm.nonheap.init is " + (nonHeapMemoryUsage.getInit()));System.out.println("jvm.nonheap.used is " + (nonHeapMemoryUsage.getUsed()));System.out.println("jvm.nonheap.committed is " + (nonHeapMemoryUsage.getCommitted()));System.out.println("jvm.nonheap.max is " + (nonHeapMemoryUsage.getMax()));
上面的方法只能得到jvm的堆和非堆的整體資料,一般都知道堆和非堆裡面都幾個不同的區,用來做不同功能,那麼如何得到不同區的資料呢?不多說,上代碼
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { final String kind = pool.getType() == MemoryType.HEAP ? "heap" : "nonheap"; final MemoryUsage usage = pool.getUsage(); System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".init is " + usage.getInit()); System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".used is " + usage.getUsed()); System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName()+ ".committed is " + usage.getCommitted()); System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".max is " + usage.getMax()); }
這樣就可以得到各種區的具體參數。
java 通用取得 系統硬體資訊及 jvm 資訊的 jar 包 oshi-core