使用IDEA對SpringBoot應用進行遠程調試方式
情境描述
有時候,應用開發(fā)完成發(fā)布到服務器時出現異常,但本地卻無法復現,也無法通過服務器上的日志定位。
此時可以通過IDEA對部署在服務器上的SpringBoot應用進行遠程調試。
環(huán)境描述
- 遠程服務器: CentOS 7
- 語言環(huán)境:JDK 1.8
- SpringBoot版本:2.3.4.RELEASE
- IDEA版本:2020.2
操作步驟
測試應用編寫與發(fā)布
本地使用IDEA寫一個簡單的SpringBoot web應用,開放一個接口,代碼如下:
package com.example.remoteapp.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author UV
* @Version 0.1
* @Data 2020/9/29
* @Description
*/
@RestController
public class OpenController {
@GetMapping("/hello")
public void hello() {
System.out.println("此處是斷點");
System.out.println("此處是正常業(yè)務內容");
}
}使用mvn install將應用打成jar包,上傳到服務器上。
IDEA遠程調試配置
打開項目配置

添加遠程應用

設置遠程應用名、debugger模式(Debugger mode)、配置遠程服務器地址(Host)、端口(Port)、應用模塊(Use module classpath)

此處的Port為遠程應用的遠程調試端口,不是應用本身的業(yè)務訪問端口。
配置完成后,復制下圖中的參數,作為遠程應用的啟動參數

啟動遠程應用
登陸服務器,啟動jar包
$ java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar remoteapp-0.0.1-SNAPSHOT.jar
Listening for transport dt_socket at address: 5005. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.4.RELEASE)2020-09-29 02:20:47.247 INFO 798 --- [ main] c.e.remoteapp.RemoteappApplication : Starting RemoteappApplication v0.0.1-SNAPSHOT on migration-plat01.novalocal with PID 798 (/root/remoteapp-0.0.1-SNAPSHOT.jar started by root in /root)
2020-09-29 02:20:47.252 INFO 798 --- [ main] c.e.remoteapp.RemoteappApplication : No active profile set, falling back to default profiles: default
2020-09-29 02:20:49.000 INFO 798 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-09-29 02:20:49.024 INFO 798 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-09-29 02:20:49.024 INFO 798 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-09-29 02:20:49.120 INFO 798 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-09-29 02:20:49.121 INFO 798 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1773 ms
2020-09-29 02:20:49.428 INFO 798 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-29 02:20:49.697 INFO 798 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-09-29 02:20:49.712 INFO 798 --- [ main] c.e.remoteapp.RemoteappApplication : Started RemoteappApplication in 3.297 seconds (JVM running for 3.953)
2020-09-29 02:21:54.307 INFO 798 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-09-29 02:21:54.307 INFO 798 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-09-29 02:21:54.318 INFO 798 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 11 ms
此處是斷點
此處是正常業(yè)務內容
Listening for transport dt_socket at address: 5005
此時可以在日志第一行看到Listening for transport dt_socket at address: 5005證明遠程調試接口配置成功。
本地使用IDEA對遠程應用進行調試
在IDEA中打一個斷點,開啟debug

訪問遠程測試接口

注意點
- 遠程調試端口一定要跟應用的業(yè)務端口不一致,否則會因為端口沖突導致應用無法啟動
- 本地代碼要與遠程代碼一致,否則調試過程會出現異常
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java Swing JToggleButton開關按鈕的實現
這篇文章主要介紹了Java Swing JToggleButton開關按鈕的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12

