preface
Arthas is probably known to you, but let me recap the official introduction for a few words
Arthas is an open source Java diagnostic tool for Alibaba that developers love. Arthas can help you when you are stuck with a problem like the following:
- From which JAR is this class loaded? Why are all kinds of class-related exceptions reported?
- Why didn’t the code I changed execute? Did I not commit? Got the branch wrong?
- If you encounter a problem, you cannot debug it online. Can you only re-publish it by logging?
- There is a problem with a user’s data processing online, but it cannot be debugged online, and it cannot be reproduced offline!
- Is there a global view of the health of the system?
- Is there any way to monitor the real-time health of the JVM?
- How to quickly locate application hot spots, generate flame map?
I’m not going to go into details about how to use arthas today, because there was a blog post about arthas, but if you’re interested, check out the link below
Arthas, an online diagnostic wizard for Java applications
With the advent of containerization, there may be a significant number of projects that are docker-based. Today we are going to talk about springboot projects running in docker environments. How to enable arthas
How to enable arthas for springboot project in docker environment
Solution 1: Enter the container and run the corresponding command
Docker exec it d2ce06ad8855 /bin/bash Running curl - O https://arthas.aliyun.com/arthas-boot.jar Java - jar arthas - the boot. The jarCopy the code
The downside of this solution is that after the container is destroyed, arthas-boot.jar is downloaded again the next time the container is run
Option 2: Install arthas into the base image
FROM openjdk:8-jdk-alpine
VOLUME /tmp
#ENV JAVA_OPTS="-Dcom.sun.management.jmxremote.port=39083 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
ENV JAVA_OPTS=""
COPY localtime /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone
ONBUILD COPY app.jar app.jar
ARG ARTHAS_VERSION="3.5.0"
ARG MIRROR=false
ENV MAVEN_HOST=https://repo1.maven.org/maven2 \
ALPINE_HOST=dl-cdn.alpinelinux.org \
MIRROR_MAVEN_HOST=https://maven.aliyun.com/repository/public \
MIRROR_ALPINE_HOST=mirrors.aliyun.com
# if use mirror change to aliyun mirror site
RUN if $MIRROR; then MAVEN_HOST=${MIRROR_MAVEN_HOST} ; ALPINE_HOST=${MIRROR_ALPINE_HOST} ; sed -i "s/dl-cdn.alpinelinux.org/${ALPINE_HOST}/g" /etc/apk/repositories ; fi && \
# https://github.com/docker-library/openjdk/issues/76
apk add --no-cache tini && \
# download & install arthas
wget -qO /tmp/arthas.zip "${MAVEN_HOST}/com/taobao/arthas/arthas-packaging/${ARTHAS_VERSION}/arthas-packaging-${ARTHAS_VERSION}-bin.zip" && \
mkdir -p /opt/arthas && \
unzip /tmp/arthas.zip -d /opt/arthas && \
rm /tmp/arthas.zip
ENTRYPOINT ["/sbin/tini"."--"."sh"."-c"."java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
Copy the code
Arthas-spring-boot-starter
1. Introduce pom.xml in the project
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spring-boot-starter</artifactId>
<version>${arthas.version}</version>
</dependency>
Copy the code
2. Configure in YML
There are two ways to configure this
- 2.1. Configure arthas Tunnel Server for remote management
Note: Arthas Tunnel Server/Client is used to remotely manage/connect multiple agents
Arthas Tunnel Server/Client
A. Download and deploy arthas Tunnel Server
Github.com/alibaba/art…
B. Enable arthas-tunnel-server
The sample
java -jar arthas-tunnel-server-3.5. 0-fatjar.jar
Copy the code
Note: By default, the arthas Tunnel Server web port is 8080 and the arthas Agent connects to port 7777
C. Do the following configuration in yML of the project
arthas:
agent-id: ${ARTHAS_AGENT_ID:hsehdfsfghhwertyfad}
app-name: ${spring.application.name}
tunnel-server: ${ARTHAS_TUNNEL_SERVER:ws://localhost:7777/ws}
Copy the code
Note: The agentId must be unique. Otherwise, the agentId conflicts with the Tunnel server and cannot work properly
D. Effect demonstration
- 2.2. Do the following configuration in yML of the project directly
arthas:
# Port accessed via HTTP
http-port: 8563
Port accessed through Telnet
telnet-port: 3658
session-timeout: 1800
# Bound IP
ip: 0.0. 0. 0
Copy the code
Note: If arthas. TelnetPort is set to -1, Telnet ports are not listened on. If arthas. TelnetPort is set to 0, the Telnet port is random. Arthas. HttpPort is similar
Results demonstrate
- A. Access through HTTP
- B. Access through Telnet
Arthas for introductory use
The main idea is to use the help command. Let’s start with help and see what arthas has
For example, if you are interested in thread, enter it on the command line
help thread
Copy the code
Example demonstrations, such as checking for deadlocks in a project
thread -b
Copy the code
We also use dashboard commands to check CPU, GC, etc
dashboard
Copy the code
conclusion
Arthas does make it easier to troubleshoot Java problems, but arthas has so many commands that most of the time we don’t remember them, and even if we did, I don’t remember them. My normal routine is to start with a help command, find an example, and follow through.
If HTTP or Telnet is used, for security reasons, it is better to use the internal IP address rather than 0.0.0.0 as in my example, for the convenience of demonstration.
The demo link
Github.com/lyb-geek/sp…