阿里诊断工具arthas (阿里java工具类)

全文目录

  • Arthas简介
  • Arthas 使用场景
  • Arthas 使用
  • 最后

Arthas简介

Arthas Alibaba 在 2018 年 9 月开源的 Java 诊断 工具。支持 JDK6+ , 采用命令行交互模式,可以方便的定位和诊断线上程序运行问题。 Arthas 官方文档十分详细,详见: https://alibaba.github.io/arthas

阿里arthas原理,java诊断工具有哪些

Arthas使用场景

阿里巴巴的 aArthas 有强大且丰富的功能,在 Arthas 中能做的事情超乎想象。下面是 aArthas 常用的一些还有场景,更多的使用场景可以在熟悉了 Arthas 之后自行探索。

1. 能否有一个全局视角来查看系统的运行状况?

2. 为什么系统的 CPU 又升高了 ?

3. 系统运行的多线程有死锁吗?

4. 程序运行耗时很长,如何监测呢?

5. 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?

6. 我改的代码为什么没有执行到?

7. 有什么办法可以监控到 JVM 的实时运行状态?

Arthas使用

1 github*载下**arthas

2 https://alibaba.github.io/arthas/arthas‐boot.jar

3 或者 Gitee *载下**

4 https://arthas.gitee.io/arthas‐boot.jar

用java -jar运行即可,可以识别机器上所有Java进程

阿里arthas原理,java诊断工具有哪些

package com.tuling.jvm;
import java.util.HashSet;
public class Arthas {
    private static HashSet hashSet = new HashSet();
    public static void main(String[] args) {
    // 模拟 CPU 过高
    cpuHigh();
    // 模拟线程死锁
    deadThread();
    // 不断的向 hashSet 集合增加数据
    addHashSetThread();
}

/**
 * 不断的向 hashSet 集合添加数据
 */
public static void addHashSetThread() {
// 初始化常量
new Thread(() ‐> {
  int count = 0;
  while (true) {
    try {
    hashSet.add("count" + count);
    Thread.sleep(1000);
    count++;
  } catch (InterruptedException e) {
 	 e.printStackTrace();32 }
  }
  }).start();
}

public static void cpuHigh() {
  new Thread(() ‐> {
    while (true) {

     }
  }).start();
}

/**
* 死锁
 */
private static void deadThread() {
    /** 创建资源 */
    Object resourceA = new Object();
    Object resourceB = new Object();
    // 创建线程
    Thread threadA = new Thread(() ‐> {
    synchronized (resourceA) {
    System.out.println(Thread.currentThread() + " get ResourceA");
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread() + "waiting get resourceB");
    synchronized (resourceB) {
    System.out.println(Thread.currentThread() + " get resourceB");
    }
     }
});

Thread threadB = new Thread(() ‐> {
    synchronized (resourceB) {
    System.out.println(Thread.currentThread() + " get ResourceB");
     try {
    Thread.sleep(1000);
     } catch (InterruptedException e) {
     e.printStackTrace();
    }
    System.out.println(Thread.currentThread() + "waiting get resourceA");
    synchronized (resourceA) {
    System.out.println(Thread.currentThread() + " get resourceA");
    }
    }
    });
    threadA.start();
    threadB.start();
    }
}

选择进程序号1,进入进程信息操作

阿里arthas原理,java诊断工具有哪些

输入 dashboard 可以查看整个进程的运行情况,线程、内存、GC、运行环境信息:

阿里arthas原理,java诊断工具有哪些

输入 thread 可以查看线程详细情况

阿里arthas原理,java诊断工具有哪些

输入 thread加上线程ID 可以查看线程堆栈

阿里arthas原理,java诊断工具有哪些

输入 thread -b 可以查看线程死锁

阿里arthas原理,java诊断工具有哪些

输入 jad加类的全名 可以反编译,这样可以方便我们查看线上代码是否是正确的版本

阿里arthas原理,java诊断工具有哪些

使用 ognl 命令可以查看线上系统变量的值,甚至可以修改变量的值

阿里arthas原理,java诊断工具有哪些

最后

几个简单的字符,就能创造出欢乐,

几个简单的号码,便能写出奇迹。

一个键盘,就能畅游世界,

一根网线,便能知晓天下。

创作不易,感谢大家的支持。后续也会分享更多的干货和技术资讯,您的阅读就是对小编最大支持,再次感谢各位老铁!