springboot使用IDEA遠(yuǎn)程Debug
項(xiàng)目上線之后,如果日志打印的很模糊或者業(yè)務(wù)邏輯比較復(fù)雜,有時(shí)候無(wú)法定位具體的錯(cuò)誤原因,因此可以通過(guò)IDEA遠(yuǎn)程代理進(jìn)行Debug。
線上的代碼一定要和本地的一致!
環(huán)境:
- 2.1.4.RELEASE(org.springframework.boot)
- jdk1.8
- Apache Maven 3.5.0
1、先創(chuàng)建一個(gè)準(zhǔn)備遠(yuǎn)程調(diào)試的Demo,注意構(gòu)建項(xiàng)目的配置
<?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">
<!--根據(jù)項(xiàng)目的全名指定啟動(dòng)類-->
<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,(大概需要等幾分鐘),最好在構(gòu)建之前指定本地repository,就不需要重新下載jar包了。


3、在IDEA配置遠(yuǎn)程Debug
指定socket port = 8081,指定準(zhǔn)備debug的模塊

4、在終端啟動(dòng)剛才打好的jar包
a. 先在IDEA啟動(dòng)debug

b. 然后在終端輸入命令:java -agentlib:jdwp=transport=dt_socket,server=n,address=localhost:8081 -jar remote_test-0.0.1-SNAPSHOT-all.jar

5、測(cè)試
在準(zhǔn)備請(qǐng)求的接口上標(biāo)記斷點(diǎn)



注意:必須先在IDEA啟動(dòng)Debug,然后再啟動(dòng)項(xiàng)目
➜ 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]
到此這篇關(guān)于springboot使用IDEA遠(yuǎn)程Debug的文章就介紹到這了,更多相關(guān)springboot IDEA遠(yuǎn)程Debug內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- idea遠(yuǎn)程debug調(diào)試部署在tomcat上項(xiàng)目
- Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦)
- idea springboot遠(yuǎn)程debug的操作方法
- IntelliJ IDEA遠(yuǎn)程Debug Linux的Java程序,找問(wèn)題不要只會(huì)看日志了(推薦)
- Tomcat使用IDEA遠(yuǎn)程Debug調(diào)試的講解
- Intellij idea遠(yuǎn)程debug連接tomcat實(shí)現(xiàn)單步調(diào)試
- idea遠(yuǎn)程Debug部署在服務(wù)器上的服務(wù)
相關(guān)文章
Java中List的contains()方法的使用小結(jié)
List?的?contains()?方法用于檢查列表中是否包含指定的元素,借助equals()方法進(jìn)行判斷,下面就來(lái)介紹Java中List的contains()方法的使用小結(jié),感興趣的可以了解一下2025-04-04
基于springMvc+hibernate的web application的構(gòu)建
下面小編就為大家?guī)?lái)一篇基于springMvc+hibernate的web application的構(gòu)建。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
詳解Huffman編碼算法之Java實(shí)現(xiàn)
Huffman編碼是一種編碼方式,常用于無(wú)損壓縮。本文只介紹用Java語(yǔ)言來(lái)實(shí)現(xiàn)該編碼方式的算法和數(shù)據(jù)結(jié)構(gòu)。有興趣的可以了解一下。2016-12-12
Mybatis-plus foreach拼接字符串查詢無(wú)數(shù)據(jù)返回問(wèn)題
這篇文章主要介紹了Mybatis-plus foreach拼接字符串查詢無(wú)數(shù)據(jù)返回問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
springboot啟動(dòng)類如何剔除掃描某個(gè)包
這篇文章主要介紹了springboot啟動(dòng)類如何剔除掃描某個(gè)包,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

