在线分析诊断工具Arthas

2019年4月22日 作者 kee

在线分析诊断工具Arthas

参考:https://alibaba.github.io/arthas/commands.html

一、观察方法

watch:观察方法参数、返回值、异常等
  • params:参数
  • returnObj:返回值
  • target:当前对象
  • throwExp:异常
  1. 观察方法参数、返回
watch com.yangshan.eship.order.service.exlabel.TrackingInformationService track "{params,returnObj}" -x 5
  1. 观察方法参数、异常
watch com.yangshan.eship.order.service.exlabel.TrackingInformationService track "{params,throwExp}" -e -x 5
  1. 观察当前对象中的属性(target代表当前对象TrackingInformationService)
watch com.yangshan.eship.order.service.exlabel.TrackingInformationService track "{params,target}" -e -x 5
trace:跟踪方法耗时
  • cost:耗时,单位毫秒
  1. 观察方法的执行耗时时间
trace com.yangshan.eship.order.repository.orde.OrderDaoImpl findAllOrder '#cost > 5000'

二、动态修改已加载的类,不需要重启服务器【注意不能添加字段和方法,只能修改方法里面内容】

jad:反编译
mc:编译
redefine:重新加载class到jvm
  1. 官网使用方法
1. 反编译类,用vim修改
jad --source-only com.xxxx.xxxx.xxxx.xxxx.UserController > /tmp/UserController.java

2. 重新编译
mc /tmp/UserController.java -d /tmp

3. 重新加载class到JVM中
redefine /tmp/com/xxxx/xxxx/xxxx/xxxx/UserController.class
  1. 实际建议使用
1. 本地编译新的class,上传到服务器【使用arthas的mc编译容易出错】
2. 使用redfine加载class到JVM中