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

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