详述 IntelliJ IDEA 远程调试 Tomcat 的方法

参考:

  1. https://blog.csdn.net/qq_35246620/article/details/78507324
  2. https://blog.csdn.net/qq_37192800/article/details/80761643

在调试代码的过程中,为了更好的定位及解决问题,有时候需要我们使用远程调试的方法。在本文中,就让我们一起来看看,如何利用 IntelliJ IDEA 进行远程 Tomcat 的调试。

首先,配置remote:

在这里插入图片描述

如上图所示,点击Edit Configurations,进入如下界面:
在这里插入图片描述

如上图所示,我们进入了Run/Debug Configurations界面,然后点击左上角的+,选择Remote:
在这里插入图片描述

标注 1:运行远程 JVM 的命令行参数;
标注 2:传输方式,默认为Socket;
标注 3:调试模式,默认为Attach;
标注 4:服务器 IP 地址,默认为localhost,需要修改为目标服务器的真实 IP 地址;
标注 5:服务器端口号,默认为5005,需要修改为目标服务器的真实端口号;
标注 6:搜索资源是使用的环境变量,默认为,即整个项目。
如上图所示,其中 标注 2 和 标注 3 又分别有两种分类,对于 标注 2,

标注 2:传输方式,默认为Socket;
Socket:macOS 及 Linux 系统使用此种传输方式;
Shared memory: Windows 系统使用此种传输方式。
对于 标注 3,

标注 3:调试模式,默认为Attach;
Attach:此种模式下,调试服务端(被调试远程运行的机器)启动一个端口等待我们(调试客户端)去连接;
Listen: 此种模式下,是我们(调试客户端)去监听一个端口,当调试服务端准备好了,就会进行连接。
然后,复制 标注 1,即 IntelliJ IDEA 自动生产的命令行参数,然后导入到 Tomcat 的配置文件中。以 Linux 系统为例,导入语句为:

export JAVA_OPTS=’-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8001’
如果是 Windows 系统,则导入语句为:

set JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8001
两者的区别在于导入语句的关键字不同以及有无引号,Linux 系统的导入关键字为export,Windows 为set;Linux 的导入值需要用单引号’'括起来,而 Windows 则不用。

接下来,修改 Tomcat 的 bin 目录下的catalina.sh文件(如果是 Windows 系统则修改catalina.bat文件),将上述的导入语句添加到此文件中即可:

在这里插入图片描述

至此,IntelliJ IDEA 远程调试 Tomcat 的配置已经完成了,调试的后续步骤按正常的调试技巧进行就可以啦!

+++++ 题外话:我是一个大彩蛋 +++++

在这里,我们假设服务器的 IP 地址为10.11.12.39,端口号为16203,设置完成后,进入Debug模式,如果连接成功,则会出现如下提示:

在这里插入图片描述

此外,如果我们是跨多个系统进行调试,则只需要在想要调试的系统中配置Remote,打上断点,启动Debug模式,然后在服务开始的地方执行程序即可进入到我们设置的断点。而且,如果我们在本地配置Remote并关联到某个 Tomcat,在Debug模式下,所有涉及到断点所在代码的功能,都会进入我们设置的断点。

例如,对于服务器上的 Tomcat A,多个系统都用到了这个 Tomcat,如订单子系统、账户子系统、路由子系统等,并且多个系统间互相调用,如订单子系统调了账户子系统,账户子系统又调了路由子系统,则当我们在这三个子系统中配置Remote并在对应的代码(如在订单子系统中查询商户的账户信息,则调到账户子系统;在账户子系统中又通过路由子系统调到其他底层服务查询商户的账户余额等)上打上断点,启动Debug模式之后,通过单元测试或者页面操作触发订单子系统中的查询商户的账户信息功能,则会依次进入到在上述三个子系统中设置的断点。

此外,在我们配置完远程调试之后,就算别人启动相关服务,也会进入到我们的断点,而且会受到我们设置的断点的影响,只有在我们执行完测试之后,服务才会继续执行下去。最后,远程调试的功能真的很强大,善用远程调试,远离 Bug!

catalina.sh

#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# -----------------------------------------------------------------------------
# Control Script for the CATALINA Server
#
# Environment Variable Prerequisites
#
#   Do not set the variables in this script. Instead put them into a script
#   setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
#
#   CATALINA_HOME   May point at your Catalina "build" directory.
#
#   CATALINA_BASE   (Optional) Base directory for resolving dynamic portions
#                   of a Catalina installation.  If not present, resolves to
#                   the same directory that CATALINA_HOME points to.
#
#   CATALINA_OUT    (Optional) Full path to a file where stdout and stderr
#                   will be redirected.
#                   Default is $CATALINA_BASE/logs/catalina.out
#
#   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
#                   "run" or "debug" command is executed.
#                   Include here and not in JAVA_OPTS all options, that should
#                   only be used by Tomcat itself, not by the stop process,
#                   the version command etc.
#                   Examples are heap size, GC logging, JMX ports etc.
#
#   CATALINA_TMPDIR (Optional) Directory path location of temporary directory
#                   the JVM should use (java.io.tmpdir).  Defaults to
#                   $CATALINA_BASE/temp.
#
#   JAVA_HOME       Must point at your Java Development Kit installation.
#                   Required to run the with the "debug" argument.
#
#   JRE_HOME        Must point at your Java Runtime installation.
#                   Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
#                   are both set, JRE_HOME is used.
#
#   JAVA_OPTS       (Optional) Java runtime options used when any command
#                   is executed.
#                   Include here and not in CATALINA_OPTS all options, that
#                   should be used by Tomcat and also by the stop process,
#                   the version command etc.
#                   Most options should go into CATALINA_OPTS.
#
#   JAVA_ENDORSED_DIRS (Optional) Lists of of colon separated directories
#                   containing some jars in order to allow replacement of APIs
#                   created outside of the JCP (i.e. DOM and SAX from W3C).
#                   It can also be used to update the XML parser implementation.
#                   Defaults to $CATALINA_HOME/endorsed.
#
#   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 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,
#                       address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND
#
#   CATALINA_PID    (Optional) Path of the file which should contains the pid
#                   of the catalina startup java process, when start (fork) is
#                   used
#
#   LOGGING_CONFIG  (Optional) Override Tomcat's logging config file
#                   Example (all one line)
#                   LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
#
#   LOGGING_MANAGER (Optional) Override Tomcat's logging manager
#                   Example (all one line)
#                   LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
# -----------------------------------------------------------------------------

JAVA_HOME=/usr/local/jdk
JRE_HOME=$JAVA_HOME/jre
JAVA_OPTS="-Xmx1500m -XX:MaxPermSize=256m -Dspring.profiles.active=qa,JobTriggerAndExecutor"
CATALINA_OPTS="-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8899"

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false
darwin=false
os400=false
case "`uname`" in
CYGWIN*) cygwin=true;;
Darwin*) darwin=true;;
OS400*) os400=true;;
esac

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值