轉載自:https://www.cnblogs.com/linhui0705/p/9795417.html
上個月,阿里開源了一個名為Arthas的監控工具。恰逢近期自己在寫多線程處理業務,由此想到了一個問題。
如果在本機開發調試,IDE可以看到當前的活動線程,例如IntelliJ IDEA,線程是運行還是休眠,在Debugger→Threads可以看到。倘若代碼寫完提交,到測試環境上構建,要怎樣才能看到測試服務器上的線程運行情況呢?
前面客套完了,教程GitHub上面也有,這里主要講下個人在部署的過程和踩到的坑。僅供參考。
GitHub: https://github.com/alibaba/arthas
Docs: https://alibaba.github.io/arthas/index.html
筆者用的是CentOS7裸機。看過教程,按照步驟執行命令:
1
|
curl -L https:
//alibaba
.github.io
/arthas/install
.sh | sh
|
下載了個名稱為as.sh的腳本,執行腳本,發現:
1
2
3
|
[root@bogon arthas]
# ./as.sh
Arthas script version: 3.0.4
illegal ENV, please
set
$JAVA_HOME to JDK6+
|
既然是Java監控,沒有Java運行環境是不可能跑得起來的(這不是廢話嗎)。馬上安裝JDK配置環境變量……這里筆者裝的是JDK8u181
JDK: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
解壓到/usr/java,執行vi /etc/profile配置環境變量
1
2
3
|
export
JAVA_HOME=
/usr/java/jdk1
.8.0_181
export
CLASSPATH=.:$JAVA_HOME
/lib/dt
.jar:$JAVA_HOME
/lib/tools
.jar:$JAVA_HOME
/jre/lib/rt
.jar
export
PATH=$PATH:$JAVA_HOME
/bin
|
:wq保存退出,再執行命令使配置的變量生效
1
|
source
/etc/profile
|
回到剛才的目錄,啟動arthas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
[root@bogon ~]
# cd /usr/local/arthas/
[root@bogon arthas]
# ./as.sh
Arthas script version: 3.0.4
Error: no available java process to attach.
Usage:
.
/as
.sh [-b [-f SCRIPT_FILE]] [debug] [--use-version VERSION] [--attach-only] <PID>[@IP:TELNET_PORT:HTTP_PORT]
[debug] : start the agent
in
debug mode
<PID> : the target Java Process ID
[IP] : the target's IP
[TELNET_PORT] : the target's PORT
for
telnet
[HTTP_PORT] : the target's PORT
for
http
[-b] : batch mode,
which
will disable interactive process selection.
[-f] : specify the path to batch script
file
.
[--attach-only] : only attach the arthas agent to target jvm.
[--use-version] : use the specified arthas version to attach.
Example:
.
/as
.sh <PID>
.
/as
.sh <PID>@[IP]
.
/as
.sh <PID>@[IP:PORT]
.
/as
.sh debug <PID>
.
/as
.sh -b <PID>
.
/as
.sh -b -f
/path/to/script
.
/as
.sh --attach-only <PID>
.
/as
.sh --use-version 2.0.20161221142407 <PID>
Here is the list of possible java process(es) to attatch:
|
結果發現,無可用Java進程。那就啟動一個咯(挖鼻屎),繼續……
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@bogon arthas]
# ./as.sh
Arthas script version: 3.0.4
Found existing java process, please choose one and hit RETURN.
* [1]: 1244
test
.jar
updating version 3.0.4 ...
######################################################################## 100.0%
.
/as
.sh: line 182: unzip:
command
not found
mv
: cannot stat ‘
/tmp/temp_3
.0.4_1265’: No such
file
or directory
update fail, ignore this update.
Calculating attach execution
time
...
Attaching to 1244 using version 3.0.4...
Error: Unable to access jarfile
/root/
.arthas
/lib/3
.0.4
/arthas/arthas-core
.jar
attach to target jvm (1244) failed, check
/root/logs/arthas/arthas
.log or stderr of target jvm
for
any exceptions.
|
然而,程序沒有如期運行成功。
……
通過度娘Google各種途徑,看了FAQ,作者建議是手動安裝+手動拼接命令行啟動。但是,有些麻煩,不喜歡(任性)
后面,看到了這個Issue: Error: Unable to access jarfile /root/.arthas/lib/3.0.4/arthas/arthas-core.jar。咦這不就是我共患難的兄弟嗎?馬上點進去。看到大神ralf0131的回答,頓時茅廁塞頓開:
1
|
First, please ensure
/root/
.arthas
/lib/3
.0.4
/arthas/arthas-core
.jar does exist.
|
cd到/root/.arthas/lib/3.0.4,發現目錄下沒有jar包。嘗不到這瓜有點不甘心,只好手動安裝了(真香~)
當前最新的包是3.0.4。下載,解壓:
1
2
3
4
|
[root@bogon 3.0.4]
# wget https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/3.0.4/arthas-packaging-3.0.4-bin.zip
......
[root@bogon 3.0.4]
# unzip arthas-packaging-3.0.4-bin.zip
-
bash
: unzip:
command
not found
|
好吧,我裝,我解壓……
1
2
|
[root@bogon 3.0.4]
# yum -y install unzip
[root@bogon 3.0.4]
# unzip arthas-packaging-3.0.4-bin.zip
|
頓時發現,剛才不是說/root/.arthas/lib/3.0.4/arthas路徑下木有jar包嗎,剛剛我把jar包解壓到/root/.arthas/lib/3.0.4,只要再新建個arthas目錄,再把4個jar包移進去就OK了。
1
2
3
|
[root@bogon 3.0.4]
# mkdir arthas
[root@bogon 3.0.4]
# mv *.jar arthas/
[root@bogon arthas]
# ./as.sh
|
大功告……你以為這樣就結束了?還有問題呢……
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Arthas script version: 3.0.4
Found existing java process, please choose one and hit RETURN.
* [1]: 1244
test
.jar
Calculating attach execution
time
...
Attaching to 1244 using version 3.0.4...
real 0m0.620s
user 0m0.148s
sys 0m0.043s
Attach success.
Connecting to arthas server... current timestamp is 1539615611
.
/as
.sh: line 423:
type
: telnet: not found
'telnet'
is required.
|
還要裝telnet-client……
1
|
[root@bogon 3.0.4]yum -y
install
telnet.x86_64
|
最后再啟動,回車,大功告成!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[root@bogon arthas]
# ./as.sh
Arthas script version: 3.0.4
Found existing java process, please choose one and hit RETURN.
* [1]: 1244
test
.jar
Calculating attach execution
time
...
Attaching to 1244 using version 3.0.4...
real 0m0.132s
user 0m0.050s
sys 0m0.113s
Attach success.
Connecting to arthas server... current timestamp is 1539615853
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is
'^]'
.
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--.
''
--. .--
'| '
--
' | / O \ '
.-'
| .-. ||
'--'
.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--
' `--'
`--
' '
--
' `--'
`--
' `--'
`--
' `--'
`-----'
wiki: https:
//alibaba
.github.io
/arthas
version: 3.0.4
pid: 1244
timestamp: 1539615854095
$
|
接下來就可以各種命令各種姿勢各種把弄了……
總結:
-
安裝arthas前需要裝JDK,配置好JAVA_HOME等環境變量,以及telnet。unzip看情況,如果你解壓完再上傳到服務器,也行……
-
不知道是bug還是神馬情況,按照文檔的步驟再一次裝在測試的服務器上,程序正常運行,沒有報Error: Unable to access jarfile /root/.arthas/lib/3.0.4/arthas/arthas-core.jar的錯誤,4個jar包都在預期的路徑。簡單來說,有的機器運行./as.sh是會因少jar包報錯的,有的是會正常運行的,具體情況具體分析。后期繼續關注arthas的GitHub動態,看項目的最新進展。
就醬。睡覺……