Arthas场景实战*

jasmine 于 2022-01-12 发布

thread 命令

1)查看线程池里面线程状态

thread | grep pool

2)查看线程是否有死锁

thread -b

3)查看指定状态的线程

thread –state BLOCKED

4)统计最近1000ms内的线程CPU时间

thread -i 1000

5)列出1000ms内最忙的5个线程栈

thread -n 5 -i 1000

6)排查线上CPU标高

thread 查看当前服务的线程情况 thread 22 查看线程22号的详细信息,对应到代码位置行数

watch 命令

1)线上环境返回接口不是预期结果,用watch结合ognl表达式:

watch -x 3 -n 1 org.springframework.web.servlet.DispatcherServlet doDispatch ‘@org.springframework.web.context.request.RequestContextHolder@currentRequestAttributes().getRequest().xxx()’

2)监控指定方法,打印出异常的堆栈信息

watch com.github.dreamroute.excel.helper.ExcelHelper importFromFile ‘{params,throwExp}’ -e -x 3

3)监控指定方法,打印出入参和返回值信息

watch com.github.dreamroute.excel.helper.ExcelHelper importFromFile ‘{params,returnObj}’ -x 3

ognl 命令

1)确认一下HEADER_INFO中保存的ExcelDTO.class相关的HeaderInfo

ognl ‘#value=new com.tom.dto.ExcelDTO(),#valueMap=@com.github.dreamroute.excel.helper.cache.CacheFactory@HEADER_INFO,#valueMap.get(#value.getClass()).entrySet().iterator.{#this.value.name}’

heapdump 命令

1)排查线上频繁FullGC问题

1)执行heapdump –live /root/jvm.hprof命令,将dump文件,生成到日志服务器 2)用MAT工具进行dump文件分析

monitor 命令

注意:monitor命令会导致方法变慢,使用后关闭Arthas

trace 命令

trace 类全称 + 空格 + 方法名

tt 命令

1)监听某个接口,并进行操作

①执行监听

tt -t -n 3 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod

②获取spring容器

tt -i 1000 -w ‘target.getApplicationContext()”

③获取bean

tt -i 1000 -w ‘target.getApplicationContext().getBean(“redisClient”).variableName

jad热修复

注意!!!

1)jad 命令反编译出内存中的字节码,生成 java 文件

jad –source-only moe.cnkirito.arthas.demo.HelloService > /tmp/HelloService.java

2)修改代码

3)sc 查找类加载器

sc -d moe.cnkirito.arthas.demo.HelloService

4)使用 mc 命令内存编译新的 class 文件(有可能失败,可以本地编译然后上传)

mc -c 18b4aac2 /tmp/HelloService.java -d /tmp

5)redefine 热更新代码

redefine /tmp/moe/cnkirito/arthas/demo/HelloService.class

profiler 火焰图

场景:当发现CPU的占用率与实际业务应该出现的占用率不相符,或者对Nginx worker的资源使用率(CPU、内存、磁盘IO)出现怀疑情况下, 使用火焰图进行抓取,对 CPU 占用率低、吐吞量低的情况也可以使用火焰图的方式排查程序中是否有阻塞调用导致整个架构的吞吐量低下