Jinshi Original | [JVM Actual Combat Series] “Monitoring and Tuning System” actual combat development arthas-spring-boot-starter monitors whether your microservices are healthy!

Prerequisites

I believe that if you have gone through my last Arthas article [[JVM Actual Combat Series] “Monitoring and Tuning System” for Alibaba-Arthas Installation and Basic Use Development Practical Guide], I believe that you should understand the functions and use of Arthas. With a certain understanding. Then we will go to the next step to explore the function.

Arthas support and monitoring system for SpringBoot2

After adding arthas-spring-boot-starter to the SpringBoot2 application, Spring will start the arthas service, attach its own process, and cooperate with the tunnel server to realize remote management. Such a solution is very suitable for remote diagnosis in a microservice container environment. In a container network environment, only the port of the tunnel server needs to be exposed externally.

Component support required by Arthas monitoring system

  • Arthas Tunnel Server/Client (management and monitoring of Java agent probes, which is convenient for us to manage services and probes)

  • Web Console

What is Arthas Tunnel

In a containerized deployment environment, Java processes can be started on different machines. It would be troublesome to use Arthas to diagnose, because users usually do not have machine permissions, and even if they log in to a machine, they cannot tell which Java process it is. In this case Arthas Tunnel Server/Client can be used.

The function and purpose of Arthas Tunnel

In the entire functional system of Arthas, multiple Agents can be remotely managed/connected through Arthas Tunnel Server/Client (that is, multiple JVM processes can be monitored). The main purpose is to monitor and obtain process data information of the target JVM.

Download and deploy Arthas tunnel server

Github source warehouse download

The download address is Arthas tunnel server. The latest version is arthas-all-3.6.7, as shown in the figure below.

Introduce the download information for the installation package of Arthas:

  • arthas-3.6.7.deb: the installation package mainly used for debian operating system to run

  • arthas-bin.zip: binary executable package

  • arthas-doc.zip: documentation for arthas

  • arthas-tunnel-server-3.6.7-fatjar.jar: Jar executable package of Arthas tunnel server service

  • Source code(zip): source code zip archive

  • Source code(tar.gz): source code tar package

Maven warehouse download

Alibaba Cloud download address: arthas.aliyun.com/download/ar…

Run the corresponding Arthas tunnel server directly

Arthas tunnel server is a Spring boot fat jar application, directly started by java -jar:

java -jar arthas-tunnel-server.jar
Copy Code

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

Open the WebConsole, enter the ip (127.0.0.1) and port (7777) of the Arthas agent, and the agent-id (URJZ5L48RPBR2ALI5K4V) configured in the SpringBoot application, and click Connect.

Web Console

If you want to connect to the Arthas service through a browser, the Arthas service here does not refer to the Arthas tunnel server. Arthas is the overall service control end, the part that sends instructions, and the Arthas tunnel server is a special service for docking and managing agents (depending on service in Arthas Spring Boot Starter).

In addition to the CLI mode, Arthas currently supports Web Console, and users can directly access: http://127.0.0.1:8563/ after the attach is successful. You can fill in the IP and remotely connect to arthas on other machines. After startup, you can visit http://127.0.0.1:8080/, and then connect to the registered arthas agent through agentId, as shown in the figure below.

Through the Endpoint of Spring Boot, you can view the specific connection information: http://127.0.0.1:8080/actuator/arthas ,

The login username is arthas, and the password can be found in the log of the arthas tunnel server, for example:

Note: By default, arthas only listens to 127.0.0.1, so if you want to connect remotely, you can use the –target-ip parameter to specify the IP of the listen. For more information, refer to the help description of -h. Note that there will be security risks, consider the tunnel server solution below.

How to connect the service to Arthas tunnel server

There are two main modes to connect to Arthas tunnel server:

  1. Arthas server running remotely connects to Arthas tunnel server

  1. The agent probe service of the Arthas Spring Boot Starter running remotely connects to the Arthas tunnel server

Connect to tunnel server when starting arthas

When starting arthas, you can pass the –tunnel-server parameter, for example:

as.sh --tunnel-server 'ws://127.0.0.1:7777/ws'copy code

If you have special requirements, you can specify the agentId in the –agent-id parameter. By default, a random ID is generated. After the attach is successful, the agentId will be printed out.

 ,---. ,------. ,-------.,--. ,--. ,---. ,---.
 / O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--''--' `--' `--' `--'`--' `--'`--- --'

Wiki https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version 3.1.2
pid 86183
time 2022-11-30 15:40:53
id URJZ5L48RPBR2ALI5K4V

Copy Code

Even if it is not connected to the tunnel server at startup, the agentId can be obtained through the session command after the subsequent automatic reconnection is successful:

[arthas@86183]$ session
 Name Value
-------------------------------------------------- ---
 JAVA_PID 86183
 SESSION_ID f7273eb5-e7b0-4a00-bc5b-3fe55d741882
 AGENT_ID URJZ5L48RPBR2ALI5K4V
 TUNNEL_SERVER ws://127.0.0.1:7777/ws
Copy Code

Visit http://localhost:8080/arthas in the browser, enter agentId, and you can connect to arthas on this machine or other machines.

Key points of tunnel server
  • The agentId must be unique, otherwise it will conflict on the tunnel server and cannot work normally.

  • If the arthas agent is configured with appName, the generated agentId will be prefixed with appName.

Add the corresponding app-name parameter
Start parameters
as.sh --tunnel-server 'ws://127.0.0.1:7777/ws' --app-name demoapp , the generated agentId may be demoapp_URJZ5L48RPBR2ALI5K4V. Copy Code

Tunnel server will use _ as a separator to extract the appName, which is convenient for management by application.

Configuration parameters

arthas.properties in the unzipped arthas directory, or configure appName in application.properties of the spring boot application.

Arthas Spring Boot Starter’s agent service connection Jar

Only supports springboot2

  • Maven warehouse address: search.maven.org/search?q=a:…

  • Configure maven dependencies:

arthas.version:3.6.7

<dependency><groupId>com.taobao.arthas</groupId><artifactId>arthas-spring-boot-starter</artifactId><version>${arthas.version}</version></dependency>Copy Code

After the application starts, spring will start arthas and attach its own process. If you don’t know how to create or which dependencies to introduce, you can use one-click to create a project containing Arthas Spring Boot Starter: click to jump to the cloud-native scaffolding

You can see that the monitoring mechanism system of arthas has been automatically checked at the bottom.

application.yml configuration
arthas:agent-name:nihaotestagent-id:URJZ5L48RPBR2ALI5K4V#Manually specify agent-idtunnel-server:ws://127.0.0.1:7777/wsCopy code

View Endpoint information

You need to configure spring boot to expose the endpoint: Assuming the endpoint port is 8080, you can view it through the following url:

http://localhost:8080/actuator/arthas

{"arthasConfigMap":{"agent-id":"hsehdfsfghhwertyfad","tunnel-server":"ws://47.75.156.201:7777/ws",} }Copy code

Finally, start the SpringBoot service.

How to use non-spring boot applications

Non-Spring Boot applications can be used in the following ways:

<dependency><groupId>com.taobao.arthas</groupId><artifactId>arthas-agent-attach</artifactId><version>${arthas.version}</version></dependency><dependency> <groupId>com.taobao.arthas</groupId><artifactId>arthas-packaging</artifactId><version>${arthas.version}</version></dependency>Copy Code
attach its own service for probe detection.
import com.taobao.arthas.agent.attach.ArthasAgent;
public classArthasAttachExample {
publicstatic void main(String[] args) {
ArthasAgent. attach();
}
}
Copy Code

Properties can also be configured:

HashMap<String, String> configMap = newHashMap<String, String>();
configMap.put("arthas.appName", "demo");
configMap.put("arthas.tunnelServer", "ws://127.0.0.1:7777/ws");
ArthasAgent. attach(configMap);
Copy Code

Tunnel Server management page

Need to configure arthas.enable-detail-pages=true in application.properties of spring boot

Note that opening the management page is risky! The management page does not have a security blocking function, so be sure to add security measures yourself.

Start the tunnel-server locally, then use as.sh attach, and specify the application name –app-name test:

$ as.sh --tunnel-server 'ws://127.0.0.1:7777/ws' --app-name test
telnet connecting to arthas server... current timestamp is1627539688
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is'^]'.
  ,---. ,------. ,--------.,--. ,--. ,---. ,---.
 / O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--''--' `--' `--' `--'`--' `--'`--- --'

Wiki https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version 3.5.3
main_classdemo. MathGame
pid 65825
time 2022-07-29 14:21:29
id test_PE3LZO9NA9ENJYTPGL9L

Copy Code

Then visit tunnel-server, you can see a list of all connected applications:

http://localhost:8080/apps.html
Copy Code

Then open the details, you can see a list of all connected agents:

http://localhost:8080/agents.html?app=testCopy code

Author: Luoshen Yanshang

Link: https://juejin.cn/post/7213744681393176633

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 108563 people are studying systematically