Arthas(阿尔萨斯)

Arthas(阿尔萨斯)

Java应用诊断利器

Arthas 是一款线上监控诊断产品,通过全局视角实时查看应用 load、内存、gc、线程的状态信息,并能在不修改应用代码的情况下,对业务问题进行诊断,包括查看方法调用的出入参、异常,监测方法执行耗时,类加载信息等,大大提升线上问题排查效率。

官网地址

Arthas官网:https://arthas.aliyun.com/

GitHub:https://github.com/alibaba/arthas

Gitee:https://gitee.com/arthas/arthas

下载全量包:https://arthas.aliyun.com/download/latest_version?mirror=aliyun

启动Arthas

java -jar arthas-boot.jar
java -jar -Dfile.encoding=UTF-8 arthas-boot.jar
java -jar arthas-boot.jar --repo-mirror aliyun --use-http

# 输入Java进程序号,按回车

help 列出所有命令
sc -h 查看sc命令的参数
pwd 查看Arthas当前目录

Web Console地址:http://127.0.0.1:8563/

# 命令列表:https://arthas.aliyun.com/doc/commands.html

退出Arthas:quit与stop

quit
退出当前 Arthas 客户端,其他 Arthas 客户端不受影响。等同于exit、logout、q三个指令。
提示:只是退出当前 Arthas 客户端,Arthas 的服务器端并没有关闭,所做的修改也不会被重置。

stop
关闭 Arthas 服务端,所有 Arthas 客户端全部退出。
提示:关闭 Arthas 服务器之前,会重置掉所有做过的增强类。但是用 redefine 重加载的类内容不会被重置。

sc、sm 查看JVM已加载的类和方法

# https://arthas.aliyun.com/doc/sc.html
# Search Classes(简称sc)(同时也会打印出这个类的子类)
sc *.JSONObject
sc com.alibaba.fastjson.*
sc *JSON*

# 查看某个类来自哪个jar包(code-source)
sc -d com.alibaba.fastjson.JSONObject | grep -E 'class-info|code-source'

-d, --details 打印详情
-f, --field 打印字段

# Search Method(简称sm)
sm com.alibaba.fastjson.JSONObject
sm -d com.alibaba.fastjson.JSONObject containsKey

watch 方法执行数据观测

# https://arthas.aliyun.com/doc/watch.html
# https://arthas.aliyun.com/doc/arthas-tutorials.html?language=cn&id=command-watch
watch -h

watch '全类名' '方法名' '{params, target, returnObj, throwExp}' '过滤条件' -n 1 -x 3

# 输出次数为1
-n 1
# 对象打印层数,为2是可以看到入参的类型和数量
-x 3

# 常用参数列表(可使用watch -h查看)
'{params, target, returnObj, throwExp}'
'{params, returnObj.toString()}'
'{params[0], returnObj.toString()}'

# 示例
watch com.alibaba.fastjson.JSONObject getInteger -n 1
watch com.alibaba.fastjson.JSONObject getInteger '{params[0], returnObj}' 'params[0].equals("xxx")' -n 3
watch 'com.alibaba.fastjson.JSONObject' 'getInteger' '{params[0], returnObj}' 'params[0].equals("xxx")' -n 3 -x 3


jad 反编译

# https://arthas.aliyun.com/doc/jad.html

jad com.alibaba.fastjson.JSONObject
jad com.alibaba.fastjson.JSONObject containsKey
jad --source-only com.alibaba.fastjson.JSONObject containsKey
jad --source-only com.alibaba.fastjson.JSONObject containsKey --lineNumber false

jad --source-only com.alibaba.fastjson.JSONObject --lineNumber false > 'xxx.java'

tt 方法执行数据的时空隧道

# https://arthas.aliyun.com/doc/tt.html
# https://arthas.aliyun.com/doc/arthas-tutorials.html?language=cn&id=command-tt

# TimeTunnel
# 方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测

# -t 记录(可以通过过滤条件过滤重载的情况)(指定-n数量,防止内存撑满)
tt -t com.alibaba.fastjson.JSONObject containsKey -n 3
tt -t com.alibaba.fastjson.JSONObject containsKey 'params.length==1' -n 3
tt -t com.alibaba.fastjson.JSONObject containsKey 'params[0] instanceof Integer' -n 3

# 查看记录的片段
tt -l
tt -s 'method.name=="containsKey"'

# 查看某个(使用ognl表达式)
tt -i 1003
tt -i 1003 -w '{params[0], returnObj.toString()}' -x 3

JVM的 环境变量sysenv 与 系统属性sysprop

# https://arthas.aliyun.com/doc/sysenv.html
# 查看当前 JVM 的环境属性(System Environment Variables)
sysenv
# 查看单个环境变量:System.getenv("xxx")
sysenv JAVA_HOME

# https://arthas.aliyun.com/doc/sysprop.html
# 查看当前 JVM 的系统属性(System Property)
sysprop
# 查看单个属性:System.getProperty("xxx")
sysprop user.dir
# 修改单个属性(会提示:Successfully changed the system property.)
sysprop user.country CN

表达式核心变量

无论是匹配表达式也好、观察表达式也罢,他们核心判断变量都是围绕着一个 Arthas 中的通用通知对象 Advice 进行。

// https://arthas.aliyun.com/doc/advice-class.html
// Advice的简略代码结构如下:
public class Advice {
    private final ClassLoader loader;
    private final Class<?> clazz;
    private final ArthasMethod method;
    private final Object target;
    private final Object[] params;
    private final Object returnObj;
    private final Throwable throwExp;
    private final boolean isBefore;
    private final boolean isThrow;
    private final boolean isReturn;
    // getter/setter
}

作者博客首页:https://blog.csdn.net/qq_43111676?type=blog

欢迎关注,持续更新中…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值