利用IDEA远程调试代码

    在工作经常遇到连接开发数据库,程序没有问题,但一到线上或者测试环境运行就出现各种问题,现在我们来配置远程debug。

一、原理

本机和远程主机的两个 VM 之间使用 Debug 协议通过 Socket 通信,传递调试指令和调试信息。 
被调试程序的远程虚拟机:作为 Debug 服务端,监听 Debug 调试指令。jdwp是Java Debug Wire Protocol的缩写。 
调试程序的本地虚拟机:IDEA 中配置的 Remote Server,指定 Debug 服务器的Host:Port,以供 Debug 客户端程序连接。

二、远程调试步骤

1、利用spring-boot创建测试项目ares-boot,并编写测试Controller

package com.ares.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("")
    public String hello(String name) {
        return "hello " + name;
    }

}

打成jar包,利用java -jar 启动服务

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:10090 -jar ares-springboot-1.0.0.jar --server.port=9090
其中 -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:10090 指定开放调试的端口

2、IDEA配置启动环境(Debug服务器)

新建remote

配置要调试的服务地址和端口

利用debug启动,浏览器访问http://localhost:9090/hello,我们发现Idea已经进入的debug模式

注意:

1)有时候调试时提示Connect time out,这时候需要关闭防火墙

2)只有本地和远程的代码保持一致才能进行远程调试

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章