# Arthas使用方式详解
Arthas是阿里巴巴开源的一款Java诊断工具,它可以帮助开发者在不重启应用的情况下实时监控线上系统的运行状态,包括查看和修改类的信息、方法的调用情况、线程的状态等,本文将详细介绍Arthas的使用方式。
## 1. 安装与启动
我们需要下载并安装Arthas,可以通过以下命令从GitHub上下载最新版本的Arthas:
mvn clean install -DskipTests -Prelease
下载完成后,进入`arthas-boot`目录,使用`as.sh`脚本启动Arthas:
cd arthas-boot/bin ./as.sh
启动成功后,你将看到类似如下的输出:
$ # 请按回车键开始远程连接...
接下来,你需要连接到目标服务器,可以使用`telnet`或`nc`等工具进行连接。
$ telnet 127.0.0.1 8563
## 2. 基本操作
### 2.1 查看类信息
Arthas提供了`dashboard`命令来查看当前运行的类信息,包括类名、加载时间、创建时间、占用内存等。
$ dashboard
### 2.2 查看方法调用情况
`watch`命令可以用于查看方法的调用情况,我们可以查看`java.lang.String`类的`length()`方法被哪些类和方法调用:
$ watch com.alibaba.arthas.dsl.commands.monitor.MethodMonitor grep -c length([)I
### 2.3 查看线程状态
`thread`命令可以查看线程的详细信息,包括线程ID、线程状态、堆栈信息等。
$ thread -n 10 -bt | grep java.lang.ThreadLocalRandom
## 3. Java API操作
除了上述基本操作外,Arthas还支持对Java API的直接操作,我们可以使用`ognl`命令执行OGNL表达式:
$ ognl '@com.example.service.UserService@getUser(name)' --target-class com.example.service.UserService --target-method getUser --args name=Tommy
还可以使用`sc`命令进行动态代码生成和修改:
```bash
$ sc -d com.example.service.UserService writeGetUser("return userRepository.findByName($1);") --value "Tommy" --target-class com.example.service.UserService --target-method getUser --methodArgs name=Tommy --exposeProxy --proxyTargetClass Object --proxyFactory newInstanceCreatorWithArgs --proxyInterceptor MethodInterceptor --interceptors InterceptorList[0] --invocationHandler InterceptorInvocationHandlerList[0] --targetConstructorArgs name=null --transformerTransformerManager List[1] --transformerAddAfter ReturnVoidTransformer --transformerAddBefore SingleValueTransformer --transformerAddFirst BeforeTransformer --transformerRemoveAfter ReturnVoidTransformer --transformerRemoveBefore SingleValueTransformer --transformerRemoveFirst BeforeTransformer --transformerAddLast LastValueTransformer --transformerAddAtEnd EndTransformer --transformerReplace AfterReturningTransformer --transformerReplaceBeforeThrowingTransformer --transformerReplaceFirst BeforeThrowingTransformer --transformerReplaceLast LastThrowingTransformer --transformerReplaceAtEnd EndThrowingTransformer --transformerAddFirst BeforeThrowingTransformer --transformerAddLast LastThrowingTransformer:org.apache.commons.collections4.CollectionUtils::newArrayList($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, ...)--proxySetStaticMethodInterceptor SetStaticMethodInterceptor:org.springframework.aop.support.DelegatingIntroductionInterceptor::setStaticMethodInterceptionCallback org.springframework.aop.support.DelegatingIntroductionInterceptor::setIntroductionCallback org.springframework.aop.support.DelegatingIntroductionInterceptor::setIntroductionCallbackWithExplicitArguments(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)--proxyTargetClass Object --proxyFactory newInstanceCreatorWithArgs --proxyInterceptor MethodInterceptor --interceptors InterceptorList[0] --invocationHandler InterceptorInvocationHandlerList[0] --targetConstructorArgs name=null --transformerTransformerManager List[1] --transformerAddAfter ReturnVoidTransformer --transformerAddBefore SingleValueTransformer --transformerAddFirst BeforeTransformer --transformerRemoveAfter ReturnVoidTransformer --transformerRemoveBefore SingleValueTransformer --transformerRemoveFirst BeforeTransformer --transformerAddLast LastValueTransformer --transformerAddAtEnd EndTransformer --transformerReplace AfterReturningTransformer --transformerReplaceBeforeThrowingTransformer --transformerReplaceFirst BeforeThrowingTransformer --transformerReplaceLast LastThrowingTransformer --transformerReplaceAtEnd EndThrowingTransformer:org.apache.commons.collections4.CollectionUtils::newArrayList($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, ...)--proxySetStaticMethodInterceptor SetStaticMethodInterceptor:org.springframework.aop.support.DelegatingIntroductionInterceptor::setStaticMethodInterceptionCallback org.springframework.aop.support.DelegatingIntroductionInterceptor::setIntroductionCallback org.springframework.aop.support.DelegatingIntroductionInterceptor::setIntroductionCallbackWithExplicitArguments(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)--proxyTargetClass Object --proxyFactory newInstanceCreatorWithArgs --proxyInterceptor MethodInterceptor --interceptors InterceptorList[0]--targetConstructorArgs name=null--transformerTransformerManager List[1]--transformerAddAfter ReturnVoidTransformer--transformerAddBefore SingleValueTransformer--transformerAddFirst BeforeTransformer--transformerRemoveAfter ReturnVoidTransformer--transformerRemoveBefore SingleValueTransformer--transformerRemoveFirst BeforeTransformer--transformerAddLast LastValueTransformer--transformerAddAtEnd EndTransformer--transformerReplace AfterReturningTransformer--transformerReplaceBeforeThrowingTransformer--transformerReplaceFirst BeforeThrowingTransformer--transformerReplaceLast LastThrowingTransformer:org.apache
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/24575.html