Java项⽬开启远程调试的⽅法步骤(tomcat、springboot)
当我们运⾏⼀个项⽬的时候,⼀般都是在本地进⾏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,
spring boot选择题# 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开启远程调试
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
如果想深⼊了解Java调试,那么去看⼀下这个吧。
问题
如果出现Connection refused。
⾸先检查⼀下端⼝8000的使⽤情况:
use:~/tomcat/logs # netstat -an|grep 8000
cp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN
可见当前16808端⼝服务被绑定了回环地址,外部⽆法访问。即本地调试。
办法:
修改catalina.sh中⼀个参数。
if [ -z "$JPDA_TRANSPORT" ]; then
JPDA_TRANSPORT="dt_socket"
fi
if [ -z "$JPDA_ADDRESS" ]; then
JPDA_ADDRESS="0.0.0.0:8000"
fi
if [ -z "$JPDA_SUSPEND" ]; then
JPDA_SUSPEND="n"
fi
对JPDA_ADDRESS="localhost:8000"把默认值(localhost:8000)改成0.0.0.0:8000。默认是本地ip调试也就是⽆法远程调试,0.0.0.0表⽰所有ip地址都能调试。远程连上后再看端⼝情况:
root@VM-198-217-ubuntu:/opt/apache-tomcat-8.5.16/bin# netstat -an | grep 8000
tcp 0 0 10.133.198.217:8000 60.177.99.27:49998 ESTABLISHED
断开后是这样的:
root@VM-198-217-ubuntu:/opt/apache-tomcat-8.5.16/bin# netstat -an | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN
tcp 0 0 10.133.198.217:8000 60.177.99.27:49998 TIME_WAIT
idea连接远程端⼝进⾏远程debug
我已经在服务器上开启了远程调试,idea连接的步骤,直接上图。
edit configurations
远程调试配置
参数配置
将红框内的地址和端⼝号改成⾃⼰的。
启动远程调试
成功界⾯
请求⼀下试试
调试的姿势和本地调试⼀样,开始造起来吧!
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论