通過arthas vmtool 調用線上正在運行的service方法
場景
場景
具體描述
業務上有某個緩存需要刪除,但是沒有寫刪除 key 的遠程接口
通過arthas執行 service 方法,刪除緩存 key
1.前期準備
1.1下載arthas
官網地址
https://arthas.gitee.io/doc/quick-start.html
下載運行
curl -O https://arthas.aliyun.com/arthas-boot.jar java -jar arthas-boot.jar
1.2 idea安裝arthas插件
1.3 寫一個普通sum方法
啟動這個應用
1.4 啟動arthas并attach上helloworld進程
選擇1
2. 實際操作
例如我們需要遠程調用下這個sum方法,但是controller沒用寫調用sum方法接口,重新發版有風險且太慢了,于是可以利用arthas直接遠程調用sum
上面我們已經啟動了arthas且attach上了helloworld進程
2.1 獲取UserService的classLoaderHash
此時需要先獲取UserService的classLoaderHash 用于后續我們指定訪問這個方法
sc -d com.example.helloworld.service.UserService
classLoaderHash 18b4aac2
2.2 通過idea arthas插件獲取執行方法的命令
復制出來是這個樣子
vmtool -x 3 --action getInstances --className com.example.helloworld.service.UserService --express 'instances[0].sum(new com.example.helloworld.controller.User())' -c 18b4aac2
參數解釋
-x 3返回參數展開形式的,默認1,設置3,方便觀察返回結果
-c xxx指定classLoaderHash
2.3 完善下,加上傳遞參數,例如傳遞年齡為3
vmtool -x 3 --action getInstances --className com.example.helloworld.service.UserService --express 'instances[0].sum(new com.example.helloworld.controller.User("zhangsan",3))' -c 18b4aac2