arthas簡單使用
- 2019 年 11 月 6 日
- 筆記
簡介
Arthas 是Alibaba開源的Java診斷工具,深受開發者喜愛,項目地址.當你遇到以下類似問題而束手無策時,Arthas可以幫助你解決:
- 這個類從哪個 jar 包載入的?為什麼會報各種類相關的 Exception?
- 我改的程式碼為什麼沒有執行到?難道是我沒 commit?分支搞錯了?
- 遇到問題無法在線上 debug,難道只能通過加日誌再重新發布嗎?
- 線上遇到某個用戶的數據處理有問題,但線上同樣無法 debug,線下無法重現!
- 是否有一個全局視角來查看系統的運行狀況?
- 有什麼辦法可以監控到JVM的實時運行狀態? Arthas支援JDK 6+,支援Linux/Mac/Windows,採用命令行交互模式,同時提供豐富的 Tab 自動補全功能,進一步方便進行問題的定位和診斷.
快速使用
官方推薦通過arthas-boot方式安裝,下載 arthas-boot.jar
,然後用 java-jar
的方式啟動:
wget https://alibaba.github.io/arthas/arthas-boot.jar java -jar arthas-boot.jar
當然如果有其他的需求,可以使用全量安裝、手動安裝的方式,具體參考Arthas Install
Simplecase
dubbo消費端調用服務時,會為介面生成一個代理類,這個代理類有什麼資訊呢,可以通過arthas可以反編譯出Protocol、Cluster、Transporter、Wrapper等代理類,幫助理解源碼.
- 啟動dubbo消費端服務
- 啟動arthas服務
➜ arthas java -jar arthas-boot.jar [INFO] arthas-boot version: 3.1.4 [INFO] Found existing java process, please choose one and hit RETURN. * [1]: 1089 zookeeper-dev-ZooInspector.jar [2]: 982 org.jetbrains.kotlin.daemon.KotlinCompileDaemon [3]: 1623 org.apache.dubbo.demo.provider.ApplicationProvider1 [4]: 1627 org.jetbrains.jps.cmdline.Launcher [5]: 1628 org.apache.dubbo.demo.consumer.ApplicationConsumer [6]: 300 [7]: 637 org.jetbrains.idea.maven.server.RemoteMavenServer36 [8]: 957 org.apache.zookeeper.server.quorum.QuorumPeerMain [9]: 958 org.apache.zookeeper.server.quorum.QuorumPeerMain [10]: 959 org.apache.zookeeper.server.quorum.QuorumPeerMain 5 [INFO] arthas home: /Users/lioswong/.arthas/lib/3.1.4/arthas [INFO] Try to attach process 1628 [INFO] Attach process 1628 success. [INFO] arthas-client connect 127.0.0.1 9998 ,---. ,------. ,--------.,--. ,--. ,---. ,---. / O | .--. ''--. .--'| '--' | / O ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || | | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki https://alibaba.github.io/arthas tutorials https://alibaba.github.io/arthas/arthas-tutorials version 3.1.4 pid 1628 time 2019-10-15 14:01:56
arthas啟動時,會發現dubbo服務消費者進程是第5個,則輸入5,再輸入回車/enter,Arthas會attach到目標進程上,並輸出日誌.
- 根據
sc
命令搜索JVM已載入的類資訊
[arthas@1628]$ sc *proxy0 org.apache.dubbo.common.bytecode.proxy0 Affect(row-cnt:1) cost in 22 ms.
- 通過
jad
命令反編譯
[arthas@1628]$ jad org.apache.dubbo.common.bytecode.proxy0 ClassLoader: +-sun.misc.Launcher$AppClassLoader@18b4aac2 +-sun.misc.Launcher$ExtClassLoader@153f5a29 Location: /Users/lioswong/dev/source/dubbo/dubbo-common/target/classes/ /* * Decompiled with CFR. * * Could not load the following classes: * com.alibaba.dubbo.rpc.service.EchoService * org.apache.dubbo.common.bytecode.ClassGenerator * org.apache.dubbo.common.bytecode.ClassGenerator$DC */ package org.apache.dubbo.common.bytecode; import com.alibaba.dubbo.rpc.service.EchoService; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import org.apache.dubbo.common.bytecode.ClassGenerator; import org.apache.dubbo.demo.DemoService; public class proxy0 implements ClassGenerator.DC, EchoService, DemoService { public static Method[] methods; private InvocationHandler handler; @Override public String sayHello(String string) { Object[] arrobject = new Object[]{string}; Object object = this.handler.invoke(this, methods[0], arrobject); return (String)object; } public Object $echo(Object object) { Object[] arrobject = new Object[]{object}; Object object2 = this.handler.invoke(this, methods[1], arrobject); return object2; } public proxy0() { } public proxy0(InvocationHandler invocationHandler) { this.handler = invocationHandler; } } Affect(row-cnt:1) cost in 571 ms.
上面簡單介紹arthas使用.
進階使用
可參考官方中文文檔