1. 程式人生 > >Java專案開啟遠端除錯(tomcat、springboot)

Java專案開啟遠端除錯(tomcat、springboot)

當我們執行一個專案的時候,一般都是在本地進行debug。但是如果是一個分散式的微服務,這時候我們選擇遠端debug是我們開發的利器。

環境

apache-tomcat-8.5.16

Linux

如何啟用遠端除錯

tomcat開啟遠端除錯

方法

切換到你的tomcat的bin目錄/apache-tomcat-8.5.16/bin
下,執行:

./catalina.sh jpda start  

執行上面的命令就可以開啟遠端debug了,如果想配置一些資訊,比如埠號什麼的,請參考下面的說明。

引數說明

#   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

操作說明

所以如果想修改配置,則如下操作:

  1. 在catalina.sh中進行配置:

    JPDA_TRANSPORT=dt_socket  
    JPDA_ADDRESS=5005  
    JPAD_SUSPEND=n  
  2. 或者通過JPDA_OPTS進行配置:

    JPDA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005’

springboot開啟遠端除錯

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 命令開啟遠端除錯

在執行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連線的步驟,直接上圖。

  1. edit configurations

    edit configurations

  2. 遠端除錯配置
    遠端除錯配置

  3. 引數配置
    引數配置

    將紅框內的地址和埠號改成自己的。

    引數配置

  4. 啟動遠端除錯
    啟動遠端除錯

  5. 成功介面

    成功介面

  6. 請求一下試試

    試試

除錯的姿勢和本地除錯一樣,開始造起來吧!