学而不思则罔 思而不学则殆.|

漆原Blog

园龄:5年10个月粉丝:0关注:1

本地使用 IDEA 调试远程部署的程序代码

​日常开发工作中经常会出现代码本地运行无问题,但部署到服务器中功能无法正常使用。本篇博客介绍如何使用IDEA像调试本地代码一样,调试远端代码。

  • 服务端口:远端部署的应用对外开放的访问端口。

  • 调试端口:远端部署的应用额外开启的对外调试端口。

    当远端服务被调用时,触发本地IDEA上的调试断点,用于调试远程服务器上部署的应用程序。


    实现步骤

    1. 创建springboot工程,并指定服务端口。(当然也可以不指定,默认8080)

      application.properties

      server.port=18080
      
    2. 配置本地IDEA远程连接。

      • Name:远程连接名称,任意写。
      • Host:远程主机。(服务部署服务器)
      • Port:调试端口
      • Command line arguments for remote JVM:填写好主机、端口信息后,IDEA会自动生成命令行,用于远端服务启动参数。
    3. 打包应用,并将Jar包上传至服务器。

    4. 启动服务。

      java -Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8088 -jar remote-debug-demo-0.0.1-SNAPSHOT.jar
      

      服务器启动日志:

      [root@iZf8ozvj95ui19Z code]# java -Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8088 -jar remote-debug-demo-0.0.1-SNAPSHOT.jar 
      Listening for transport dt_socket at address: 8088
      
        .   ____          _            __ _ _
       /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
      ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
       \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
        '  |____| .__|_| |_|_| |_\__, | / / / /
       =========|_|==============|___/=/_/_/_/
       :: Spring Boot ::        (v2.3.1.RELEASE)
      
      2020-06-23 20:14:16.975  INFO 3206 --- [           main] com.ari.RemoteDebugDemoApplication       : Starting RemoteDebugDemoApplication v0.0.1-SNAPSHOT on iZf8ozvj95ui19Z with PID 3206 (/usr/code/remote-debug-demo-0.0.1-SNAPSHOT.jar started by root in /usr/code)
      2020-06-23 20:14:16.980  INFO 3206 --- [           main] com.ari.RemoteDebugDemoApplication       : No active profile set, falling back to default profiles: default
      2020-06-23 20:14:20.567  INFO 3206 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 18080 (http)
      2020-06-23 20:14:20.630  INFO 3206 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
      2020-06-23 20:14:20.631  INFO 3206 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
      2020-06-23 20:14:21.020  INFO 3206 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
      2020-06-23 20:14:21.020  INFO 3206 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3904 ms
      2020-06-23 20:14:22.194  INFO 3206 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
      2020-06-23 20:14:23.003  INFO 3206 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 18080 (http) with context path ''
      2020-06-23 20:14:23.041  INFO 3206 --- [           main] com.ari.RemoteDebugDemoApplication       : Started RemoteDebugDemoApplication in 7.484 seconds (JVM running for 8.882)
      2020-06-23 20:17:25.379  INFO 3206 --- [io-18080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
      2020-06-23 20:17:25.379  INFO 3206 --- [io-18080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
      2020-06-23 20:17:25.415  INFO 3206 --- [io-18080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 36 ms
      

      Listening for transport dt_socket at address:8088 调试端口:8088

      Tomcat started on port(s): 18080 (http) with context path '' 服务端口:18080

    5. 启动本地远程连接,控制台提示”成功连接到远程调试端口“

      Connected to the target VM, address: '118.190.209.24:8088', transport: 'socket'
      
    6. 本地打断点并重新访问远端服务,本地成功进入DEBUG

    原视频链接:https://www.bilibili.com/video/BV1Da4y1e7kG

本文作者:漆原Blog

本文链接:https://www.cnblogs.com/7moon/p/13786591.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   漆原Blog  阅读(808)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起