springboot项目使用IDEA远程Debug
程序员文章站
2022-03-10 18:38:08
项目上线之后,如果日志打印的很模糊或者业务逻辑比较复杂,有时候无法定位具体的错误原因,因此可以通过IDEA远程代理进行Debug。线上的代码一定要和本地的一致!环境:2.1.4.RELEASE(org.springframework.boot)jdk1.8Apache Maven 3.5.01、先创建一个准备远程调试的Demo,注意构建项目的配置
项目上线之后,如果日志打印的很模糊或者业务逻辑比较复杂,有时候无法定位具体的错误原因,因此可以通过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
上一篇: Redis学习笔记记录
下一篇: 第一节 常用的一些命令
推荐阅读
-
如何使用向日葵远程控制软件实现远程控制、管理、操作等一系列项目的图文教程
-
初次使用IDEA创建maven项目的教程
-
荐 使用IDEA搭建一个简单的JavaWeb图书管理项目(详细步骤指导、提供源码)
-
springboot系列之03-使用IDEA完成第一个示例程序
-
微项目:一步一步带你使用SpringBoot入门(二)
-
SpringBoot项目使用RedisTemplate遇到key值\xac\xed\x00\x05t\x00的坑
-
idea新建springboot项目的方法
-
解决Idea、WebStorm下使用Vue cli脚手架项目无法使用Webpack别名的问题
-
IDEA中使用Maven模板创建Maven WebApp项目并使用Tomact来运行项目
-
基于idea把springboot项目部署到docker