當咱們運行一個項目的時候,通常都是在本地進行debug。可是若是是一個分佈式的微服務,這時候咱們選擇遠程debug是咱們開發的利器。java
環境
apache-tomcat-8.5.16spring
Linuxapache
如何啓用遠程調試
tomcat開啓遠程調試
方法
切換到你的tomcat的bin目錄/apache-tomcat-8.5.16/bin
下,執行:ubuntu
./catalina.sh jpda start
執行上面的命令就能夠開啓遠程debug了,若是想配置一些信息,好比端口號什麼的,請參考下面的說明。tomcat
參數說明
# 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
操做說明
因此若是想修改配置,則以下操做:springboot
在catalina.sh中進行配置:服務器
JPDA_TRANSPORT=dt_socket JPDA_ADDRESS=5005 JPAD_SUSPEND=n
或者經過JPDA_OPTS進行配置:app
JPDA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005’
springboot開啓遠程調試
遠程調試maven設置jvm
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 5005socket
<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 命令開啓遠程調試
在執行jar的時候,添加上參數。以下:
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar demo.jar
若是想深刻了解Java調試,那麼去看一下這個吧。深刻Java調試體系
問題
若是出現Connection refused。
首先檢查一下端口8000的使用狀況:
use:~/tomcat/logs # netstat -an|grep 8000
cp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN
可見當前16808端口服務被綁定了迴環地址,外部沒法訪問。即本地調試。
辦法:
修改catalina.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
對JPDA_ADDRESS="localhost:8000" 把默認值(localhost:8000)改爲0.0.0.0:8000。默認是本地ip調試也就是沒法遠程調試,0.0.0.0表示全部ip地址都能調試。
遠程連上後再看端口狀況:
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鏈接遠程端口進行遠程debug
我已經在服務器上開啓了遠程調試,idea鏈接的步驟,直接上圖。
edit configurations
遠程調試配置
參數配置
將紅框內的地址和端口號改爲本身的。
啓動遠程調試
成功界面
請求一下試試
調試的姿式和本地調試同樣,開始造起來吧!