springboot使用IDEA远程Debug
项目上线之后,如果日志打印的很模糊或者业务逻辑比较复杂,有时候无法定位具体的错误原因,因此可以通过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]
到此这篇关于springboot使用idea远程debug的文章就介绍到这了,更多相关springboot idea远程debug内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: react 项目中引入图片的几种方式
推荐阅读
-
Tomcat使用IDEA远程Debug调试的讲解
-
从零开始使用IDEA创建SpringBoot项目(图文)
-
Intellij idea远程debug连接tomcat实现单步调试
-
从零开始使用IDEA创建SpringBoot项目(图文)
-
springboot系列之03-使用IDEA完成第一个示例程序
-
使用iDea创建一个Springboot项目
-
使用IDEA创建springboot依赖下载很慢,解决方法
-
使用IDEA开发springboot入门程序
-
idea使用spring Initializr 快速搭建springboot项目遇到的坑
-
[视频教程] 配置vscode的PHP自动补全提示与使用Xdebug进行远程调试debug