1. 程式人生 > 實用技巧 >本地使用 IDEA 除錯遠端部署的程式程式碼

本地使用 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