I remember that I wrote an Arthas usage tutorial before. By using Arthas, we can realize both online debugging and hot repair. Recently I visited the official website of Arthas and found that it already supports direct integration into SpringBoot applications, and also has a dedicated IDEA plug-in. Let's experience it again today to see if its function is more powerful!
SpringBoot actual e-commerce project mall (50k+star) address: https://github.com/macrozheng/mall
Introduction to Arthas
Arthas is Alibaba's open source Java diagnostic tool, which is very popular among developers. It is currently available on Github 29K+Star
. It adopts the command-line interactive mode and provides rich Tab auto-completion functions to further facilitate problem location and diagnosis.
ArthasTunnel
In order to demonstrate a more realistic online environment, next we will diagnose the SpringBoot application in the Docker container. We will useArthasTunnel
to implement,ArthasTunnel
is equivalent to a web console, which allows us to diagnose the application without entering the application container, which is very convenient.
- First we need to download the installation package of
ArthasTunnel
, download address: https://github.com/alibaba/arthas/releases
- Since only the JAR package is officially provided, if you want to start it through Docker, you can package the Docker image yourself. The Dockerfile script used for packaging is as follows:
# 该镜像需要依赖的基础镜像
FROM java:8
# 将当前目录下的jar包复制到docker容器的/目录下
ADD arthas-tunnel-server.jar /arthas-tunnel-server.jar
# 声明服务运行的端口
EXPOSE 8080 7777
# 指定docker容器启动时运行jar包
ENTRYPOINT ["java", "-jar","/arthas-tunnel-server.jar"]
# 指定维护者的名字
MAINTAINER macro
- Here is another one-click package operation
ArthasTunnel
container execution scriptrun.sh
, the script content is as follows;
#!/usr/bin/env bash
# 定义应用组名
group_name='mall-tiny'
# 定义应用名称
app_name='arthas-tunnel-server'
# 定义应用版本
app_version='1.0-SNAPSHOT'
echo '----copy jar----'
docker stop ${app_name}
echo '----stop container----'
docker rm ${app_name}
echo '----rm container----'
docker rmi ${group_name}/${app_name}:${app_version}
echo '----rm image----'
# 打包编译docker镜像
docker build -t ${group_name}/${app_name}:${app_version} .
echo '----build image----'
docker run -p 8080:8080 -p 7777:7777 --name ${app_name} \
-e TZ="Asia/Shanghai" \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/${app_name}/logs:/var/logs \
-d ${group_name}/${app_name}:${app_version}
echo '----start container----'
- Next, upload the JAR package, Dockerfile, and execution script
run.sh
ArthasTunnel
---9cd3fe8c6dbdf2627769610768a256cd--- to the Linux server, and then use the./run.sh
command to run it;
- After successful operation, you can directly access the web console of
ArthasTunnel
, and visit the address: http://192.168.3.105:8080
SpringBoot integration
It is undoubtedly the most convenient to directly integrate and use Arthas in the SpringBoot application, and we will use this method next.
- First, add the following dependencies to the project
pom.xml
, you can compare the usage in the Arthas tutorial , the direct integration is indeed much simpler;
<!--集成Java诊断利器Arthas-->
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spring-boot-starter</artifactId>
<version>3.6.1</version>
</dependency>
- Then modify the configuration file
application.yml
, remember thisagent-id
,ArthasTunnel
The connection needs to be used, because we will use the application container through--link
way to connect to theArthasTunnel
container, wheretunnel-server
is configured as follows;
management:
endpoints:
web:
exposure:
# 暴露端点`/actuator/arthas`
include: 'arthas'
arthas:
agent-id: mall-tiny-arthas
tunnel-server: ws://arthas-tunnel-server:7777/ws
- 接下来通过之前的Dockerfile和
run.sh
应用,run.sh
对比,只多--link
连接到ArthasTunnel
容器The command;
- The Dockerfile and running script used for packaging
run.sh
have been included in the sample code, and the structure is as follows;
- Next, enter ---01b15c061bc1f8f2d534aa55dd193638
ArthasTunnel
in the Web console ofAgentId
asmall-tiny-arthas
, and click theConnect
button to start the diagnosis;
- For example, through the
dashboard
command to display the real-time data panel of the current system, including thread information, JVM memory information and JVM runtime parameters;
- Another example is to use the
thread
command to view the current thread information, view the thread stack, and find the thread that currently occupies the most CPU;
- Of course, the functions of Arthas are very powerful, far more than that. It supports dynamic modification logs and hot updates. For details, please refer to the Arthas tutorial .
IDEA plugin
Because Arthas is very powerful, there are many commands that need to be remembered, and sometimes I can't remember it, so I have this IDEA plug-in, which is mainly used to help generate Arthas commands.
- Search directly in IDEA's plug-in market
arthas
to find the plug-in, and then click Install;
- After the installation is complete, let's talk about how to use it. For example, when we feel that the online code is inconsistent with our expectations, we can use the
jad
command to decompile and see, directly select the class, right-click the Arthas command, and then select Jad to decompile ;
- At this point, the command will be copied directly to the clipboard, and then right-clicked and pasted to
ArthasTunnel
, which is much easier than typing the command by hand!
- If you want to observe the parameters and return values during the execution of the method, you can use the
watch
command, select the method to be observed and right-click to select it;
- Observe the execution process of the method in the Controller here;
- We can also modify the log level of a class separately, right-click the class name and select the
logger
command;
- First copy the
logger sc
command to check that the log level of the current class isINFO
;
- Copy the Hash value of the ClassLoader, because in Linux
Ctrl+C
key conflict, use theCtrl+Insert
combination to copy;
- Next, enter the Hash value of ClassLoader, modify the log level, and then copy the command to modify the log level;
- Check the log level after execution, it has been changed to
DEBUG
level
Summarize
Today, I experienced a new version of Arthas, which is very convenient to use with ArthasTunnel and IDEA plug-ins! And it can be seamlessly integrated with SpringBoot, which is really powerful. For more usage of Arthas, please refer to the Arthas usage tutorial .
References
- Project official website: https://github.com/alibaba/arthas
- Arthas official documentation: https://arthas.aliyun.com/doc/index.html
- IDEA plugin usage documentation: https://www.yuque.com/arthas-idea-plugin
Project source code address
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-arthas2
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。