當(dāng)項目投產(chǎn)后,可能會遇到之前測試環(huán)境上沒有遇到的問題,而又無法快速定位問題原因,令人十分捉急。
如果開發(fā)人員電腦可以接入生產(chǎn)環(huán)境,則可以采用java -agentlib:jdwp命令、idea或eclipse進(jìn)行遠(yuǎn)程調(diào)試。
本文中使用IDEA進(jìn)行遠(yuǎn)程調(diào)試。
一、服務(wù)端啟動jar命令,增加java -agentlib:jdwp,如下:
需要保證-agentlib:jdwp命令在 -jar命令之前
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xmx512m -Xms256m -Dspring.config.location=application.yml -jar abc-1.0.0.jar
命令解釋
1、輸入java命令后,可以看到agentlib的介紹,表示加載本機(jī)的代理庫
-agentlib:<libname>[=<選項>]
加載本機(jī)代理庫 <libname>, 例如 -agentlib:hprof
另請參閱 -agentlib:jdwp=help 和 -agentlib:hprof=help
-agentpath:<pathname>[=<選項>]
按完整路徑名加載本機(jī)代理庫
-javaagent:<jarpath>[=<選項>]
加載 Java 編程語言代理, 請參閱 java.lang.instrument
2、繼續(xù)輸入:java -agentlib:jdwp=help。
JDWP 是 Java Debug Wire Protocol 的縮寫,它定義了調(diào)試器(debugger)和被調(diào)試的 Java 虛擬機(jī)(target vm)之間的通信協(xié)議。
當(dāng)使用java -agentlib:jdwp啟動jar時,表示也在會address對應(yīng)的端口上啟動一個tcp監(jiān)聽,等待客戶端(調(diào)試端,比如idea、eclipse)連接。
Java Debugger JDWP Agent Library
--------------------------------
(see http://java.sun.com/products/jpda for more information)
jdwp usage: java -agentlib:jdwp=[help]|[<option>=<value>, ...]
Option Name and Value Description Default
--------------------- ----------- -------
suspend=y|n wait on startup? y
transport=<name> transport spec none
address=<listen/attach address> transport spec ""
server=y|n listen for debugger? n
launch=<command line> run debugger on event none
onthrow=<exception name> debug on throw none
onuncaught=y|n debug on any uncaught? n
timeout=<timeout value> for listen/attach in milliseconds n
mutf8=y|n output modified utf-8 n
quiet=y|n control over terminal messages n
Obsolete Options
----------------
strict=y|n
stdalloc=y|n
Examples
--------
- Using sockets connect to a debugger at a specific address:
java -agentlib:jdwp=transport=dt_socket,address=localhost:8000 ...
- Using sockets listen for a debugger to attach:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y ...
Notes
-----
- A timeout value of 0 (the default) is no timeout.
Warnings
--------
- The older -Xrunjdwp interface can still be used, but will be removed in
a future release, for example:
java -Xdebug -Xrunjdwp:[help]|[<option>=<value>, ...]
二、idea配置

iShot2021-06-23 11.25.42.png
三、啟動調(diào)試
1、啟動jar
2、在idea中啟動配置好的Remote
3、在源碼中打斷點,就可以調(diào)試
另Arthas很強大,也可以使用Arthas進(jìn)行線上調(diào)試。
參考資料
1、JDWP:https://paper.seebug.org/933/
2、# Arthas
3、https://arthas.aliyun.com/doc/download.html