官网地址:https://arthas.aliyun.com/doc/quick-start.html#_6-
1.安装启动
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
2.选择对应进程编号+回车
3.watch命令
官网命令文档:https://arthas.aliyun.com/doc/arthas-tutorials.html?language=cn&id=command-watch
作用:监控函数的输入、输出、异常等信息。
watch 命令定义了 4 个观察事件点,即 -b 函数调用前,-e 函数异常后,-s 函数返回后,-f 函数结束后(默认开启)
在 watch 命令的结果里,会打印出location信息。location有三种可能值:AtEnter,AtExit,AtExceptionExit。对应函数入口,函数正常 return,函数抛出异常。
-x:指定输出结果的属性遍历深度
-n:只执行多少次数
-e表示抛出异常时才触发
查看调用之前函数的结果以及函数调用之后的结果
watch com.hqhp.jwt.utils.JwtUtil getFieldValue '{params,returnObj,throwExp}' -x 2
只有第一个参数小于0才会触发watch
watch demo.MathGame primeFactors "{params,target,returnObj}" -x 2 -b -s -n 2
查看响应异常信息
watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2
对监控方法耗时进行过滤(200单位ms)
watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2
查看函数运行前后当前对象属性,也就是函数所在class包含属性内容
watch demo.MathGame primeFactors 'target'
触发指定方法后,调用当前对象的属性
watch demo.MathGame primeFactors 'target.illegalArgumentCount'
4.trace命令
作用:能输出方法内部调用路径,并输出方法路径上的每个节点上耗时
场景:当出现耗时方法时,可通过此命令查看调用的其他方法耗时时间
捕捉到函数被调用了一次就退出监控
监控指定函数调用链路及耗时
trace com.hqhp.jwt.filter.JwtFilter isAccessAllowed
捕捉到函数被调用了一次就退出监控
trace com.hqhp.jwt.filter.JwtFilter isAccessAllowed -n 1
输入调用JDK内部函数信息
trace --skipJDKMethod false com.hqhp.jwt.filter.JwtFilter isAccessAllowed -n 1
只会展示耗时大于 10ms 的调用路径,有助于在只关注异常情况
trace com.hqhp.jwt.filter.JwtFilter isAccessAllowed '#cost > 10'
trace 多个类或者多个函数
trace -E com.test.ClassA|org.test.ClassB method1|method2|method3
排除掉指定的类
trace javax.servlet.Filter * --exclude-class-pattern com.demo.TestFilter
5.ognl命令
执行 ognl 表达式
获取静态变量
ognl '@demo.MathGame@random'
从spring容器中获取class中的变量
ognl '@com.zqh.bean.ApplicationUtil@getBean("classBean").str'
动态修改非静态属性值
ognl --classLoaderClass org.apache.catalina.loader.WebappClassLoader
'#instence=@cn.com.maxtech.util.Maxtech@me.getBean("LedgerTimer"),
#fieldObj=@cn.com.maxtech.jswyj.ledger.timer.LedgerTimer@class.getDeclaredField("isOpen"),
#fieldObj.setAccessible(true),
#fieldObj.set(#instence,true)'
1、--classLoaderClass org.apache.catalina.loader.WebappClassLoader 指定classload为tomcat的webappClassLoader(因为我们的springboot项目使用的外置tomcat)
2、#instence=@cn.com.maxtech.util.Maxtech@me.getBean("LedgerTimer") 从spring容器中拿到LedgerTimer类的实例放入一个变量instence中
3、#fieldObj=@cn.com.maxtech.jswyj.ledger.timer.LedgerTimer@class.getDeclaredField("isOpen") 使用反射获取LedgerTimer类的isOpen字段对象放入fieldObj变量中
4、#fieldObj.setAccessible(true) 由于isOpen字段没有设置public访问权限,所以需要执行此段代码放开访问权限
5、#fieldObj.set(#instence,true) 修改字段值