Arthas 是一款线上监控诊断产品,通过全局视角实时查看应用 load、内存、gc、线程的状态信息,并能在不修改应用代码的情况下,对业务问题进行诊断,包括查看方法调用的出入参、异常,监测方法执行耗时,类加载信息等,大大提升线上问题排查效率。
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
Arthas 支持 JDK 6+,支持 Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。
curl -O https://arthas.aliyun.com/math-game.jar
java -jar math-game.jar
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
java -jar arthas-boot.jar
* [1]: 35542
[2]: 71560 math-game.jar
[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
$ 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.
如果只是退出当前的连接,可以用quit或者exit命令。Attach 到目标进程上的 arthas 还会继续运行,端口会保持开放,下次连接时可以直接连接上。
如果想完全退出 arthas,可以执行stop命令
# 默认参数: "{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'
# 调用静态方法
ognl '@java.lang.System@out.println("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 '#clz=@com.xxx.AdhocGroupService@class,#bean=@com.xxx.AppContext@getBean(#clz),#bean.getAdhocGroupsAndSpsUserGroups()'
# 设置前置上下文参数信息,获取 Spring bean对象,提供设置参数,进行对象调用
ognl '@com.xxx.CurrentProjectUtil@setCurrentProjectIdAndName(1,"default"), @com.xxx.CurrentProjectUtil@setCurrentTestAccountId(1),#clz=@com.xxx.AdhocGroupService@class,#bean=@com.xxx.AppContext@getBean(#clz),#bean.getAdhocGroupsAndSpsUserGroups()'