【开发工具】Arthas使用笔记:1、下载和简单使用

本文参考官方文档:https://alibaba.github.io/arthas/install-detail.html

如有问题可加入上述文档中的官方QQ/钉钉群

一、安装(windows)

1、开发命令框,切换到准备安装的目录,D:/cloudcore/open_tools/arthas

2、复制下面命令,完成一键安装,并且在该目录下会生成一个as.sh文件

curl -L https://alibaba.github.io/arthas/install.sh | sh

3、执行as.sh,./as.sh,Found existing java process, please choose one and hit RETURN.这里选择Java进程,输入2,回车,执行后出现下面的错误,这里有个错误,直接输入2即可,被迷惑性的输入了[2]会出错。

liuguangyu@LAPTOP-4R84C55I MINGW64 /d/cloudcore/open_tools/arthas
$ ./as.sh
Arthas script version: 3.0.5.2
[INFO] JAVA_HOME: /c/Java/jdk1.8.0_161
Found existing java process, please choose one and hit RETURN.
* [1]: 21924 org.apache.zookeeper.server.quorum.QuorumPeerMain
  [2]: 25752
[2]
./as.sh: line 626: [2]-1: syntax error: operand expected (error token is "[2]-1")
2
updating version 3.0.5 ...
Download arthas from: https://repo1.maven.org/maven2/com/taobao/arthas/arthas-packaging/3.0.5/arthas-packaging-3.0.5-bin.zip
######################################################################## 100.0%
Archive:  /tmp/temp_3.0.5_22664/arthas-3.0.5-bin.zip
  inflating: /tmp/temp_3.0.5_22664/arthas-spy.jar
  inflating: /tmp/temp_3.0.5_22664/arthas-boot.jar
  inflating: /tmp/temp_3.0.5_22664/arthas-core.jar
  inflating: /tmp/temp_3.0.5_22664/arthas-agent.jar
  inflating: /tmp/temp_3.0.5_22664/arthas-demo.jar
  inflating: /tmp/temp_3.0.5_22664/install-local.sh
  inflating: /tmp/temp_3.0.5_22664/as.sh
  inflating: /tmp/temp_3.0.5_22664/as.bat
  inflating: /tmp/temp_3.0.5_22664/arthas-client.jar
update completed.
Arthas home: /c/Users/liuguangyu/.arthas/lib/3.0.5/arthas
Calculating attach execution time...
Attaching to  using version /c/Users/liuguangyu/.arthas/lib/3.0.5/arthas...
[ERROR] Start arthas failed, exception stack trace:
com.taobao.middleware.cli.InvalidValueException: The value '-target-ip' is not accepted by 'pid'
        at com.taobao.middleware.cli.impl.DefaultCommandLine.create(DefaultCommandLine.java:342)
        at com.taobao.middleware.cli.impl.DefaultCommandLine.getValue(DefaultCommandLine.java:267)
        at com.taobao.middleware.cli.impl.DefaultCommandLine.getOptionValue(DefaultCommandLine.java:91)
        at com.taobao.arthas.core.Arthas.parse(Arthas.java:44)
        at com.taobao.arthas.core.Arthas.<init>(Arthas.java:25)
        at com.taobao.arthas.core.Arthas.main(Arthas.java:99)
Caused by: java.lang.IllegalArgumentException: java.lang.NumberFormatException: For input string: "-target-ip"
        at com.taobao.middleware.cli.converters.ConstructorBasedConverter.fromString(ConstructorBasedConverter.java:70)
        at com.taobao.middleware.cli.converters.Converters.create(Converters.java:52)
        at com.taobao.middleware.cli.impl.DefaultCommandLine.create(DefaultCommandLine.java:339)
        ... 5 more
Caused by: java.lang.NumberFormatException: For input string: "-target-ip"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.<init>(Integer.java:867)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.taobao.middleware.cli.converters.ConstructorBasedConverter.fromString(ConstructorBasedConverter.java:67)
        ... 7 more

real    0m1.425s
user    0m0.015s
sys     0m0.015s
attach to target jvm () failed, check /c/Users/liuguangyu/logs/arthas/arthas.log or stderr of target jvm for any exceptions.

由于上面输入的【2】是由问题的,直接输入阿拉伯数字即可,不用加[],再次执行./as.sh,执行成功。

liuguangyu@LAPTOP-4R84C55I MINGW64 /d/cloudcore/open_tools/arthas
$ ./as.sh
Arthas script version: 3.0.5.2
[INFO] JAVA_HOME: /c/Java/jdk1.8.0_161
Found existing java process, please choose one and hit RETURN.
* [1]: 25752

Arthas home: /c/Users/liuguangyu/.arthas/lib/3.0.5/arthas
Calculating attach execution time...
Attaching to 25752 using version /c/Users/liuguangyu/.arthas/lib/3.0.5/arthas...
1
real    0m13.995s
user    0m0.000s
sys     0m0.061s
Attach success.
It seems that current bash is under Windows. /c/WINDOWS/system32/telnet can not run under bash.
Please start cmd.exe from Windows start menu, and then run telnet 127.0.0.1 3658 to connect to target process.
Or visit http://127.0.0.1:8563 to connect to target process.

4、在浏览器中输入http://localhost:8563/  测试一下

点击Connect,出现一下页面

或者使用命令行进入:win+R—>cmd—>telnet 127.0.0.1 3658,同样可以出现上面的arthas语境

5、在本机上准备一个测试类,随便找了一个,并执行

public class TestArthas {
	public static void main(String[] args) throws Exception {
            int i = 0;
            while(true) {
		i += 1;
		System.err.println(i);
		Thread.sleep(2000);
	    }		
	}
}

然后通过命令行打开arthas

PS D:\cloudcore\open_tools\arthas> java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.0.5.2
[INFO] Process 25752 already using port 3658
[INFO] Process 25752 already using port 8563
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 17296
  [2]: 25752
  [3]: 10188 com.cloudcore.pms.transport.test.TestArthas
3
[ERROR] Target process 10188 is not the process using port 3658, you will connect to an unexpected process.
[ERROR] If you still want to attach target process 10188, Try to set a different telnet port by using --telnet-port argument.
[ERROR] Or try to shutdown the process 25752 using the telnet port first.
PS D:\cloudcore\open_tools\arthas>

可以看到刚才启动的java程序占用进程号是10188,输入3,回车

下面出现ERROR的原因是因为之前已经进行过了连接,所以端口被占用,无法用此端口打开新的进程,这里还不清楚如何关闭端口,这里按照提示重新连接新的端口:java -jar arthas-boot.jar --telnet-port 4000 --http-port 4001

PS D:\cloudcore\open_tools\arthas> java -jar arthas-boot.jar --telnet-port 4000 --http-port 4001
[INFO] arthas-boot version: 3.0.5.2
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 17296
  [2]: 25752
  [3]: 10188 com.cloudcore.pms.transport.test.TestArthas
3
[INFO] arthas home: C:\Users\liuguangyu\.arthas\lib\3.0.5\arthas
[INFO] Try to attach process 10188
[INFO] Found java home from System Env JAVA_HOME: C:\Java\jdk1.8.0_161
[WARN] Current VM java version: 1.8 do not match target VM java version: 1.7, attach may fail.
[WARN] Target VM JAVA_HOME is C:\Java\jdk1.7.0_67\jre, try to set the same JAVA_HOME.
[INFO] Attach process 10188 success.
[INFO] arthas-client connect 127.0.0.1 4000
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'


wiki: https://alibaba.github.io/arthas
version: 3.0.5
pid: 10188
time: 2019-01-09 16:04:48

$

还有一种情况,就是需要将此arthas-boot.jar上传到没有外网环境的服务器上进行监测,在可以连接外网环境的本地或者其他地方获取执行以来的jar包,默认在/home/$user/.arthas/lib/ 下面有使用的版本jar:

将lib下的文件整个上传到服务器的指定目录下即可。

二、命令列表

Arthas的基础命令:

  • help——查看命令帮助信息
  • cls——清空当前屏幕区域
  • session——查看当前会话的信息
  • reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类
  • version——输出当前目标 Java 进程所加载的 Arthas 版本号
  • history——打印命令历史
  • quit——退出当前 Arthas 客户端,其他 Arthas 客户端不受影响
  • shutdown——关闭 Arthas 服务端,所有 Arthas 客户端全部退出
  • keymap——Arthas快捷键列表及自定义快捷键

再次贴出官方文档:https://alibaba.github.io/arthas/install-detail.html

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: Arthas 是一款面向 Java 应用的开发工具,旨在帮助开发人员更好地理解和调试 Java 应用程序的运行状态。它的功能非常丰富,可以帮助开发人员解决许多常见的问题,例如应用程序性能问题、内存泄漏、线程问题、类加载问题等等。 下面是 Arthas 的一些常用使用技巧: 1. 安装和启动 Arthas 首先,我们需要在本地安装 Arthas。安装方法可以参考 Arthas 的官方文档。安装完成后,我们就可以使用 Arthas 连接到正在运行的 Java 应用程序了。 2. 查看应用程序的基本信息 使用 Arthas 可以查看应用程序的基本信息,例如类加载器、系统属性、线程信息等。使用命令 `system` 可以查看这些信息。 3. 查看应用程序的方法调用链 使用 Arthas 可以查看应用程序的方法调用链,即方法的调用关系。使用命令 `trace` 可以查看方法调用链。 4. 查看方法的执行时间 使用 Arthas 可以查看方法的执行时间,即方法的平均执行时间、最大执行时间、最小执行时间等。使用命令 `tt` 可以 ### 回答2: Arthas 是一个强大的Java开发者工具,可以帮助开发人员在调试和诊断Java应用程序过程中提供便利。下面我将用300字回答如何使用Arthas。 首先,安装Arthas是必需的。可以通过在终端窗口中运行命令来安装Arthas: curl -L https://alibaba.github.io/arthas/install.sh | sh 一旦安装了Arthas,就可以通过运行arthas命令来启动Arthas控制台。Arthas的控制台提供了许多命令和选项来帮助开发人员调试和诊断Java应用程序。 其中一个非常有用的功能是监视方法的调用。可以使用如下命令来监视方法的调用: watch com.example.demo.DemoClass demoMethod "{params, returnObj}" 上述命令将监视com.example.demo.DemoClass类中的demoMethod方法,并打印方法的参数和返回值。 另一个有用的功能是查看方法的代码。可以使用如下命令来查看方法的代码: jad com.example.demo.DemoClass demoMethod 上述命令将显示com.example.demo.DemoClass类中的demoMethod方法的源代码。 Arthas还提供了一些其他功能,如查看线程堆栈、监视HTTP请求、查看类加载器信息等。我们可以使用help命令来查看所有可用的命令和选项。 Arthas还支持在生产环境中远程调试Java应用程序。可以使用如下命令远程连接到应用程序: arthas-boot.jar -c com.example.demo.App --address 127.0.0.1:8080 上述命令将连接到运行在127.0.0.1:8080上的com.example.demo.App应用程序。 总之,Arthas是一个功能强大的开发者工具,可以帮助开发人员在调试和诊断Java应用程序过程中提供便利。通过监视方法调用、查看方法代码以及其他各种功能,开发人员可以更快地定位和解决问题。Arthas还支持在生产环境中进行远程调试,为开发人员提供了更多的灵活性和便利性。 ### 回答3: Arthas是一款开发工具,用于Java应用程序的故障排查和性能优化。它为开发者提供了丰富的命令行工具和可视化界面,帮助他们更轻松地分析和调试应用程序。下面是一些使用Arthas的技巧: 1. 熟悉Arthas命令行工具:Arthas提供了大量的命令,用于监控和调试Java应用程序。通过掌握并熟练使用这些命令,开发者可以更有效地分析和解决问题。 2. 实时监控应用程序:Arthas可以监控应用程序的各个方面,包括类加载、方法调用、线程状态等。通过查看实时监控数据,开发者可以了解应用程序的运行状况,并及时发现和解决问题。 3. 进行方法拦截和修改:Arthas提供了方法拦截和修改的功能,可以在运行时动态地拦截和修改Java方法。这个功能非常有用,可以帮助开发者在不修改原始代码的情况下进行调试和测试。 4. 使用断点进行调试:Arthas支持断点功能,开发者可以在应用程序中设置断点,并通过调试命令查看断点处的变量和调用栈信息。这对于定位问题和调试复杂的应用程序非常有帮助。 5. 进行性能分析和优化:Arthas可以进行性能分析和优化,开发者可以通过查看应用程序中的热点方法和对象分配情况,找到性能瓶颈,并进行相应的优化。 总结来说,Arthas是一款功能强大的Java开发工具,它可以帮助开发者更轻松地分析和调试应用程序,提高开发效率。通过熟练掌握Arthas使用技巧,开发者可以更快速地解决问题,并优化应用程序的性能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值