IDEA远程DEBUG
1、IDEA构建SpringBoot测试Demo
新建SpringBoot测试项目remote-debug,只需要web依赖支持即可;版本选用2.2.6.RELEASE(注意,别用2.5.1版本,有坑,亲测)
1.1、pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>remote-debug</artifactId>
<version>v1.0</version>
<name>remote-debug</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2、application.yml
server:
port: 8087
servlet:
context-path: /
tomcat:
uri-encoding: utf-8
1.3、新增Controller,提供接口测试
package com.java1234.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@PostMapping("/test")
public String test(Integer id, String name) {
System.out.println("id=" + id);
System.out.println("name=" + name);
if (id > 0) {
return "success " + name;
} else {
return "fail";
}
}
}
1.4、启动项目使用postman测试
2、测试Demo项目配置支持远程调试
2.1、pom.xml里配置jvmArguments参数
<configuration>
<jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n</jvmArguments>
</configuration>
整体效果
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
参数描述:
- -Xdebug 通知JVM工作在DEBUG模式下;
- -Xrunjdwp 通知JVM使用(Java debug wire protocol)运行调试环境。该参数同时包含了一系列的调试选项;
- transport 指定了调试数据的传送方式,dt_socket是指用SOCKET模式,另有dt_shmem指用共享内存方式,其中,dt_shmem只适用于Windows平台;
- address 调试服务器的端口号,客户端用来连接服务器的端口号;
- server=y/n VM 是否需要作为调试服务器执行;
- suspend=y/n 是否在调试客户端建立连接之后启动 VM;
3.IDEA打包JAR
4.启动jar并且带启动参数支持远程调试
4.1、把jar存储至随意目录
4.2、启动jar
4.3、启动命令
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar remote-debug-v1.0.jar