当前位置 主页 > 服务器问题 > Linux/apache问题 >

    Java项目开启远程调试的方法步骤(tomcat、springboot)

    栏目:Linux/apache问题 时间:2019-10-16 15:36

    当我们运行一个项目的时候,一般都是在本地进行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

    操作说明

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

    在catalina.sh中进行配置:

    JPDA_TRANSPORT=dt_socket 
    JPDA_ADDRESS=5005 
    JPAD_SUSPEND=n 

    或者通过JPDA_OPTS进行配置:

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

    springboot开启远程调试

    远程调试maven设置

    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