如何使用Eclipse调试Spring Boot应用程序?

Jef*_*fLL 91 java eclipse debugging spring spring-boot

我的Spring Bootwebapp运行得很好,我想通过Eclipse调试它.

因此,在启动我的远程Java应用程序调试器时,我应该收听哪个端口?我的webapp上有一个设置我必须设置为启用调试吗?

Dav*_*yer 155

你为什么不直接点击main()方法并选择"Debug As... Java Application"

  • 有时可能需要调试在服务器上远程运行的应用程序,这不适用. (8认同)
  • 我的情况是一个不同的情况,“直接运行Main”并没有选择一些spring配置文件,即bootstrap.xml,然后我不得不切换为以maven spring-boot:run的身份运行它。但是它的问题是,即使您正在从Eclipse运行此Maven目标,它也不会调试它。 (4认同)

小智 84

Spring Boot Reference中的19.2告诉您在启用远程调试支持的情况下启动应用程序.

$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
   -jar target/myproject-0.0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

启动应用程序后,只需在运行/调试配置中添加远程Java应用程序配置,选择启动应用程序时定义的端口/地址,然后就可以自由调试.

  • 请注意,在Java 8(我不知道其他版本)下,运行`java -agentlib:jdwp = help`会指出“较旧的-Xrunjdwp接口仍然可以使用,但在将来的发行版中将被删除”。而是使用-agentlib:jdwp = transport = dt_socket,address = 8000,server = y,suspend = n` (2认同)

Dur*_*ris 20

更简单的解决方案

而不是键入mvn spring-boot:run,只需键入mvnDebug spring-boot:run

您仍然需要通过在相关端口上为"远程Java应用程序"创建新的调试配置来在Eclipse中附加调试器.

  • 然后如何指定默认端口号以外的端口号? (2认同)

Duf*_*ffJ 17

我不需要设置远程调试才能使其正常工作,我使用了Maven.

  1. 确保在Eclipse中安装了Maven插件.
  2. 单击运行>运行配置> Maven构建>新启动配置:
    • 基目录:浏览到项目的根目录
    • 目标:spring-boot::run.
  3. 单击Apply,然后单击Run.

NB.如果您的IDE在进行逐行调试时发现项目的源代码时遇到问题,请查看此SO答案以了解如何手动将源代码附加到调试应用程序.

希望这有助于某人!


Abd*_*ull 17

如何调试远程登台或生产Spring Boot应用程序

服务器端

假设您已成功遵循Spring Boot的指南,将Spring Boot应用程序设置为服务.您的应用程序工件驻留在/srv/my-app/my-app.war配置文件中/srv/my-app/my-app.conf:

# This is file my-app.conf
# What can you do in this .conf file? The my-app.war is prepended with a SysV init.d script
# (yes, take a look into the war file with a text editor). As my-app.war is symlinked in the init.d directory, that init.d script
# gets executed. One of its step is actually `source`ing this .conf file. Therefore we can do anything in this .conf file that
# we can also do in a regular shell script.

JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,address=localhost:8002,server=y,suspend=n"
export SPRING_PROFILES_ACTIVE=staging
Run Code Online (Sandbox Code Playgroud)

当你重新启动Spring Boot应用程序时sudo service my-app restart,那么在它的日志文件中/var/log/my-app.log应该是一行说Listening for transport dt_socket at address: 8002.

客户端(开发者机器)

打开到服务器的SSH端口转发隧道:ssh -L 8002:localhost:8002 myusername@staging.example.com.保持此SSH会话运行.

在Eclipse中,从工具栏中选择Run - > Debug Configurations - >选择Remote Java Application - >单击New按钮 - >选择Connection Type Standard(Socket Attach),Host localhost和Port 8002(或任何你拥有的)在之前的步骤中配置).单击Apply,然后单击Debug.

Eclipse调试器现在应该连接到远程服务器.切换到Debug透视图应该显示连接的JVM及其线程.断点应在远程触发后立即触发.


Arp*_*wal 13

运行以下命令pom.xml放置位置:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
Run Code Online (Sandbox Code Playgroud)

并在端口上启动带有调试选项的远程Java应用程序5005

  • 这无法等待调试器附加到"Spring Boot 2.0.3.RELEASE"上. (2认同)

krm*_*007 9

在我看来,最好的解决方案是在pom.xml中添加一个插件,你不需要一直做任何其他事情:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>
                        -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9898
                    </jvmArguments>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

  • @Stephane,此配置可能应该位于易于启用的特定配置文件中 (2认同)

小智 5

  • 右键单击您的项目并选择“Debug As => Java application or Spring boot App”-capture1 - 在此处输入图片说明
  • 如果您希望您的应用程序暂停在您应用程序的某个地方,双击左侧,您将拥有这样的捕获 2。 在此处输入图片说明
  • 然后当你启动你的应用程序时,使用这些箭头进行下一步。(捕获 3) 在此处输入图片说明