arthas是阿里開源的一個java線上監控以及診斷工具,在docker容器中我們無需重啟服務,也不用更改代碼,就可以完成對應用內存、線程、日志級別的修改、方法調用的出入參、異常監測、執行耗時等,xxxx.xxxx.xxxxx為脫敏內容
1. 在docker容器中怎么安裝和啟動arthas
1.1 遠程下載arthas的jar
curl -O https://arthas.aliyun.com/arthas-boot.jar
1.2 啟動arthas
java -jar arthas-boot.jar
2. arthas可以解決哪些問題以及怎么解決
2.1 線上發布的容器內是否是包含最新編寫的代碼?反編譯
jad com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance
2.2 線上加載的環境變量有哪些,變量值是什么?
sysenv
sysenv JAVA_OPTS
ognl '#env=@System@getenv(),{#env}'
ognl '#config=@System@getenv("proxy.config"),{#config}'
2.3 線上的系統屬性有哪些,屬性值是什么?
sysprop
sysprop nacos.config.password
sysprop nacos.config.password test 永久修改
ognl '#properties=@System@getProperties(),{#properties}'
ognl '#SW_AGENT_NAME=@System@getProperty("SW_AGENT_NAME"),{#SW_AGENT_NAME}'
2.4 打印日志級別較低的日志:FATAL > ERROR > WARN > INFO > DEBUG > TRACE(未生效)
logger-- root路徑下整體更新
logger --name ROOT --level debug-- 沒有特別指定日志等級
logger -n com.xxxx.xxxx.xxxxx.cache;-- 查看classloader hashcode
sc -d com.xxxx.xxxx.xxxxx.cache;-- 根據hashcode更新level
logger -c 5cc69cfe --name ROOT --level debug
2.5 查看類的靜態屬性以及對靜態屬性的處理
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCachegetstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'entrySet().iterator.{? #this.key=="I_REDPAY_P01_REDPAYGETORDER_1.0_DEF"}'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'get("I_REDPAY_P01_REDPAYGETORDER_1.0_DEF")'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'get("I_REDPAY_P01_REDPAYGETORDER_1.0_DEF").getCode()'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'size()'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'put("1","2")'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'entrySet().iterator.{? #this.key=="1"}'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'remove("1")'sc -d com.xxxx.xxxx.xxxxx.cache.ApiDefCache
ognl '@com.xxxx.xxxx.xxxxx.cache.ApiDefCache@apiDefCache'(不一定可以找到類,因為默認只會去SystemClassLoader里找類)
ognl -c 5cc69cfe '@com.xxxx.xxxx.xxxxx.cache.ApiDefCache@apiDefCache'getstatic com.xxxx.xxxx.xxxxx.util.ProxyUtil proxySelectFromDbSwitch
getstatic com.xxxx.xxxx.xxxxx.util.ProxyUtil proxySelectFromDbSwitch '#this.value=true'
ognl -c 5cc69cfe '@com.xxxx.xxxx.xxxxx.util.ProxyUtil@proxySelectFromDbSwitch'
ognl -c 5cc69cfe '#value=true, @com.xxxx.xxxx.xxxxx.util.ProxyUtil@proxySelectFromDbSwitch.value=#value'
2.6 查看實例對象的方法調用以及實例對象的屬性
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send-- -x 指定輸出結果的屬性遍歷深度
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -x 3-- 只查看入參
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{params}"-- 只查看第一個入參
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{params[0]}"-- 只查看第一個入參的code字段
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{params[0].code}"-- 只查看第一個入參且入參的code字段值為payment
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send '{params[0]}' '{params[0].code=="payment"}'-- 只查看方法所在類對象實例
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{traget}"-- 只查看方法所在類對象實例的defaultOkHttpClient字段
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{target.defaultOkHttpClient}" -x 3-- 只查看方法的返回結果
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{result}"-- 查看方法的入參,所在類實例,返回結果
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{params, target, result}"-- 在方法調用之前觀察(返回值,異常均不存在)
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -b-- 在方法調用異常之后觀察
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -e-- 在方法返回之后觀察(默認)
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -s-- 在方法結束之后觀察
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -fAtEnter,AtExit,AtExceptionExit。對應函數入口,函數正常 return,函數拋出異常-- 當方法耗時超過200ms才進行觀察
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send '{params, returnObj}' '#cost>200' -x 2
2.7 調整展示結果的形式為json
options json-format true
2.8 方法調用鏈路分析
stack com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send
stack com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send '#cost>200'
2.9 將stack、watch等命令在后臺執行并將結果輸出到指定文件目錄
stack com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send >> stack.out & -- 查看
cat stack.out-- 查看后臺執行的任務
jobs
-- 停止后臺任務
kill <job-id>
-- 后臺任務最多支持同時執行8個,默認一天后會超時自動關閉,可以通過options的job-timeout調整超時時間
2.10 容器內部線程分析
–查看最繁忙的前3個線程
thread -n 3
3. 高級用法
- 獲取Spring context,然后調用bean執行方法等等:
- https://github.com/alibaba/arthas/issues/482
- 本地代碼熱部署到遠程服務器:
- https://arthas.aliyun.com/doc/idea-plugin.html
4. 官方文檔
https://arthas.aliyun.com/doc