1

Preface

Everyone knows how arthas is possible. In order to make up the number of characters, I will copy the official introduction again.

Arthas is Alibaba's open source Java diagnostic tool, which is deeply loved by developers. When you encounter the following similar problems and are at a loss, Arthas can help you solve them:

  1. Which jar package is this class loaded from? Why are various types of related Exception reported?
  2. Why is the code I changed not executed? Could it be that I didn't commit? Wrong branch?
  3. I can’t debug online if I encounter a problem, can I only republish it by adding a log?
  4. I encountered a user's data processing problem online, but the online debugging is also impossible, and the offline cannot be reproduced!
  5. Is there a global perspective to view the health of the system?
  6. Is there any way to monitor the real-time running status of the JVM?
  7. How to quickly locate application hot spots and generate flame graphs?

I won’t talk about how to use arthas in detail today, because I wrote a blog post about it before, and interested friends can check the following link

java application online diagnostic artifact--Arthas

With the popularity of containerization, a considerable part of the projects may be based on docker deployment. Today, I will mainly talk about the springboot project running in the docker environment and how to enable arthas.

How to enable arthas for the springboot project in the docker environment

Solution 1: Enter the container and execute the corresponding command

 docker exec -it d2ce06ad8855 /bin/bash
 进入容器后,再运行
 curl -O https://arthas.aliyun.com/arthas-boot.jar
 java -jar arthas-boot.jar

The disadvantage of this scheme is that after the container is destroyed, the arthas-boot.jar must be downloaded again after the container is run next time.

Solution two, install arthas to 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" ]

Solution three, use arthas-spring-boot-starter

1. Introduce in the pom.xml of the project
 <dependency>
            <groupId>com.taobao.arthas</groupId>
            <artifactId>arthas-spring-boot-starter</artifactId>
            <version>${arthas.version}</version>
        </dependency>
2. Configure in yml

There are two ways to configure here

  • 2.1, configure arthas tunnel server to achieve remote management

Note: remotely manage/connect multiple agents through Arthas Tunnel Server/Client

So we must first install Arthas Tunnel Server/Client

a, download and deploy arthas tunnel server

https://github.com/alibaba/arthas/releases

b. Start arthas-tunnel-server

Example

java -jar  arthas-tunnel-server-3.5.0-fatjar.jar

Note: By default, the web port of arthas tunnel server is 8080, and the port of arthas agent connection is 7777

c, do the following configuration in the 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}

Note: agentId must be unique, otherwise it will conflict on the tunnel server and not work properly

d, effect demonstration

image.png

  • 2.2. Do the following configuration directly in the yml of the project
arthas:
 # 通过http访问的端口
  http-port: 8563
  # 通过telnet访问的端口
  telnet-port: 3658
  session-timeout: 1800
  # 绑定的ip
  ip: 0.0.0.0

NOTE: if configured arthas.telnetPort -1, telnet port is not listening. If arthas.telnetPort is configured as 0, the telnet port will be random. arthas.httpPort is similar

Effect demonstration

  • a, access via http

image.png

  • b. Access via telnet

image.png

Getting started with arthas

The core idea is through the help command, first through help to understand what commands arthas has

image.png
Then use the help command you are interested in. For example, if you are interested in the thread command, enter it on the command line

help thread

image.png
Sample demonstration, such as checking whether there is a deadlock in the project

thread -b

image.png
We also use dashboard commands to view information such as cpu, gc, etc.

dashboard

image.png

to sum up

Arthas does make it easier for us to troubleshoot java problems, but arthas has so many commands that we can’t remember many times. Even if we can, I won’t remember it. The routine I use normally is to help the command first, and then find the corresponding For example, the last picture is the cat and the tiger.

If you use http or telnet, based on security considerations, the ip is best to use intranet ip instead of 0.0.0.0 as in my example, I am for the convenience of demonstration.

demo link

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-arthas


linyb极客之路
347 声望193 粉丝