Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of troubleshooting production Environment with Arthas in Java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the method of Java using Arthas to check the production environment". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preface

Arthas is Alibaba's open source Java diagnostic tool. Troubleshoot problems online without restarting; dynamically track Java code; monitor JVM status in real time. For online anomalies that count against the clock, Arthas can help us quickly diagnose related problems.

Download and install

Download arthas-boot.jar for Arthas

Wget https://alibaba.github.io/arthas/arthas-boot.jar

After downloading arthas, let's first learn about the help information, which can be viewed through the java-jar arthas-boot.jar-h command. Here are some examples and parameter instructions.

[root@izwz94a0v1sz0gk4rezdcbz arthas] # java-jar arthas-boot.jar-h [INFO] arthas-boot version: 3.1.4Usage: arthas-boot [- h] [--target-ip] [--telnet-port] [--http-port] [--session-timeout] [--arthas-home] [--use-version] [--repo-mirror] [--versions] [--use-http] [- -attach-only] [- c] [- f] [--height] [--width] [- v] [--tunnel-server] [--agent-id] [--stat-url] [pid] Bootstrap ArthasEXAMPLES: java-jar arthas-boot.jar java-jar arthas-boot.jar-- target-ip 0.0.0.0 java-jar arthas-boot.jar-- telnet-port 9999-- http-port-1 Java-jar arthas-boot.jar-- tunnel-server 'ws://192.168.10.11:7777/ws' java-jar arthas-boot.jar-- tunnel-server' ws://192.168.10.11:7777/ws'--agent-id bvDOe8XbTM2pQWjF4cfw java-jar arthas-boot.jar-- stat-url 'http://192.168.10.11:8080/api/stat' java-jar arthas-boot.jar-c' sysprop Thread' java-jar arthas-boot.jar-f batch.as java-jar arthas-boot.jar-use-version 3.1.4 java-jar arthas-boot.jar-- versions java-jar arthas-boot.jar-- session-timeout 3600 java-jar arthas-boot.jar-- attach-only java-jar arthas-boot.jar-- repo-mirror aliyun-- use-httpWIKI: https://alibaba.github.io/arthasOptions and Arguments:-h -help Print usage-- target-ip The target jvm listen ip, default 127.0.0.1-- telnet-port The target jvm listen telnet port, default 3658-- http-port The target jvm listen http port, default 8563-- session-timeout The session timeout seconds Default 1800 (30min)-arthas-home The arthas home-use-version Use special version arthas--repo-mirror Use special maven repository mirror, value is center/aliyun or http repo url. -- versions List local and remote arthas versions-- use-http Enforce use http to download, default use https-- attach-only Attach target process only, do not connect-c Murray Murray command Command to execute, multiple commands separated by -- The batch file to execute The batch file to execute-- height arthas-client terminal height-width arthas-client terminal width-- VMagneWhile verbose Verbose, print debug info. -- tunnel-server The tunnel server url-- agent-id The agent id register to tunnel server-- stat-url The report stat url Target pid start

Before starting arthas, start an springboot application. The demo is at the address https://github.com/yangtao...

Java-jar ytao-springboot-demo.jar

Start the arthas-boot.jar command

Java-jar arthas-boot.jar

Note here that you need to start demo and arthas with the same privileged user, otherwise you can't get the process information by using the attach mechanism (I didn't notice this when I first used it, but I encountered this problem). Example: when the root user starts demo,u1, when the user starts arthas, the print message Can not find java process. Try to pass in command line.

Look at the source code and add the log output after getting the process. If the result is empty,-1 is returned. If the result is less than 0, exit directly.

Start the code of class Bootstrap#main

The code of the process tool class ProcessUtils#select

From the above analysis, before we start arthas, we must start our target process, otherwise arthas may not be able to start.

Launch the successful interface using the root user

Select the java process. Here our ytao-springboot-demo is 1. After selection, there will be connection information.

[INFO] arthas home: / root/.arthas/lib/3.1.4/arthas [INFO] Try to attach process 22005 [INFO] Attach process 22005 success. [INFO] arthas-client connect 127.0.0.1 3658, -. . . ,. ,. ,. / O\ |. .--'|'|'-'| / O\'. -'|. | |'-. |. -. | |.. | `. `-. |\ |. -'| `- -'|`--'`- -'-' Wiki https://alibaba.github.io/arthas tutorials https://alibaba.github.io/arthas/arthas-tutorials version 3.1.4 pid 17339 Time 2019-10-17 02:29:06dashboard data Panel

Using the dashboard command, you can view thread, memory, GC, and Runtime information

Jad decompilation

Sometimes we encounter that the running result of the online code is not what we expect, and there is a situation where the online code is not the version we want, but to view it, we need to download it and decompile it. At this point, arthas's jad can help us do real-time decompilation online to confirm that the code conforms to our version.

Jad com.ytao.service.UserServiceImpl

Watch function execution information

Use the watch command to view the execution information of the function. Parameter list of watch (from the official website)

Parameter description class-pattern class name expression matching method-pattern method name expression matching express observation expression condition-express conditional expression [b] before method call observe [e] after method exception observe [s] after method return observe [f] after method end (normal return and exception return) observe [E] turn on regular expression matching The default is wildcard match [x:], which specifies the attribute traversal depth of the output, and defaults to 1.

When we encounter online data bug, our general approach is for the development environment to simulate online data, look for clues from production logs, or remote debug. No matter which of the above means of investigation, it is relatively troublesome. At this point, Arthas's watch can help us see the real-time code execution. Use the observation expression to view the parameters, return values, and exception information of the function. Observation expressions are mainly made up of OGNL expressions, so you can write OGNL expressions to execute.

Observe the variables of the expression

Variable variable indicates the input parameter of the params function, the return value of the returnObj function, the throwExp exception information, target the current object

View the input parameters and return values of a function

Watch com.ytao.service.UserServiceImpl getUser "{params,returnObj}"

Print information isEmpty=false;size=1 can see that the parameter is not empty, the number of parameters is one. View specific input parameter information

Watch com.ytao.service.UserServiceImpl getUser "{params [0], returnObj}"

View exception information

Watch com.ytao.service.UserServiceImpl getUser "throwExp"

When we pass in a parameter of-1, we print out the illegal parameter exception we defined.

In addition to observing expressions, watch can also use conditional expressions and observe event points. Note that when using the observation event point, some variables of the observation expression do not necessarily exist, such as when using-b, the return value and exception information are empty.

Sometimes we canvass a function and can't get the information about the function right away. The background asynchronous task provided by arthas can help us log. It is used in a manner similar to that of Linux.

Watch com.ytao.service.UserServiceImpl getUser "{params,returnObj}" > / log/w.log &

View logs saved asynchronously

Tt location exception call

The watch described above can check the call of a function, and it is more suitable to view the information after knowing that there may be a call at that time. If a function calls n times, there are several times to execute exceptions, we have to find these abnormal calls, it is not convenient to troubleshoot in watch. Using the tt command, you can easily view the call and information of the exception. Check the function of com.ytao.service.UserServiceImpl#getUser.-t is recorded every time the function is called.

Tt-t com.ytao.service.UserServiceImpl getUser

Record information

View all records

Tt-l

View the specified function record

Tt-s' method.name== "getUser"'

Output information description

The table field explains the INDEX time segment record number, each number represents a call, and many subsequent tt commands specify record operations based on this number, which is very important. The native time of the TIMESTAMP method execution records whether the native time COST (ms) method executes the time-consuming IS-RET method ends in the form of a normal return, whether the IS-EXP method ends the hashCode () of the OBJECT execution object in the form of throwing an exception, note that someone once mistakenly thought it was the memory address of the object in the JVM, but unfortunately he is not. But it can help you simply mark the name of the class entity that currently executes the method, the class name executed by CLASS, the name of the method executed by METHOD.

From the above parameters, we can see that the 1003 call ends with an exception. Because tt records the information of each call, we can view the details of 1003.

Tt-I 1003

Trace View call Link

We often encounter that the rt is too long when calling an api, so we need to find one or more functions on the calling chain to optimize. We usually locate several possible anchor points and print the rt between the various anchor points. Or from the log to find out the time point of log printing to calculate the time difference, no matter which method is more cumbersome. When using the trace command of arthas, we can easily complete our requirements. Trace parameter description

Parameter parameter description class-pattern class name expression matching method-pattern method name expression matching condition-express conditional expression [E] turn on regular expression matching, default is wildcard matching [n:] command execution times # cost method execution time

Use trace to output information about com.ytao.controller.UserController#getUser

Trace com.ytao.service.UserServiceImpl getUser

Output result

In the process of troubleshooting in actual use, in order to reduce the output of useless information, we generally use # cost to filter functions that take a short time and come with jdk, which can be ignored to reduce the output of information. For example: filter out calls less than 1ms

Trace com.ytao.service.UserServiceImpl getUser'# cost > 1'redefine for hot deployment

When we found out bug and wanted to go online quickly to save lives, Arthas prepared the redefine command for us to implement hot updates. Although jad/mc/redefine hot one-stop is now advocated, online code is recommended to compile locally and then replace it to avoid manual misoperation. First, add a line of code to UserServiceImpl.

Get the classLoaderHash and get the information of the class through the sc command

Sc-d * UserServiceImpl

Classes that perform redefine modifications

Redefine-c 1d56ce6a / usr/local/jar/UserServiceImpl.class

Verify that the UserServiceImpl class is updated with the printed information

The use of Arthas, in addition to the above, there are some other diagnostic features, this is only my personal use of the method. However, the use of this kind of tools must have a set of combined punches, in the process of troubleshooting problems, there are corresponding means of troubleshooting, not blind troubleshooting.

This is the end of the content of "Java's method of troubleshooting the production environment with Arthas". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report