Java項(xiàng)目開啟遠(yuǎn)程調(diào)試的方法步驟(tomcat、springboot)
當(dāng)我們運(yùn)行一個(gè)項(xiàng)目的時(shí)候,一般都是在本地進(jìn)行debug。但是如果是一個(gè)分布式的微服務(wù),這時(shí)候我們選擇遠(yuǎn)程debug是我們開發(fā)的利器。
環(huán)境
apache-tomcat-8.5.16
Linux
如何啟用遠(yuǎn)程調(diào)試
tomcat開啟遠(yuǎn)程調(diào)試
方法
切換到你的tomcat的bin目錄/apache-tomcat-8.5.16/bin
下,執(zhí)行:
./catalina.sh jpda start
執(zhí)行上面的命令就可以開啟遠(yuǎn)程debug了,如果想配置一些信息,比如端口號(hào)什么的,請(qǐng)參考下面的說(shuō)明。
參數(shù)說(shuō)明
# JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start" # command is executed. The default is "dt_socket". # # JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start" # command is executed. The default is localhost:8000. # # JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start" # command is executed. Specifies whether JVM should suspend # execution immediately after startup. Default is "n". # # JPDA_OPTS (Optional) Java runtime options used when the "jpda start" # command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS, # and JPDA_SUSPEND are ignored. Thus, all required jpda # options MUST be specified. The default is: # # -agentlib:jdwp=transport=$JPDA_TRANSPORT, # address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND
操作說(shuō)明
所以如果想修改配置,則如下操作:
在catalina.sh中進(jìn)行配置:
JPDA_TRANSPORT=dt_socket JPDA_ADDRESS=5005 JPAD_SUSPEND=n
或者通過(guò)JPDA_OPTS進(jìn)行配置:
JPDA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'
springboot開啟遠(yuǎn)程調(diào)試
The run goal forks a process for the boot application. It is possible to specify jvm arguments to that forked process. The following configuration suspend the process until a debugger has joined on port 5005
<project> ... <build> ... <plugins> ... <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.1.12.RELEASE</version> <configuration> <jvmArguments> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 </jvmArguments> </configuration> ... </plugin> ... </plugins> ... </build> ... </project>
These arguments can be specified on the command line as well, make sure to wrap that properly, that is:
mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
jar 命令開啟遠(yuǎn)程調(diào)試
在執(zhí)行jar的時(shí)候,添加上參數(shù)。如下:
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar demo.jar
如果想深入了解Java調(diào)試,那么去看一下這個(gè)吧。深入Java調(diào)試體系
問(wèn)題
如果出現(xiàn)Connection refused
。
首先檢查一下端口8000的使用情況:
use:~/tomcat/logs # netstat -an|grep 8000 cp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN
可見當(dāng)前16808端口服務(wù)被綁定了回環(huán)地址,外部無(wú)法訪問(wèn)。即本地調(diào)試。
辦法:
修改catalina.sh中一個(gè)參數(shù)。
if [ -z "$JPDA_TRANSPORT" ]; then JPDA_TRANSPORT="dt_socket" fi if [ -z "$JPDA_ADDRESS" ]; then JPDA_ADDRESS="0.0.0.0:8000" fi if [ -z "$JPDA_SUSPEND" ]; then JPDA_SUSPEND="n" fi
對(duì)JPDA_ADDRESS="localhost:8000"
把默認(rèn)值(localhost:8000)改成0.0.0.0:8000。默認(rèn)是本地ip調(diào)試也就是無(wú)法遠(yuǎn)程調(diào)試,0.0.0.0表示所有ip地址都能調(diào)試。
遠(yuǎn)程連上后再看端口情況:
root@VM-198-217-ubuntu:/opt/apache-tomcat-8.5.16/bin# netstat -an | grep 8000 tcp 0 0 10.133.198.217:8000 60.177.99.27:49998 ESTABLISHED
斷開后是這樣的:
root@VM-198-217-ubuntu:/opt/apache-tomcat-8.5.16/bin# netstat -an | grep 8000 tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN tcp 0 0 10.133.198.217:8000 60.177.99.27:49998 TIME_WAIT
idea連接遠(yuǎn)程端口進(jìn)行遠(yuǎn)程debug
我已經(jīng)在服務(wù)器上開啟了遠(yuǎn)程調(diào)試,idea連接的步驟,直接上圖。
edit configurations
遠(yuǎn)程調(diào)試配置
參數(shù)配置
將紅框內(nèi)的地址和端口號(hào)改成自己的。
啟動(dòng)遠(yuǎn)程調(diào)試
成功界面
請(qǐng)求一下試試
調(diào)試的姿勢(shì)和本地調(diào)試一樣,開始造起來(lái)吧!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA-SpringBoot項(xiàng)目Debug啟動(dòng)不了(卡住不動(dòng))的原因分析
這篇文章主要介紹了IDEA-SpringBoot項(xiàng)目Debug啟動(dòng)不了(卡住不動(dòng))的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11解決MyBatis中模糊搜索使用like匹配帶%字符時(shí)失效問(wèn)題
Mybatis是我們?nèi)粘m?xiàng)目中經(jīng)常使用的框架,在項(xiàng)目中我們一般會(huì)使用like查詢作為模糊匹配字符進(jìn)行搜索匹配,下面的Mapper.xml是我們使用like在項(xiàng)目中進(jìn)行模糊匹配的常用方式,感興趣的朋友跟隨小編一起看看吧2021-09-09線程池中execute與submit的區(qū)別說(shuō)明
這篇文章主要介紹了線程池execute與submit的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03