点击上方 匠心零度 ,选择“ 设为星标

做积极的人,而不是积极废人

这一篇介绍如何使用 Intellij IDEA 实现远程 debug。

项目中经常会有出现这样的问题,会令程序员抓狂:关键代码段没有打印日志,本地环境正常生产环境却又问题… 这时候,远程 debug 可能会启动作用。

1 准备用于 debug 的代码

准备一个 RestController 用于接收请求,最后可以通过本地断点验证是否成功开启了远程 debug

@RestController
public class TestController {

@RequestMapping("/test")
public Integer test() {
int i = 0;
i++;
i++;
i++;
i++;
i++;
return i;
}

}

项目使用 springboot 和 maven 构建,依赖就省略了,使用 springboot 提供的 maven 打包插件,方便我们打包成可运行的 jar。

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

2 使用 maven 插件打包成 jar

maven 插件

maven 插件

3 准备启动脚本

java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=64057 remote-debug-1.0-SNAPSHOT.jar
-
transport=dt_socket,server=y,suspend=n,address=64057

4 配置 IDEA

IDEA 配置

IDEA 配置

  1. 与脚本中的指令完全一致

  2. 远程 jar 包运行的 host,由于我的 jar 运行在本地,所以使用的是 localhost,一般线上环境自然是修改为线上的地址

  3. 与远程 jar 包进行交互的端口号,idea 会根据指令自动帮我们输入

  4. 选择与远程 jar 包一致的本地代码

请务必保证远程 jar 包的代码与本地代码一致!!!

5 验证

保存第 4 步的配置后,先执行脚本让远程的 jar 包跑起来,再在 IDEA 中运行 remote-debug

运行 remote-jar

运行 remote-jar

如上便代表连接运行成功了

在本地打上断点,访问 localhost:8080/test

远程 debug 信息展示

远程 debug 信息展示

可以在本地看到堆栈信息,大功告成。一行指令便完成了远程调试。

END

如果读完觉得有收获的话,欢迎点【好看】,关注【匠心零度】,查阅更多精彩历史!!!

让我“ 好看 ” 

相关文章