Java Remote Debug (idea remote debugging)

Overview

I don't know if you have any good methods for debugging distributed systems. For me, before I know the method of remote debugging, it is to type various logs in the code, and then redeploy, go online, and debug, which is time-consuming. Today, let's learn about the awesome function of Java remote debugging. This article uses Intellij IDEA as an example to explain how to use remote debugging. Take the program used in the Thrift Getting Started Tutorial article as an example. This program consists of a Thrift server and a client. Describe the problem that remote debugging needs to solve:

The server program runs on a remote server. We can set breakpoints in the code of the local server (provided that the local code must be the same as the code running on the remote server), and can know locally whenever a request is made to the remote server. The internal state of the remote server at this time.

The following steps describe how to debug remotely.

Run server-side code with specific JVM arguments

To allow the code running on the remote server to support remote debugging, specific JVM parameters must be added at startup. These parameters are:

-Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=${debug_port}

The ${debug_port} is user-defined and is the debug port. In this example, port 5555 is used as an example.

 

Configuration steps

1. Local idea configuration

Open Intellij IDEA, select "Edit Configurations..." at the top right, click the + sign after entering, select "Remote", just fill in the content in the red box according to the figure below, where host is the ip of the machine where the remote code is running /hostname, port is the debug_port specified in the previous step, in this case 5555

 

Then click Apply, and finally click OK

Note: where host is the remote server address Port is a custom port

 Preserve JVM arguments (used during server configuration)

or

 

Start debug mode

Now select the name of the remote created in the previous step in the drop-down box of "Edit Configurations..." in the previous step, then click the debug button on the right (the one that looks like a bug), and look at the console log, if something like "Connected to the target VM, address: 'xx.xx.xx.xx:5555', transport: 'socket'" means the connection is successful.

Set breakpoints and start debugging

The remote debug mode has been enabled, and you can now breakpoints in the code that needs to be debugged, such as:

As shown in the figure, if there is a √ in the breakpoint, it means that the selected breakpoint is correct.

Now send a request to the remote server locally, look at the bug interface of the local console, scroll to the debugger label, and you can see that the current internal state (various variables) of the remote service has all been displayed, and just set the break The variable value of the row is also displayed where the dot is placed.

Java Remote Debug (idea remote debugging)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324613161&siteId=291194637