Use IDEA remote Debug debugging, so practical!

Click to follow the official account, Java dry goods will be delivered in timeabc26f1fc593107565cfec3b716c347e.png

fd94531cb8e28838d2c94fdff950bc46.png There is no one of the strongest microservice frameworks in China!

776165a6faced74a956bbedbda1da816.png Covers almost all Spring Boot operations!

49944e51258d6323e13b1af4231dcc38.png 2 023 new Java interview questions (2500+)


Dachang Java development internal recommendation, quick resume ~

background

Sometimes we need to perform remote debugging. This article studies how to perform remote debugging and the details of the process of using IDEA remote debugging. Read it to clear up some of your doubts.

configuration

Remote debug service, take springboot microservice as an example (springcloud should be similar, I haven't studied it). First of all, specific parameters need to be added to start springboot.

Recommend an open source and free Spring Boot practical project:

https://github.com/javastacks/spring-boot-best-practice

1. IDEA settings

The settings of the IDEA of the higher and lower versions may have a slightly different interface. I use 2020.1.1. Roughly the same, explore by yourself.

IDEA opens the springboot application corresponding to the remote start

1. Select Edit Configuration

ffd4db30662543e9f385146ed834ff7a.png

2. As shown in the figure, click the plus sign and select Remote

ab5c22ffb5d7499c790dd30831086b07.png

3. Configuration, the detailed steps are shown in the figure

9ae4e4feef8dc1d8f0e972aacb97cc92.png

Note: Be careful that the port is not occupied. The subsequent port is used to communicate with the remote java process.

It can be noticed that the generated scripts are different when switching different jdk versions

Select jdk1.4, then it is

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=50055

This is why you search other blogs, there will be such a configuration, in fact, this configuration is also feasible. But more accurately, it should follow the configuration of jdk5-8 below

Select jdk 5-8, then it is

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=50055

Select jdk9 or above, then it is

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:50055

It is said that because jdk9 has become safe, remote debugging is only allowed locally. If you want to be remote, you need to configure it before the port *.

In addition, pay attention to the Java technology stack of the official account, reply to the keyword in the background: IDEA, and read a large number of IDEA tutorials I wrote.

2. Startup script modification

Just use the obtained in the first step Command line arguments for remote JVM, that is-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=50055

The modified startup script is as follows

nohup java \
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=50055 \
-jar remote-debug-0.0.1-SNAPSHOT.jar &

Note that ^ is used for line breaks in windows, for example

java ^
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=50055 ^
-jar remote-debug-0.0.1-SNAPSHOT.jar

illustrate:

1. The port can be set at will, even if it is not occupied, but it must be consistent with the port set in remote in IDEA! Copy other parameters. For detailed parameter explanations, please refer to the appendix or search by yourself

2. remote-debug-0.0.1-SNAPSHOT.jarChange to your own jar package name

3. The script I gave runs in the background. If you don’t need to run in the background, remove nohupand&

3. Start springboot, start IDEA in

385929c42d5c54a0317663e80f68ac8d.png

Details of IDEA remote debugging

1. Detail 1: stop at a local breakpoint, will it continue to execute after closing the program?

If the remote debugging stops at its own breakpoint, and the project in IDEA is closed to stop running, will it continue to run and execute the rest of the logic? Yes, it's a little harder to remember.

Taking the following code as an example, it stops at the first line. Then it stopped in IDEA, and found that the console still printed the remaining logs after the stop.

7938b4a192c2bf293355e0ef44bd923d.png

2. Detail 2: What happens if the jar package code is inconsistent with the local one?

What happens if the code in IDEA is not consistent with the jar package.

Conclusion: Ensure that the code is consistent with the remote start.

Otherwise, the number of lines will not match when you debug. Reporting an error and throwing an exception will not. It can still match the number of lines like this

For example, if you debug the test1 method, the test2 method is under test1, and adding code in test2 will not affect the line number in test1, which can accurately reflect the line number during debugging.

3. Detail 3: Where is the log printed?

The log will not be printed on IDEA's console. That is System.out, and log.infoare still printed remotely.

@GetMapping("/test1")
public String test1() {
    System.out.println("第一行");
    System.out.println("第二行");
    log.info("log 第一行");
    log.info("log 第二行");
    return "ok";
}

4. Detail 4: Will others get stuck during debugging?

During remote debugging, if a breakpoint is hit, will the page request be stuck after stopping?

For example, if you use remote debugging, other QAs are testing this page, what is the result they see? Will it get stuck? Yes, this has actually happened to me.

5. Detail 5: When the local code fixes the bug and calls remotely

If you find a bug in the remote debugging process, restart the project in IDEA after the local modification, and call it again on the page, can it be fixed? No, the code in the remotely deployed jar is still running

This directly shatters the dream of remote pages triggering local code for debugging. It would be much easier to debug code if possible.

In addition, if you are preparing for an interview to change jobs in the near future, it is recommended to brush up questions online in the Java interview library applet, covering 2000+ Java interview questions, covering almost all mainstream technical interview questions.

6. Details 6: This is not a problem of remote debugging, but a problem of dropframe, so let’s talk about it here

Regarding drop framethe question, if drop frameyou re-debugging, will you insert 2 records?

As shown in the figure userMapper.insert(eo), this method is not @Transactionalmodified. After the mapper method is executed, the transaction will be submitted immediately, and a row of records will be added in the library table. If it is drop framelater, debug again and execute the code again, so another record will be inserted.

e94b6cf55cd100bbf2622377e116a251.png

If you add , @Transationalthere will be no two records, and the transaction is not submitted when the dropframe is executed, and the insertion code will not be inserted again.

About what is drop frame
9969739aaffb6865f819942190b9f5f6.png

7. Detail 7: Same as above, it is a dropframe problem

If the above logic of inserting into the database is replaced by calling a remote interface, and the same code is executed again after the dropframe, will the remote interface be executed twice? Will do.

Summarize

It seems that the usefulness of remote debugging is not so great, and it cannot be used as a long-term debugging tool. It can only be used as a means of temporary debugging.

There are several difficulties:

  • It is difficult to ensure that the local code is consistent with the remote, and it is also difficult for you to judge whether it is consistent

  • The bug is found through remote debugging, but it cannot be repaired immediately and then continue debugging. It can only be repaired and then deployed to continue remote debugging.

Copyright statement: This article is an original article by CSDN blogger "Stone Wang", which follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement. Original link: https://blog.csdn.net/w8y56f/article/details/116493681


If you want to learn Spring Boot systematically, I recommend my " Spring Boot Core Technology Course ", based on the latest 3.x version, covering almost all core knowledge points of Spring Boot , one-time payment, permanent learning...

Those who are interested scan the code to contact and subscribe to learn:

ef93b80758f2baaf7793ced5c0a536fd.png

If you want to try/preview, add Brother R on WeChat

b5f67ea429827562d7352fa6c8c959b5.jpeg

Add WeChat, please note: 399

Guess you like

Origin blog.csdn.net/youanyyou/article/details/132033250