概述
本文主要是个人学习记录,看情况更新,详细教程请前往官网查看
更多案例请前往github官网学习->https://github.com/alibaba/arthas/issues?q=label%3Auser-case
测试准备
下载demo应用并启动
# 下载
wget https://arthas.aliyun.com/math-game.jar
# 启动
java -jar math-game.jar
下载Arthas并启动
# 下载Arthas
wget https://arthas.aliyun.com/arthas-boot.jar
# 启动
java -jar arthas-boot.jar
一. 案例一: 动态更新应用Logger Level
1.1 更新UserController的日志Level
- 查询UserController的ClassLoader
[arthas@92276]$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash
classLoaderHash 5b2133b1
- 用ognl获取logger
ognl -c 5b2133b1 '@com.example.demo.arthas.user.UserController@logger'
[arthas@92276]$ ognl -c 5b2133b1 '@com.example.demo.arthas.user.UserController@logger'
@Logger[
serialVersionUID=@Long[5454405123156820674],
FQCN=@String[ch.qos.logback.classic.Logger],
name=@String[com.example.demo.arthas.user.UserController],
level=null,
effectiveLevelInt=@Integer[20000],
parent=@Logger[Logger[com.example.demo.arthas.user]],
childrenList=null,
aai=null,
additive=@Boolean[true],
loggerContext=@LoggerContext[ch.qos.logback.classic.LoggerContext[default]],
]
可以知道UserController@logger实际使用的是logback。可以看到level=null,则说明实际最终的level是从root logger里来的。
单独设置UserController的logger level
ognl -c 5b2133b1 '@[email protected](@ch.qos.logback.classic.Level@DEBUG)'
检查修改结果
[arthas@92276]$ ognl -c 5b2133b1 '@com.example.demo.arthas.user.UserController@logger'
@Logger[
serialVersionUID=@Long[5454405123156820674],
FQCN=@String[ch.qos.logback.classic.Logger],
name=@String[com.example.demo.arthas.user.UserController],
level=@Level[DEBUG],
effectiveLevelInt=@Integer[10000],
parent=@Logger[Logger[com.example.demo.arthas.user]],
childrenList=null,
aai=null,
additive=@Boolean[true],
loggerContext=@LoggerContext[ch.qos.logback.classic.LoggerContext[default]],
]
1.2 修改logback的全局logger level
ognl -c 5b2133b1 '@org.slf4j.LoggerFactory@getLogger("root").setLevel(@ch.qos.logback.classic.Level@DEBUG)'
二. 案例二: 从Spring Context获取bean并调用函数
2.1 使用tt命令获取到spring context
运行命令tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod
, 然后随便发送一个请求
[arthas@92276]$ tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 42 ms, listenerId: 17
INDEX TIMESTAMP COST(ms) IS-RET IS-EXP OBJECT CLASS METHOD
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
1018 2022-02-07 16:57:49 0.497819 false true 0x29be953b RequestMappingHandlerAdapter invokeHandlerMethod
1019 2022-02-07 16:57:49 2.333579 true false 0x29be953b RequestMappingHandlerAdapter invokeHandlerMethod
输入 Q 或者 Ctrl + C 退出上面的 tt -t命令。然后接着进行下一步
2.2 获取spring bean,并调用函数
[arthas@92276]$ tt -i 1000 -w 'target.getApplicationContext().getBean("userController").findUserById(10)'
@User[
id=@Integer[10],
name=@String[name10],
]