arthas学习笔记

287 阅读1分钟

官方文档

arthas.aliyun.com/doc/

安装

入门

运行

java -jar arthas-boot.jar

选择java进程序号,回车

以下是基础命令

  • 查看当前进程信息: dashboard
  • 打印线程的栈:thread 线程id
  • 查找main.class:thread 线程id | grep 'main('
  • 查找jvm加载的类: sc -d *类名
  • 反编译类:jad 类(sc -d *类名命令展示的class-info)
  • 查看函数的参数/返回值/异常信息:watch 类 primeFactors returnObj
  • 退出: quit 或者exit,完全退出用stop

进阶

  • 查看函数返回值:watch 类 方法名 returnObj -x 2 ,-x 后面带的数字表示返回值的深度,自己执行体验下就知道了
  • 查看函数传参:watch 类 方法名 params[0] param[0] 中括号里0,1表示第几个参数
  • 方法内部调用路径,并输出方法路径上的每个节点上耗时:trace 类 方法

实践

@RestController
public class DemoController {
    @Autowired
    DemoService demoService;

    @GetMapping("testArthas")
    public RestResponse test(Integer param1, String param2, Long param3) {
        return RestResponse.success(demoService.testArthas(param1, param2, param3));
    }

}

@Service
public class DemoServiceImpl implements DemoService {
    @Override
    public ArthasVO testArthas(Integer param1, String param2, Long param3) {
        ArthasVO arthasVO = new ArthasVO();
        arthasVO.setAge(param1 + 20);
        arthasVO.setId(param3 + 1000);
        arthasVO.setName(param2);
        return arthasVO;
    }
}

栗子1:发现线上有bug,怀疑是代码没更新,或者传参有问题。

操作:

  1. 反编译代码,看看代码是不是最新的
    jad com.study.demo.service.impl.DemoServiceImpl
  2. 打印传参
    watch com.study.demo.service.impl.DemoServiceImpl testArthas params
  3. 查看返回值
    watch com.study.demo.service.impl.DemoServiceImpl testArthas returnObj -x 1

栗子2:发现接口响应过慢
trace com.study.demo.controller.DemoController test

栗子3:发现调用外部接口这部分处理逻辑影响导致线上问题,想打印传参以及返回值 watch com.study.demo.service.impl.DemoServiceImpl testArthas "{params,returnObj}" "#cost>200" -x 2 解释:查看传参跟返回值,以及方法耗时大于200ms的