Arthas小工具学习

Arthas是什么?

Arthas 是Alibaba开源的Java诊断工具

Arthas能做什么?

*Arthas可以帮助你解决如下问题:

  • 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
  • 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
  • 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
  • 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
  • 是否有一个全局视角来查看系统的运行状况?
  • 有什么办法可以监控到JVM的实时运行状态?
  • 怎么快速定位应用的热点,生成火焰图?

安装

curl -O https://arthas.aliyun.com/arthas-boot.jar

使用

在使用之前,要牢记一点:arthas这个插件必须是在应用程序启动时才可用

  1. 编辑一个简单的应用程序
@SpringBootApplication
public class SpringbootJwtApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootJwtApplication.class, args);
    }
}
@RestController
public class JvmStackDeadStackDemo {

    @GetMapping("deadLock")
    public String deadLock(){
        Object lock1 = new Object();
        Object lock2 = new Object();
        new Thread(() -> {
            // 获取自己的锁
            synchronized (lock1){
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 获取他人的锁
                synchronized (lock2){
                    System.err.println("thread1 try hold lock2");
                }
            }
        },"thread1").start();

        new Thread(() -> {
            // 获取自己的锁
            synchronized (lock2){
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 获取他人的锁
                synchronized (lock1){
                    System.err.println("thread2 try hold lock1");
                }
            }
        },"thread2").start();
        return "deadLock";
    }
}

启动应用

java -jar springboot-jwt.jar &

  1. 进入arthas
java -jar arthas.jar 

选择我们的应用pid

1
[INFO] Start download arthas from remote server: https://arthas.aliyun.com/download/3.4.6?mirror=aliyun
[INFO] File size: 11.99 MB, downloaded size: 7.11 MB, downloading ...
[INFO] Download arthas success.
[INFO] arthas home: /home/lb/.arthas/lib/3.4.6/arthas
[INFO] Try to attach process 1235
[INFO] Attach process 1235 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'                          
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.                          
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |                         
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'                          
                                                                                

wiki      https://arthas.aliyun.com/doc                                         
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html                   
version   3.4.6                                                                 
pid       1235                                                                  
time      2021-02-01 09:48:27                                                   
# 已经进入1235应用的控制台了
[arthas@1235]$ 

输入dashbord命令,我们可以查看当前应用的系统情况

# 支持tab键提示
[arthas@1235]$ dashboard 

测试

  1. 访问接口localhost:8081/deadLock
  2. 检查是否有死锁
    • 在arthas控制台执行thread -b命令

更多命令请参照官方文档:https://arthas.aliyun.com/doc/advanced-use.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章