springboot 远程debug
应用:本地打断点,对运行在远程服务器上的程序进行调试
*******************
远程调试
原理:本地jvm程序与远程jvm程序代码保持一致,通过调试协议进行通信
attach 模式:远程服务器暴露端口、开启监听,本地连接远程服务器
listen 模式:本地暴露端口、开启监听,远程应用连接到本地
-Xdebug:开启远程调试
jwdp参数,格式:
-Xrunjwdp:name=value,name2=value2,...
-agentlib:jwdp:name=value,name2=value2,...
jwdp name可选值:
transport:远程debug程序连接方式,dt_socket(默认)、dt_shmem(共享内存)
server:y(debug服务端)、n(默认,debug客户端)
address:server=n时必须设置;server=y时,可不设置
launch:jwdp初始化完成后,启动指定的程序(可选)
onthrow:指定异常发生后,开始jwdp的初始化(可选)
onuncaught:y(未捕获的异常发生后,开始jwdp的初始化)(可选)
n(默认,jwdp正常初始化)
stdalloc:n(默认值),可选
By default, the JDWP reference implementation uses an alternate allocator for its memory allocation.
If “y”, the standard C runtime library allocator will be used. This option is mainly for testing; use it with care.
Deadlocks can occur in this VM if the alternative allocator is disabled
strict:n(默认值),可选
If “y”, assume strict JVMDI conformance. This will disable all workarounds to known bugs in JVMDI implementations.
This option is mainly for testing and should be used with care.
suspend:y(默认值),可选
debug服务端是否等待debug客户端连接上后再启动,y:挂起等待、n:直接启动
If “y”, VMStartEvent has a suspend Policy of SUSPEND_ALL.
If “n”, VMStartEvent has a suspend policy of SUSPEND_NONE
debug服务端参数
jdk5及以后:-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
jdk4:-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
jdk3及之前:-Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
debug客户端只需要连接服务端的ip:port(5005)即可进行调试
*******************
示例
*************
controller 层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
int i=1;
i+=1;
System.out.println(i);
return "hello";
}
}
项目打包:demo.jar
*******************
attach 模式
demo.jar上传到远程服务器:suspend=n
[root@centos test3]# java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=\*:5005 demo.jar
Listening for transport dt_socket at address: 5005
# 远程服务器开启监听端口:5005
# suspend=n,debug服务端不需要等待客户端连接就可以启动
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.4)
2021-09-07 20:57:06.135 INFO 7603 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 16.0.2 on centos with PID 7603 (/usr/java/test3/demo.jar started by root in /usr/java/test3)
2021-09-07 20:57:06.152 INFO 7603 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-09-07 20:57:09.843 INFO 7603 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-09-07 20:57:09.908 INFO 7603 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-09-07 20:57:09.909 INFO 7603 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.52]
2021-09-07 20:57:10.256 INFO 7603 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-09-07 20:57:10.256 INFO 7603 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3745 ms
2021-09-07 20:57:12.248 INFO 7603 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-09-07 20:57:12.299 INFO 7603 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 8.387 seconds (JVM running for 12.667)
远程服务端:suspend=y
[root@centos test3]# java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=\*:5005 demo.jar
Listening for transport dt_socket at address: 5005
# suspend=y:远程服务端开启监听端口,一直处于监听状态,直到debug客户端连接后才会启动
debug客户端:run ==> edit configuration ==> remote jvm debug
设置host、port
点击debug
debug服务端连接成功
本地打断点
发送请求:192.168.57.120:8080/hello
本地代码进入断点
*******************
listen 模式
先开启本地debug(listen模式)
点击debug
demo.jar上传到远程服务器,启动
[root@centos test3]# java -jar -agentlib:jdwp=transport=dt_socket,server=n,address=******:5005,suspend=y demo.jar
******:本地服务器ip
server=n:表示远程服务器为debug客户端
server=n时,suspend=y、n启动效果等同
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.4)
2021-09-07 21:28:16.162 INFO 7868 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 16.0.2 on centos with PID 7868 (/usr/java/test3/demo.jar started by root in /usr/java/test3)
2021-09-07 21:28:16.164 INFO 7868 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-09-07 21:28:18.888 INFO 7868 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-09-07 21:28:18.920 INFO 7868 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-09-07 21:28:18.921 INFO 7868 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.52]
2021-09-07 21:28:19.163 INFO 7868 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-09-07 21:28:19.163 INFO 7868 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2878 ms
2021-09-07 21:28:20.941 INFO 7868 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-09-07 21:28:20.967 INFO 7868 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 6.044 seconds (JVM running for 7.015)
远程debug客户端启动后,debug连接建立
本地打断点,发送请求:192.168.57.120:8080/hello
listen模式重新debug,需要重启远程应用,attach模式不需要