springboot使用IDEA远程Debug
作者:燕少༒江湖 发布时间:2021-12-13 06:40:35
项目上线之后,如果日志打印的很模糊或者业务逻辑比较复杂,有时候无法定位具体的错误原因,因此可以通过IDEA远程代理进行Debug。
线上的代码一定要和本地的一致!
环境:
2.1.4.RELEASE(org.springframework.boot)
jdk1.8
Apache Maven 3.5.0
1、先创建一个准备远程调试的Demo,注意构建项目的配置
<?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.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.remote.test</groupId>
<artifactId>remote_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>remote_test</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>
<configuration>
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-${project.version}-all</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!--根据项目的全名指定启动类-->
<mainClass>com.remote.test.remote_test.RemoteTestApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.remote.test.remote_test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("remote/test")
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@PostMapping("selectByUserId")
public String selectUserInfo(@RequestParam("userId") String userId) {
try {
Map<String,Object> userInfo = new HashMap<>();
userInfo.put("userId",userId);
userInfo.put("age",23);
userInfo.put("name","yanshao");
userInfo.put("address","shanghai");
logger.info("Query user information by user ID. userInfo: {}",userInfo.toString());
return this.success(userInfo);
} catch (Exception e) {
logger.error("Query user information by user ID. userId:{} ", userId, e);
return this.fail();
}
}
private String success(Object data){
Map<String,Object> res = new HashMap<>();
res.put("code",0);
res.put("desc","success");
res.put("data",data);
return res.toString();
}
private String fail(){
Map<String,Object> res = new HashMap<>();
res.put("code",1);
res.put("desc","fail");
return res.toString();
}
}
2、打包
输入:mvn clean package
,(大概需要等几分钟),最好在构建之前指定本地repository,就不需要重新下载jar包了。
3、在IDEA配置远程Debug
指定socket port = 8081,指定准备debug的模块
4、在终端启动刚才打好的jar包
a. 先在IDEA启动debug
b. 然后在终端输入命令:java -agentlib:jdwp=transport=dt_socket,server=n,address=localhost:8081 -jar remote_test-0.0.1-SNAPSHOT-all.jar
5、测试
在准备请求的接口上标记断点
注意:必须先在IDEA启动Debug,然后再启动项目
➜ Desktop java -agentlib:jdwp=transport=dt_socket,server=n,address=localhost:8081 -jar remote_test-0.0.1-SNAPSHOT-all.jar
ERROR: transport error 202: connect failed: Connection refused
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [debugInit.c:750]
来源:https://blog.csdn.net/qq_31289187/article/details/112667136


猜你喜欢
- 数据访问之Druid数据源的使用说明:该数据源Druid,使用自定义方式实现,后面文章使用start启动器实现,学习思路为主。为什么要使用数
- import java.io.ByteArrayInputStream; import java.io.FileOutputSt
- 首先在命令行创建一个PhoneGap工程phonegap create . "jspdf.sample" "J
- java 遍历listpackage com.tiandy.core.rest;import java.util.ArrayList;imp
- 我们在构建C# Form窗口的时候经常需要到弹出新的窗口,那么接着就会如何弹出窗口的疑问。这里介绍最常见的两种弹窗方法show()和show
- 在Java中我们知道静态变量会在类加载时机的“初始化”阶段得到赋值(编译器会收集类中的静态变量及静态
- ofType和javaType的区别JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型
- 本文实例为大家分享了Android实现单选按钮的具体代码,供大家参考,具体内容如下单选按钮在默认情况下,单选按钮显示为一个圆形图标,可以在图
- 有段时间没有看视频了,昨天晚上抽了点空时间,又看了下鸿洋大神的视频教程,又抽时间写了个学习记录。代码和老师讲的基本一样,网上也有很多相同的博
- 动态方法就是一个Action对应多个请求,减少Action的数量1、指定method属性<action name="addA
- 综述Android中的事件分发机制也就是View与ViewGroup的对事件的分发与处理。在ViewGroup的内部包含了许多View,而V
- 今天碰到一个非常奇怪的问题: 在Android中ImageView无法显示加载的本地SDCard图片。 具体过程是:先调用本地照相机程序摄像
- 本文实例讲述了Java设计模式之抽象工厂模式。分享给大家供大家参考,具体如下:具体工厂类:生产创建某一类具体产品对象。抽象产品类可以使用接口
- 近期公司要做报表功能,在网上搜索下表格的样式后便自己写了一个自定义的表格控件,该表格控件能根据设置的数据中数据的最大值自动设置左侧信息栏显示
- 数组:是一组相关变量的集合数组是一组相关数据的集合,一个数组实际上就是一连串的变量,数组按照使用可以分为一维数组、二维数组、多维数组数据的有
- bootstrap.yml和bootstrap.properties优先级直接先说结论 bootstrap.properties 优于boo
- 本文实例讲述了java取两个字符串的最大交集的实现方法,分享给大家供大家参考。具体实现方法如下:package com.itheima.ne
- 在进行一些小型APP的开发,或者是对拍照界面没有自定义要求时,我们可以用调起系统相机的方式快速完成拍照需求和不需读写权限进行读写操作的方案一
- 实践过程pdf转excelpublic static long pdfToExcel(String inFile, String outFi
- Activity是Android组件中最基本也是最为常见用的四大组件之一,在 android开发中 ,运用极为广泛,作为初学者需要熟练掌握,