Recently, I checked an online question for a new school recruit. The question itself was not too difficult, but I stumbled on an Arthas pit in the process, which was quite interesting.

Some useful tools, including tcpdump, arthas, simpleHTTPServer, etc., are also shared.

1. Problem description

A newly developed function, in a nutshell, is to read the data from the database displayed in the foreground.

Local start service debugging, call API with Postman, return data display normal, the Chinese in the data is normal.

However, after being deployed in an online environment, non-Chinese data is normal, but Chinese characters are displayed as garbled characters through the call interface of Chrome and Postman.

2. Check your thinking

The first response to this problem is that there is something wrong with the content-Type of the request.

The content-type of the request and response is application/json. Charset =UTF-8, no problem.

Then I googled a bunch of garbled issues, which basically refer to Spring’s HttpMessageConverter problem or content-type.

We’ll just have to dig deep.

The main idea of the investigation is to determine which step is generated by the garbled code.

  • One is to find the place in the database, we need to capture a package to confirm, but our local service call is correct, so there should be no problem in this step.

  • One is where the application service returns, which needs to be confirmed by capturing a packet. In an online deployment environment, packets are captured when application services return data using tcpdump.

  • One is that there is data transformation in the code logic, and this requires arthas to look at runtime data for online applications.

3. Use tcpdump to capture packets and view the response of the server

3.1 What is tcpdump?

Tcpdump is a tool for analyzing network packet capture in Linux. Tcpdump is frequently used in daily Network management on Linux. Being familiar with tcpdump helps improve work efficiency.

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p3-tt.byteimg.com/origin/pgc-image/7447ff3227c04f59a44a4eb45382c024?from=pc)

3.2 Packet Capture

To obtain service request packets, log in to the corresponding server (or THE POD of K8S) and use tcpdump to capture them.

As a warm man, I will write down to you step by step from installation to use 🙂

1) Installation tools

If tcpdump has not been installed on your server, run the following command to install it

yum -y install net-tools
Copy the code

2) Check the network status

If the service has multiple network adapters, run the following command to view the network adapters

Netstat -i
Copy the code
! [the problem come from line, accidentally stepped on ali arthas pit] (https://p6-tt.byteimg.com/origin/pgc-image/68deb21e8adf46e3928d0af3208712e1?from=pc)

3) Deploy packet capture

tcpdump -i eth0 tcp -w xxx.cap
Copy the code
  • En0 indicates the listening network adapter
  • TCP Indicates the packet type
  • -w Specifies the output file name

There are a lot of other options that you can filter, but you can look them up on the Internet and I won’t go through them here.

4) Invoke the request

After tcpdump is deployed, API requests are made to the server. In this case, all related TCP packets are output to the xxx.cap file.

3.3 Packet Parsing

1) Send the xxx.cap file locally

Generally, you can use the SCP command to send it directly

scp xxxx.cap [email protected]:/path
Copy the code

If SCP is inconvenient to use when transferring server files to local, such as some firewall restrictions.

You can also use Python to start a Web service on the server (port is customizable).

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p1-tt.byteimg.com/origin/pgc-image/191e327edbab4aba8af149954375c48b?from=pc)
python -m SimpleHTTPServer 18888 &
Copy the code

Then download the file locally using wget.

2) Parse the CAP file

After the CAP file is obtained locally, you can use the Wireshark to parse the cap file. The following information is displayed:

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p1-tt.byteimg.com/origin/pgc-image/b005cedb2adb472e99f6b380383b187b?from=pc)
! [the problem come from line, accidentally stepped on ali arthas pit] (https://p3-tt.byteimg.com/origin/pgc-image/e7135375dec94e6e885114bec8a39c48?from=pc)
! [insert picture description here] (https://img-blog.csdnimg.cn/20201102163516692.png?x-oss-process=image/watermark, type_ZmFuZ3poZW5naGVpdGk, sha dow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ3MzA2NTg=,size_16,color_FFFFFF,t_70#pic_center)

After the API packet is parsed, the returned Chinese characters are garbled. It is confirmed that the response content sent by the server is garbled.

So, you just have to continue to troubleshoot the application itself.

4. Arthas check for running code online

Arthas is Alibaba, open source Java diagnostic tool, when you meet the following similar problems and helpless, can try to use Arthas (the use of the official documents in more detail: arthas.aliyun.com/doc/quick-s…

  • From which JAR is this class loaded? Why are all kinds of class-related exceptions reported?
  • Problems can not be debug online, and do not want to frequently log and re-publish
  • There is a problem with a user’s data processing online, but it cannot be debugged online, and it cannot be reproduced offline!

4.1 Quick Installation and Startup

curl -O https://arthas.aliyun.com/arthas-boot.jar

java -jar arthas-boot.jar
Copy the code

4.2 Running the code Returns a Troubleshooting

The screen, use the function of arthas’s watch (the use of the official documents in more detail: arthas.aliyun.com/doc/watch.h…

Let’s take a look at the response of the controller layer of the online running application to the request. Without adding logs and redeploying, we can immediately see the result of the online code.

watch xxx.xxx.controller method "{params,returnObj}" -x 2
Copy the code

Then an API call is made and arthas displays the following result:

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p1-tt.byteimg.com/origin/pgc-image/e53f375875a04b70a5df35bcb82a65d5?from=pc)

As you can see, the controller method returns something that is garbled.

Therefore, there is a conversion problem in the code logic.

5. Locate the fault

From the business logic, you can guess that there is a problem converting byte[] from the business to string.

New string() does not specify a character set:

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p1-tt.byteimg.com/origin/pgc-image/ce22759873724cf8b68f6f281e0e28da?from=pc)

Therefore, the system variable file.encoding is read as the character set by default during conversion.

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p6-tt.byteimg.com/origin/pgc-image/80ecbe6bb1554867a6b94616fe91c0b8?from=pc)
! [the problem come from line, accidentally stepped on ali arthas pit] (https://p3-tt.byteimg.com/origin/pgc-image/8acacf5aea3b42e4b9e36557b8cc105f?from=pc)
! [the problem come from line, accidentally stepped on ali arthas pit] (https://p6-tt.byteimg.com/origin/pgc-image/b2d352a654fd4a7e851399ce346bf9a5?from=pc)

Then we use arthas to look directly at the system variable, and sure enough, it’s not UTF8.

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p3-tt.byteimg.com/origin/pgc-image/de2b3b6caba849b5abf58d6e3f7af17d?from=pc)

So, there are two solutions.

The first is to specify the character set **** when new String (bytes).

The second is to set the system variable file.encoding= UTF-8.

Step further

We initially chose to fix the code by specifying the character set when converting in code.

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p1-tt.byteimg.com/origin/pgc-image/3b7edac0428e41bab0342633dd9a237f?from=pc)

After republishing, I looked at arthas and found that it was still garbled? !!!!!

Then I went back and looked at the code for a long time. I couldn’t find the reason.

All of a sudden, I look at the offline page, and it’s all right. Nani? Is arthas the problem?

Then I googled arthas and found that a lot of people had problems with arthas displaying Chinese garble…

The solution is to start arthas and specify the character set as well.

java -jar -Dfile.encoding=UTF-8 arthas-boot.jar

And then the problem was solved… Hehe…

At this point arthas’s results were normal.

What does that tell us? !!!!!!!!!!

Arthas output interface must not specify character set for string conversion…

One foot stepped in a chain hole…

! [the problem come from line, accidentally stepped on ali arthas pit] (https://p1-tt.byteimg.com/origin/pgc-image/d74e0ba922ff44e880f92059d05d183b?from=pc)

7. To summarize

In fact, the whole issue is rather superficial, but arthas’s garbled Chinese characters at the end are a bit painful…

Tcpdump, arthas, simpleHTTPServer, etc.

See the end, the original is not easy, point a concern, point a like it ~

Reorganize the knowledge fragments and construct the Java knowledge graph: github.com/saigu/JavaK… (Easy access to historical articles)