Arthas 排查工具

文章目录

  • 一、Arthas 介绍
  • 二、Arthas 使用
    • 基础使用
    • 常用场景
  • 三、Arthas 利器

一、Arthas 介绍

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

Arthas 官方文档

Arthas 代码仓库

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

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

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

二、Arthas 使用

基础使用

  1. 启动 java 进程
curl -O https://arthas.aliyun.com/math-game.jar
java -jar math-game.jar
  1. 启动 arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
  1. 选择 java 进程:
java -jar arthas-boot.jar
* [1]: 35542
  [2]: 71560 math-game.jar
 
  1. 进入 arthas
[INFO] Try to attach process 71560
[INFO] Attach process 71560 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'


wiki: https://arthas.aliyun.com/doc
version: 3.0.5.20181127201536
pid: 71560
time: 2018-11-28 19:16:24
  1. 使用 jad 编译类
$ jad demo.MathGame

ClassLoader:
+-sun.misc.Launcher$AppClassLoader@3d4eac69
  +-sun.misc.Launcher$ExtClassLoader@66350f69

Location:
/tmp/math-game.jar

/*
 * Decompiled with CFR 0_132.
 */
package demo;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MathGame {
    private static Random random = new Random();
    private int illegalArgumentCount = 0;

    public static void main(String[] args) throws InterruptedException {
        MathGame game = new MathGame();
        do {
            game.run();
            TimeUnit.SECONDS.sleep(1L);
        } while (true);
    }

    public void run() throws InterruptedException {
        try {
            int number = random.nextInt();
            List<Integer> primeFactors = this.primeFactors(number);
            MathGame.print(number, primeFactors);
        }
        catch (Exception e) {
            System.out.println(String.format("illegalArgumentCount:%3d, ", this.illegalArgumentCount) + e.getMessage());
        }
    }

    public static void print(int number, List<Integer> primeFactors) {
        StringBuffer sb = new StringBuffer("" + number + "=");
        Iterator<Integer> iterator = primeFactors.iterator();
        while (iterator.hasNext()) {
            int factor = iterator.next();
            sb.append(factor).append('*');
        }
        if (sb.charAt(sb.length() - 1) == '*') {
            sb.deleteCharAt(sb.length() - 1);
        }
        System.out.println(sb);
    }

    public List<Integer> primeFactors(int number) {
        if (number < 2) {
            ++this.illegalArgumentCount;
            throw new IllegalArgumentException("number is: " + number + ", need >= 2");
        }
        ArrayList<Integer> result = new ArrayList<Integer>();
        int i = 2;
        while (i <= number) {
            if (number % i == 0) {
                result.add(i);
                number /= i;
                i = 2;
                continue;
            }
            ++i;
        }
        return result;
    }
}

Affect(row-cnt:1) cost in 970 ms.

  1. 退出 arthas

如果只是退出当前的连接,可以用quit或者exit命令。Attach 到目标进程上的 arthas 还会继续运行,端口会保持开放,下次连接时可以直接连接上。

如果想完全退出 arthas,可以执行stop命令

基础入门
命令介绍

常用场景

  1. watch、trace、stack
# 默认参数: "{params, target, returnObj}" params:参数、target: this对象、returnObj: 返回值、throwExp: 异常
watch demo.MathGame primeFactors "{params, target, returnObj}" 
# ognl表达式 花费时间大于200毫秒 -x 调用参数深度【1-4】、-n 调用次数
watch demo.MathGame primeFactors "#cost>200" -x 2 -n 1
# ognl表达式 参数小于0
watch demo.MathGame primeFactors "{params[0]}" "params[0]<0"
# 查看第一个参数:
watch com.taobao.container.Test test "params[0]"
# 查看第一个参数的size:
watch com.taobao.container.Test test "params[0].size()"
# 将结果按name属性投影:
watch com.taobao.container.Test test "params[0].{ #this.name }"
# 按条件过滤:
watch com.taobao.container.Test test "params[0].{? #this.name == null }" -x 2
# 过滤后统计:
watch com.taobao.container.Test test "params[0].{? #this.age > 10 }.size()" -x 2
# 判断字符串相等比如第一个参数是String类型:
watch com.demo.Test test 'params[0]=="xyz"'
# 判断long型
watch com.demo.Test test 'params[0]==123456789L'
  1. ognl
# 调用静态方法
ognl '@[email protected]("hello")'

# 获取静态对象
ognl '@demo.MathGame@random'

# 设置参数调用
ognl '#dateStr=new java.lang.String("2023-04-01"), @com.sensorsdata.platform.web.utils.DateUtil@parseDate(#dateStr)'

# 获取Spring bean对象,提供设置参数,进行对象调用
ognl '#[email protected]@class,#[email protected]@getBean(#clz),#bean.getAdhocGroupsAndSpsUserGroups()'

# 设置前置上下文参数信息,获取 Spring bean对象,提供设置参数,进行对象调用
ognl '@com.xxx.CurrentProjectUtil@setCurrentProjectIdAndName(1,"default"), @com.xxx.CurrentProjectUtil@setCurrentTestAccountId(1),#[email protected]@class,#[email protected]@getBean(#clz),#bean.getAdhocGroupsAndSpsUserGroups()'

三、Arthas 利器

你可能感兴趣的:(java)