This article is intended for those with basic Java knowledge.

Author: HelloGitHub- Qin Ren

Arthas is an open source Java diagnostic tool from Alibaba. Arthas is an open source Java diagnostic tool from Alibaba. Arthas is an open source Java diagnostic tool from Alibaba

The project source address: https://github.com/alibaba/arthas

A list,

Why Arthas? Many Java developers may encounter the following problems:

  • The project imported different versions of a JAR package. From which jar package was the class loaded? Why does the online environment report all kinds of exceptions?

  • The local project runs fine. Why does the online environment run differently from the local one? Data failure? Code not commit? Is the branch used in the environment wrong?

  • When the online environment encounters accidental problems, can we only add logs, adjust the project log level, and repackage the release validation issues?

  • The online environment encounters a user’s approval process is not correct, and the offline environment cannot be reproduced. How to conduct remote debugging online?

  • Is there a function to monitor the overall health of the system?

  • The JDK comes with a number of monitoring tools that can monitor JVM health dynamically locally, so how can an online environment monitor JVM health in real time?

  • How can the online environment quickly locate application hotspots and generate flame maps?

1.1 Implementation Principles

The overall macro module call diagram is as follows:


1.2 Main Functions

Arthas provides three main features:

  1. Information monitoring
    • Basic process running information includes memory, CPU usage, thread information, thread stack, thread statistics, and environment variables.
    • Object information: class object static properties, Mbean property information, loaded class information, class loader, class method information.
  2. The method call
    • Method call input parameter, return value view.
    • Method invocation path, invocation time, method invocation times, success times, failure times, etc.
    • Log and redo method calls.
  3. Class file handling
    • Dump bytecode, bytecode decompilation, class compilation, and class reloading of loaded classes.

Two, installation and use

2.1 Using Linux

Log in to your Linux environment, download arthas-boot.jar, and run the jar package directly using the command Java -jar xxx.jar.

The following two commands have the same effect and can be downloaded.

curl -O https://alibaba.github.io/arthas/arthas-boot.jar
wget https://alibaba.github.io/arthas/arthas-boot.jar
Copy the code

The first step to Arthas is to select a project


2.2 Docker Environment Usage

Enter a Docker container that was already started, and here I’m in the Tomcat7 container.

docker ps -a # View all containers
docker cp arthas-boot.jar tomcat7:/home Copy jar to container home directory
docker exec -it tomcat7 bash Enter the container named Tomcat7
cd /home 
java -jar arthas-boot.jar Run the jar package
Copy the code

Note: The Docker container selected must be built with JDK based dependencies.

You can enter dashboard from the command line to access all the data on the dashboard.


2.3 Use in the development tool IntelliJ IDEA

The Cloud Toolkit is an IDE plug-in that helps developers develop, test, diagnose, and deploy applications more efficiently. Easy one-click deployment of local applications to any machine, or ECS, EDAS, Kubernetes. This is just about connecting to a remote server, using Arthas.

2.3.1 Installing plug-ins

Search for Alibaba Cloud Toolkit in File->Settings->Plugins


Restart IDEA after the plug-in is installed.

2.3.2 Using tools

Add a remote server as shown in the following figure:


After the server is configured, choose More >Diagnostic to connect to the server.


2.3.3 Operation Effect


Iii. Actual combat case analysis

With online code hot updates, here I’ve written a small Sprinboot project, which is basically an interface to get learning information.

@RestController
public class StudentConroller {

   @GetMapping("getUserInfo")
   public Student getUserInfo(a) {
 return new Student("Liu".12."Xi 'an Yanta District");  } } Copy the code

Through the curl http://localhost:9000/getUserInfo, access to content is as follows:

{"name":"Liu"."id":12."address":"Xi 'an Yanta District"}
Copy the code

Run the project to be tested, demo-0.0.1- snapshot.jar, on the service

Nohup Java -jar demo-0.0.1 -snapshot.jar &# background run
curl http://localhost:9000/getUserInfo # access interface
Copy the code

Run Arthas main program arthas-boot.jar and select Demo-0.0.1 – snapshot.jar


Use jad to decompress studentConroll. Java code

jad --source-only com.example.demo.controller.StudentConroller > /tmp/StudentConroller.java
Copy the code

Under the open/TMP/StudentConroller. Java file, modify the compilation of code, modify the content is as follows:

@RestController
public class StudentConroller {
   @GetMapping(value={"getUserInfo"})
   public Student getUserInfo(a) {
      return new Student("Liu 1".122.Xi 'an High-tech Zone);
 } } Copy the code
  • Sc command to query the ClassLoader that loads StudentConroller

    $ sc -d *StudentConroller | grep classLoaderHash
    classLoaderHash   2e0fa5d3
    Copy the code
  • MC command memory compiled code

    $ mc -c 2e0fa5d3 /tmp/StudentConroller.java -d /tmp
    Memory compiler output:/tmp/com/example/demo/arthas/user/controller/StudentConroller.class
    Affect(row-cnt:1) cost in 346 ms
    Copy the code
  • Re-define command hot update code

    $ redefine /tmp/com/example/demo/controller/StudentConroller.class
    redefine success, size: 1
    Copy the code
  • Check hot update results for re-accesscurl http://localhost:9000/getUserInfoThe following information is displayed:

Use Arthas’s JAD/MC/Re-define dragon to hot update code online is powerful, but dangerous. Manage permissions.

Four,

This article begins with what Arthas does and why we use it. Arthas was then used in three scenarios. Finally, Arthas gets a feel for the power of hot deployment of Java code online. I think you have a simple idea of the Arthas tool as well.

There is a tendency for developers to become DevOps over time, and to learn more about the underlying logic and better feedback to the superstructure at the code level.

This article is just a primer, Arthas has many more powerful features to discover!

Five, the appendix

  • Arthas official documentation
  • Arthas Brain Map – Summary of common commands

Pay attention to the public account to join the communication group