当前位置:网站首页>You still can't remotely debug idea? Come and have a look at my article. It's easy to use

You still can't remotely debug idea? Come and have a look at my article. It's easy to use

2022-06-13 05:52:00 cx7

1、 long-range DEBUG The necessity of
        Due to the difference of deployment environment , I believe many friends have encountered that the functions normally tested in the development environment appear in the test environment or even the production environment bug The situation of . In general , The means that can be taken by the production environment are relatively single , That is, get the running environment context by logging , Analyze the log file and try to reproduce bug. This will bring many problems , First , Log analysis is a time-consuming work ; secondly , Existing logging does not necessarily reflect the problem , You may need to repeat this process many times ( Analysis log -> Guess the problem -> Add log -> Deploy -> Get log ) To slowly approach the problem . If it's a test environment , We also have an alternative —— Remote debugging —— Put the program in the test environment as debug mode , Use on this machine IDEA Set breakpoints in the project for debugging .
2、IDEA structure SpringBoot Test project
Here I used the previous test demo, Dependency basically does not introduce anything , Below redis Dependency was introduced by myself , You don't need to introduce .

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cxk</groupId>
    <artifactId>redis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
  
</project>

yaml perhaps properties Just add a port number to the file

	server.port=8999

And then just make an interface :

package com.cxk.controller.main;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    

    @RequestMapping("/test2")
    public String execute2() {
    
        String s1 = 1+"";
        String s2 = 2+"";
        String s3 = s2+s1;
        return  " --- " + s3;
    }
}

Then let's start postman Test it
 Insert picture description here
3、 test Demo The project configuration supports remote debugging
pom.xml Internal configuration jvmArguments Parameters -Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n </jvmArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

With this configuration , The packaged project publisher , Can support remote DEBUG;

Detailed description of specific parameters :

-Xdebug notice JVM Working in DEBUG In mode ;
-Xrunjdwp notice JVM Use (Java debug wire protocol) Operation and debugging environment . This parameter also contains a series of debugging options ;
transport The transmission mode of debugging data is specified ,
dt_socket Refers to using SOCKET Pattern , have other dt_shmem Using shared memory , among ,dt_shmem Only applicable to Windows platform ;
address The port number of the debugging server , The port number used by the client to connect to the server ;
server=y/nVM Does it need to be executed as a debugging server ;
suspend=y/n Whether to start after debugging the client to establish a connection VM;
4、IDEA pack jar
In this step, everyone may , Here I'm going to say a few words
If you don't know how to pack, look carefully , Point at the top right Maven,
 Insert picture description here
Then execute in the following order , I didn't do it here test Options , If necessary, you can execute this option .
 Insert picture description here
After the execution , Will be in target Create one in the directory jar package
 Insert picture description here
Then copy this to the desktop to run , Here execute the following command .
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar redis-1.0-SNAPSHOT.jar
The second line will indicate that you are listening 5005 port .
 Insert picture description here
Then the next step is to make a breakpoint where the breakpoint is needed ,
 Insert picture description here
Then we configure the remote DEBUG To configure .
 Insert picture description here
Click on Edit Configuration A page will pop up , Then we click on the... In the upper left corner + Number , find Remote Options 、 Configure as follows . there 5005 It is the listening port number mentioned above . Then later apply. Then click on the bug in the upper right corner .
 Insert picture description here
 Insert picture description here
Click on the run DEBUG after , The pop-up window indicates that the connection has been made .
 Insert picture description here
Then let's go Postman Test it .
 Insert picture description here
Pay attention : The path address of the request here is you jar The address of the package deployment and the port number configured in the project . Then the breakpoint enters . Be accomplished .
 Insert picture description here

原网站

版权声明
本文为[cx7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280506341043.html