本文参考官方文档: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

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐