Arthas 是 阿里巴巴最近開源出來的一個針對 java 的工具,主要是針對 java 的問題進行診斷!java
這個工具能夠協助你作下面這些事情:git
下載地址:http://search.maven.org/classic/#search%7Cga%7C1%7Cg%3A%22com.taobao.arthas%22%20AND%20a%3A%22arthas-packaging%22github
下載完成後,解壓縮,以下圖所示::web
在Download
欄下載最新的 bin.zip
包,解壓後在bin目錄有 as.bat
。此腳本暫時只接受一個參數 pid,即只能診斷本機上的 Java 進程。express
啓動命令爲:apache
as.bat <pid>tomcat
注:我在 window 10 上面啓動的時候遇到以下問題,websocket
D:\download\arthas-packaging-3.0.4-bin>telnet
'telnet' 不是內部或外部命令,也不是可運行的程序
或批處理文件。session
解決辦法爲:「控制面板」 ——> 「啓動或關閉Windows功能」 ——> 勾選 「Telnet 功能」dom
安裝Arthas:
curl -L https://alibaba.github.io/arthas/install.sh | sh
啓動Arthas:
./as.sh
成功啓動後,會看到以下界面。
help——查看命令幫助信息
cls——清空當前屏幕區域
session——查看當前會話的信息
reset——重置加強類,將被 Arthas 加強過的類所有還原,Arthas 服務端關閉時會重置全部加強過的類
version——輸出當前目標 Java 進程所加載的 Arthas 版本號
quit——退出當前 Arthas 客戶端,其餘 Arthas 客戶端不受影響
shutdown——關閉 Arthas 服務端,全部 Arthas 客戶端所有退出
keymap——Arthas快捷鍵列表及自定義快捷鍵
dashboard——當前系統的實時數據面板
thread——查看當前 JVM 的線程堆棧信息
jvm——查看當前 JVM 的信息
sysprop——查看和修改JVM的系統屬性
New! getstatic——查看類的靜態屬性
sc——查看JVM已加載的類信息
sm——查看已加載類的方法信息
dump——dump 已加載類的 byte code 到特定目錄
redefine——加載外部的.class文件,redefine到JVM裏
jad——反編譯指定已加載類的源碼
classloader——查看classloader的繼承樹,urls,類加載信息,使用classloader去getResource
請注意,這些命令,都經過字節碼加強技術來實現的,會在指定類的方法中插入一些切面來實現數據統計和觀測,所以在線上、預發使用時,請儘可能明確須要觀測的類、方法以及條件,診斷結束要執行 shutdown 或將加強過的類執行 reset 命令。
monitor——方法執行監控
watch——方法執行數據觀測
trace——方法內部調用路徑,並輸出方法路徑上的每一個節點上耗時
stack——輸出當前方法被調用的調用路徑
tt——方法執行數據的時空隧道,記錄下指定方法每次調用的入參和返回信息,並能對這些不一樣的時間下調用進行觀測
options——查看或設置Arthas全局開關
Arthas支持使用管道對上述命令的結果進行進一步的處理,如sm org.apache.log4j.Logger | grep
grep——搜索知足條件的結果
plaintext——將命令的結果去除顏色
wc——按行統計輸出結果
經過websocket鏈接Arthas。
Web Console
異步命令支持
執行結果存日誌
批處理的支持
ognl表達式的用法說明
首先,在窗口中,輸入 help 查看一下全部提供的可用命令(他的通訊本質是經過 telnet 協議來通訊的),以下圖:
Attach success. Connecting to arthas server... current timestamp is 1537266148 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. ,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki: https://alibaba.github.io/arthas version: 3.0.4 pid: 25206 timestamp: 1537266148841 $ help NAME DESCRIPTION help Display Arthas Help keymap Display all the available keymap for the specified connection. sc Search all the classes loaded by JVM sm Search the method of classes loaded by JVM classloader Show classloader info jad Decompile class getstatic Show the static field of a class monitor Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. stack Display the stack trace for the specified class and method thread Display thread info, thread stack trace Trace the execution time of specified method invocation. watch Display the input/output parameter, return object, and thrown exception of specified method invocation tt Time Tunnel jvm Display the target JVM information dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info. dump Dump class byte array from JVM options View and change various Arthas options cls Clear the screen reset Reset all the enhanced classes version Display Arthas version shutdown Shut down Arthas server and exit the console session Display current session information sysprop Display, and change the system properties. redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...) $
這裏主要說一下 watch ,這個命令對變量進行數據監測。
==========================================================================
首先貼上個人測試代碼:
package com.oct.tail; import java.util.UUID; /** * @Author Ryan * @Date 2018/9/18 9:58 * @desc */ public class OtherTestCase { /** * * @return */ public static String uuid(){ return UUID.randomUUID().toString().replaceAll("-", ""); } public static void main(String[] args) { while(true){ System.out.println("uuid = " + uuid()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
以下我作一個示例 ( 本人是基於 Window 10 , JDK 8 環境, Linux 雷同),(對於 watch 命令我僞裝不知道如何使用,當即輸入 watch help 來看看會有什麼玩意兒):
,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki: https://alibaba.github.io/arthas version: 3.0.4 pid: 11924 timestamp: 1537326702039 $ watch -help USAGE: watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class- pattern method-pattern express [condition-express] SUMMARY: Display the input/output parameter, return object, and thrown exception of specif ied method invocation The express may be one of the following expression (evaluated dynamically): target : the object clazz : the object's class method : the constructor or method params[0..n] : the parameters of method returnObj : the returned object of method throwExp : the throw exception of method isReturn : the method ended by return isThrow : the method ended by throwing exception #cost : the execution time in ms of method invocation Examples: watch -Eb org\.apache\.commons\.lang\.StringUtils isBlank params[0] watch -b org.apache.commons.lang.StringUtils isBlank params[0] watch -f org.apache.commons.lang.StringUtils isBlank returnObj watch -bf *StringUtils isBlank params[0] watch *StringUtils isBlank params[0] watch *StringUtils isBlank params[0] params[0].length==1 watch *StringUtils isBlank '#cost>100' WIKI: https://alibaba.github.io/arthas/watch OPTIONS: -b, --before Watch before invocation -e, --exception Watch after throw exception -x, --expand <value> Expand level of object (1 by default) -f, --finish Watch after invocation, enable by default -h, --help this help -n, --limits <value> Threshold of execution times -E, --regex Enable regular expression to match (wildcard matching b y default) -M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1 024 by default) -s, --success Watch after successful invocation <class-pattern> The full qualified class name you want to watch <method-pattern> The method name you want to watch <express> the content you want to watch, written by ognl. Examples: params[0] 'params[0]+params[1]' returnObj throwExp target clazz method <condition-express> Conditional expression in ognl style, for example: TRUE : 1==1 TRUE : true FALSE : false TRUE : 'params.length>=0' FALSE : 1==2 $
在這裏,咱們針對方法 uuid() 返回值進行監測。監測結果以下:
$ $ $ watch -f com.oct.tail.OtherTestCase uuid returnObj Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 18 ms. ts=2018-09-19 11:13:48;result=@String[26c80eb505664dbcb14f8d810fb4811c] ts=2018-09-19 11:13:49;result=@String[fc03c43864f94372b646ce6253d90646] ts=2018-09-19 11:13:50;result=@String[55ff41e0d66347c2bc75ab8ff4ffda4e] ts=2018-09-19 11:13:51;result=@String[c504388c0aa74458a41a1b3a77c3d536] ts=2018-09-19 11:13:52;result=@String[18d59c09ffde4c7aab15feb88b3e433f] ts=2018-09-19 11:13:53;result=@String[c19dd8c1e5f8442696c8f886e81e74d5] ts=2018-09-19 11:13:54;result=@String[d37a74aa502f4897aa1ed84dc69b83d8] ts=2018-09-19 11:13:55;result=@String[cc11753b6f424c1e9a6a1ab36f334349] ts=2018-09-19 11:13:56;result=@String[75a9b3c0bed4426d9363168912f16d74] ts=2018-09-19 11:13:57;result=@String[f13022118e5a4115800a6eacc480e6a8]
簡直方便的死去活來!太感動了。