Arthas快速使用—生产问题排查利器

简介

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

官方文档:https://arthas.aliyun.com/doc/

Arthas能帮你做什么?

Arthas 是 Alibaba 开源的 Java 诊断工具,深受开发者喜爱。

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

  • 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
  • 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
  • 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
  • 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
  • 是否有一个全局视角来查看系统的运行状况?
  • 有什么办法可以监控到 JVM 的实时运行状态?
  • 怎么快速定位应用的热点,生成火焰图?
  • 怎样直接从 JVM 内查找某个类的实例?

Arthas 支持 JDK 6+,支持 Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。

如何使用

安装步骤

可以通过以下方式进行安装:

github下载

wget https://alibaba.github.io/arthas/arthas-boot.jar

gitee 下载

wget https://arthas.gitee.io/arthas-boot.jar

直接curl下载

curl -O https://arthas.gitee.io/arthas-boot.jar

打印帮助信息

java -jar arthas-boot.jar -h

启动运行

下载完成如下所示:
image

使用java -jar arthas-boot.jar -h查看帮助信息:

[root@xxkfz arthas]# java -jar arthas-boot.jar -h
[INFO] JAVA_HOME: /root/xxkfz/soft/jdk-17
[INFO] arthas-boot version: 3.7.2
Usage: arthas-boot [-h] [--target-ip <value>] [--telnet-port <value>]
       [--http-port <value>] [--session-timeout <value>] [--arthas-home <value>]
       [--use-version <value>] [--repo-mirror <value>] [--versions] [--use-http]
       [--attach-only] [-c <value>] [-f <value>] [--height <value>] [--width
       <value>] [-v] [--tunnel-server <value>] [--agent-id <value>] [--app-name
       <value>] [--username <value>] [--password <value>] [--stat-url <value>]
       [--select <value>] [--disabled-commands <value>] [pid]

Bootstrap Arthas

EXAMPLES:
  java -jar arthas-boot.jar <pid>
  java -jar arthas-boot.jar --telnet-port 9999 --http-port -1
  java -jar arthas-boot.jar --username admin --password <password>
  java -jar arthas-boot.jar --tunnel-server 'ws://192.168.10.11:7777/ws'
--app-name demoapp
  java -jar arthas-boot.jar --tunnel-server 'ws://192.168.10.11:7777/ws'
--agent-id bvDOe8XbTM2pQWjF4cfw
  java -jar arthas-boot.jar --stat-url 'http://192.168.10.11:8080/api/stat'
  java -jar arthas-boot.jar -c 'sysprop; thread' <pid>
  java -jar arthas-boot.jar -f batch.as <pid>
  java -jar arthas-boot.jar --use-version 3.7.2
  java -jar arthas-boot.jar --versions
  java -jar arthas-boot.jar --select math-game
  java -jar arthas-boot.jar --session-timeout 3600
  java -jar arthas-boot.jar --attach-only
  java -jar arthas-boot.jar --disabled-commands stop,dump
  java -jar arthas-boot.jar --repo-mirror aliyun --use-http
WIKI:
  https://arthas.aliyun.com/doc

Options and Arguments:
 -h,--help                        Print usage
    --target-ip <value>           The target jvm listen ip, default 127.0.0.1
    --telnet-port <value>         The target jvm listen telnet port, default
                                  3658
    --http-port <value>           The target jvm listen http port, default 8563
    --session-timeout <value>     The session timeout seconds, default 1800
                                  (30min)
    --arthas-home <value>         The arthas home
    --use-version <value>         Use special version arthas
    --repo-mirror <value>         Use special remote repository mirror, value is
                                  center/aliyun or http repo url.
    --versions                    List local and remote arthas versions
    --use-http                    Enforce use http to download, default use
                                  https
    --attach-only                 Attach target process only, do not connect
 -c,--command <value>             Command to execute, multiple commands
                                  separated by ;
 -f,--batch-file <value>          The batch file to execute
    --height <value>              arthas-client terminal height
    --width <value>               arthas-client terminal width
 -v,--verbose                     Verbose, print debug info.
    --tunnel-server <value>       The tunnel server url
    --agent-id <value>            The agent id register to tunnel server
    --app-name <value>            The app name
    --username <value>            The username
    --password <value>            The password
    --stat-url <value>            The report stat url
    --select <value>              select target process by classname or
                                  JARfilename
    --disabled-commands <value>   disable some commands
 <pid>                            Target pid

[root@xxkfz arthas]# 

下载完成后,执行命令启动:

java -jar arthas-boot.jar

启动

然后、选择应用 java 进程:(输入进程前面的序号回车):

注:小编这里由于只有1个进程,输入1即可。

启动#选择进程

注:输入q键即可退出arthas!

退出arthas

常用命令使用

dashboard—系统数据面板

输入dashboard命令,按回车/enter,展示当前系统的实时数据面板,可以概览程序的 线程、内存、GC、运行环境信息。 按ctrl+c可以中断执行。

dashboard命令

查看JVM状态

通过以下命令查看当前JVM的状态:

[arthas@17022]$ jvm

查看JVM状态

方法调用链路追踪

有时我们需要查看某个方法被调用了多少次、被哪些方法调用了,甚至是每一次调用的参数和返回值。这时可以使用Arthas的trace命令。

追踪某个方法的所有调用
[arthas@17022]$ trace org.dromara.system.service.impl.SysPostServiceImpl selectPagePostList

该命令将会追踪org.dromara.system.service.impl.SysPostServiceImpl类中的selectPagePostList方法的所有调用。

追踪某个方法的所有调用

追踪某个类中所有方法的调用
trace org.dromara.system.service.impl.SysPostServiceImpl *

追踪某个类中所有方法的调用

追踪某个方法的详细信息
trace org.dromara.system.service.impl.SysPostServiceImpl selectPagePostList -n 3

该命令将会追踪org.dromara.system.service.impl.SysPostServiceImpl类中的selectPagePostList方法的前3次调用,并打印出每次调用的参数和返回值。

追踪某个方法的详细信息

监控线程

[arthas@29672]$ thread 

执行以上命令后,会输出当前JVM中所有线程的信息。

监控线程

反编译指定已加载类的源码

反编译整个类
[arthas@29672]$ jad org.dromara.system.service.impl.SysPostServiceImpl
[arthas@29672]$ jad --source-only org.dromara.system.service.impl.SysPostServiceImpl

上述命令加上参数:jad --source-only 为反编译时只显示源代码!

反编译整个类

反编译指定的函数
[arthas@29672]$ jad org.dromara.system.service.impl.SysPostServiceImpl selectPagePostList
[arthas@29672]$ jad -source-only   org.dromara.system.service.impl.SysPostServiceImpl selectPagePostList

反编译指定的函数

生成系统火焰图

用于统计系统指标,分析系统新能。可统计指标还是挺多的。例如:-e cpu, 可以看出占用cpu比较大的资源。

开始启动统计
profiler -e cpu start
查看统计状态
profiler status
停止统计并输出问题,默认是svg
profiler stop

操作演示

生成的火焰图

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值