Idea中tomcat启动源码调试进入到tomcat内部进行调试的方法
使用idea开发工具调试代码的时候,如果是java的web项目,使用的是tomcat作为web容器,打断点debug调试跟踪,当跟踪到org.apache.catalina包下的时候,则无法进入,这是因为idea运行的tomcat是通过插件的方式集成的,tomcat里面的lib包不再项目的依赖路径中,所以不能跟踪进去
首先在自己项目中被tomcat回调的接口实现类中,标记一个断点信息,通过idea启动web项目,当出现如图所示的断点信息的时候,因为断点位置标记的是tomcat回调的接口类,所以按照调用堆栈网上则是tomcat内部代码,但此时双击org.apache.catalina包下面的类名,是没有任何反应的,因为我们还没有将tomcat对应的依赖文件添加到classpath下面
添加依赖
<dependency> <groupid>org.apache.tomcat</groupid> <artifactid>tomcat-catalina</artifactid> <version>8.5.55</version> <scope>provided</scope> </dependency>
因为运行时使用的是tomcat的lib目录下面的jar文件,所以此处的scope使用provided方式
下面就可以进入到tomcat源码调试了
tomcat启动日志是怎么执行打出来的?
03-jun-2020 10:31:30.929 信息 [main] org.apache.catalina.startup.versionloggerlistener.log server.服务器版本: apache tomcat/8.5.55
03-jun-2020 10:31:30.938 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 服务器构建: may 5 2020 22:10:54 utc
03-jun-2020 10:31:30.938 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 服务器版本号(: 8.5.55.0
03-jun-2020 10:31:30.938 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 操作系统名称: windows 10
03-jun-2020 10:31:30.938 信息 [main] org.apache.catalina.startup.versionloggerlistener.log os.版本: 10.0
03-jun-2020 10:31:30.938 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 架构: amd64
03-jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.versionloggerlistener.log java 环境变量: c:\program files\java\jdk1.8.0_212\jre
03-jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.versionloggerlistener.log java虚拟机版本: 1.8.0_212-b10
03-jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.versionloggerlistener.log jvm.供应商: oracle corporation
03-jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.versionloggerlistener.log catalina_base: c:\users\administrator\.intellijidea2018.3\system\tomcat\unnamed_spring-shiro-training
03-jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.versionloggerlistener.log catalina_home: d:\tomcat8.5.55\apache-tomcat-8.5.55
通过定位到 versionloggerlistener 日志可以查看到
private void log() { log.info(sm.getstring("versionloggerlistener.serverinfo.server.version", serverinfo.getserverinfo())); log.info(sm.getstring("versionloggerlistener.serverinfo.server.built", serverinfo.getserverbuilt())); log.info(sm.getstring("versionloggerlistener.serverinfo.server.number", serverinfo.getservernumber())); log.info(sm.getstring("versionloggerlistener.os.name", system.getproperty("os.name"))); log.info(sm.getstring("versionloggerlistener.os.version", system.getproperty("os.version"))); log.info(sm.getstring("versionloggerlistener.os.arch", system.getproperty("os.arch"))); log.info(sm.getstring("versionloggerlistener.java.home", system.getproperty("java.home"))); log.info(sm.getstring("versionloggerlistener.vm.version", system.getproperty("java.runtime.version"))); log.info(sm.getstring("versionloggerlistener.vm.vendor", system.getproperty("java.vm.vendor"))); log.info(sm.getstring("versionloggerlistener.catalina.base", system.getproperty("catalina.base"))); log.info(sm.getstring("versionloggerlistener.catalina.home", system.getproperty("catalina.home"))); if (logargs) { list<string> args = managementfactory.getruntimemxbean().getinputarguments(); for (string arg : args) { log.info(sm.getstring("versionloggerlistener.arg", arg)); } } if (logenv) { sortedmap<string, string> sortedmap = new treemap<>(system.getenv()); for (map.entry<string, string> e : sortedmap.entryset()) { log.info(sm.getstring("versionloggerlistener.env", e.getkey(), e.getvalue())); } } if (logprops) { sortedmap<string, string> sortedmap = new treemap<>(); for (map.entry<object, object> e : system.getproperties().entryset()) { sortedmap.put(string.valueof(e.getkey()), string.valueof(e.getvalue())); } for (map.entry<string, string> e : sortedmap.entryset()) { log.info(sm.getstring("versionloggerlistener.prop", e.getkey(), e.getvalue())); } } }
发现是通过键值对的方式获取的,再通过字符串全局搜索发现
但是匹配的是英文,那么中文是怎么打出来的呢?
最后通过调试发现找的是这个
类似上面的东西,调试的时候发现tocmat的东西启动还是相当多的
且看下面这个
idea tomcat启动后的数据
03-jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-djava.util.logging.config.file=c:\users\administrator\.intellijidea2018.3\system\tomcat\unnamed_spring-shiro-training\conf\logging.properties
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-djava.util.logging.manager=org.apache.juli.classloaderlogmanager
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:52290,suspend=y,server=n
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-javaagent:c:\users\administrator\.intellijidea2018.3\system\captureagent\debugger-agent.jar
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dfile.encoding=utf-8
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dcom.sun.management.jmxremote=
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dcom.sun.management.jmxremote.port=1099
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dcom.sun.management.jmxremote.ssl=false
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dcom.sun.management.jmxremote.password.file=c:\users\administrator\.intellijidea2018.3\system\tomcat\unnamed_spring-shiro-training\jmxremote.password
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dcom.sun.management.jmxremote.access.file=c:\users\administrator\.intellijidea2018.3\system\tomcat\unnamed_spring-shiro-training\jmxremote.access
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-djava.rmi.server.hostname=127.0.0.1
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-djdk.tls.ephemeraldhkeysize=2048
03-jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-djava.protocol.handler.pkgs=org.apache.catalina.webresources
03-jun-2020 10:31:30.941 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dignore.endorsed.dirs=
03-jun-2020 10:31:30.941 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dcatalina.base=c:\users\administrator\.intellijidea2018.3\system\tomcat\unnamed_spring-shiro-training
03-jun-2020 10:31:30.941 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-dcatalina.home=d:\tomcat8.5.55\apache-tomcat-8.5.55
03-jun-2020 10:31:30.941 信息 [main] org.apache.catalina.startup.versionloggerlistener.log 命令行参数:-djava.io.tmpdir=d:\tomcat8.5.55\apache-tomcat-8.5.55\temp
以上还只是基本的环境配置等启动
然后是准备链接到tomcat服务
03-jun-2020 10:31:30.941 信息 [main] org.apache.catalina.core.aprlifecyclelistener.lifecycleevent 使用apr版本[1.7.0]加载了基于apr的apache tomcat本机库[1.2.24]。
03-jun-2020 10:31:30.941 信息 [main] org.apache.catalina.core.aprlifecyclelistener.lifecycleevent apr功能:ipv6[true]、sendfile[true]、accept filters[false]、random[true]。
03-jun-2020 10:31:30.941 信息 [main] org.apache.catalina.core.aprlifecyclelistener.lifecycleevent apr/openssl配置:useaprconnector[false],useopenssl[true]
03-jun-2020 10:31:30.944 信息 [main] org.apache.catalina.core.aprlifecyclelistener.initializessl openssl成功初始化 [openssl 1.1.1g 21 apr 2020]
03-jun-2020 10:31:31.032 信息 [main] org.apache.coyote.abstractprotocol.init 初始化协议处理器 ["http-nio-8080"]
03-jun-2020 10:31:31.046 信息 [main] org.apache.tomcat.util.net.nioselectorpool.getsharedselector using a shared selector for servlet write/read
03-jun-2020 10:31:31.055 信息 [main] org.apache.catalina.startup.catalina.load initialization processed in 175489 ms
03-jun-2020 10:31:31.080 信息 [main] org.apache.catalina.core.standardservice.startinternal 正在启动服务[catalina]
03-jun-2020 10:31:31.080 信息 [main] org.apache.catalina.core.standardengine.startinternal starting servlet engine: apache tomcat/8.5.55
03-jun-2020 10:31:31.089 信息 [main] org.apache.coyote.abstractprotocol.start 开始协议处理句柄["http-nio-8080"]
03-jun-2020 10:31:31.102 信息 [main] org.apache.catalina.startup.catalina.start server startup in 47 ms
tomat启动主要是在catalina中
然后是
启动
/** * await and shutdown. */ public void await() { getserver().await(); }
其实tomcat启动本质上就是socket的服务器罢了
@override public void await() { // negative values - don't wait on port - tomcat is embedded or we just don't like ports if (port == -2) { // undocumented yet - for embedding apps that are around, alive. return; } if (port==-1) { try { awaitthread = thread.currentthread(); while(!stopawait) { try { thread.sleep( 10000 ); } catch( interruptedexception ex ) { // continue and check the flag } } } finally { awaitthread = null; } return; } // set up a server socket to wait on try { awaitsocket = new serversocket(port, 1, inetaddress.getbyname(address)); } catch (ioexception e) { log.error("standardserver.await: create[" + address + ":" + port + "]: ", e); return; } try { awaitthread = thread.currentthread(); // loop waiting for a connection and a valid command while (!stopawait) { serversocket serversocket = awaitsocket; if (serversocket == null) { break; } // wait for the next connection socket socket = null; stringbuilder command = new stringbuilder(); try { inputstream stream; long acceptstarttime = system.currenttimemillis(); try { socket = serversocket.accept(); //一旦accecpt后面就开始执行了 socket.setsotimeout(10 * 1000); // ten seconds stream = socket.getinputstream(); } catch (sockettimeoutexception ste) { // this should never happen but bug 56684 suggests that // it does. log.warn(sm.getstring("standardserver.accept.timeout", long.valueof(system.currenttimemillis() - acceptstarttime)), ste); continue; } catch (accesscontrolexception ace) { log.warn(sm.getstring("standardserver.accept.security"), ace); continue; } catch (ioexception e) { if (stopawait) { // wait was aborted with socket.close() break; } log.error(sm.getstring("standardserver.accept.error"), e); break; } // read a set of characters from the socket int expected = 1024; // cut off to avoid dos attack while (expected < shutdown.length()) { if (random == null) random = new random(); expected += (random.nextint() % 1024); } while (expected > 0) { int ch = -1; try { ch = stream.read(); } catch (ioexception e) { log.warn(sm.getstring("standardserver.accept.readerror"), e); ch = -1; } // control character or eof (-1) terminates loop if (ch < 32 || ch == 127) { break; } command.append((char) ch); expected--; } } finally { // close the socket now that we are done with it try { if (socket != null) { socket.close(); } } catch (ioexception e) { // ignore } } // match against our command string boolean match = command.tostring().equals(shutdown); if (match) { log.info(sm.getstring("standardserver.shutdownviaport")); break; } else log.warn(sm.getstring("standardserver.invalidshutdowncommand", command.tostring())); } } finally { serversocket serversocket = awaitsocket; awaitthread = null; awaitsocket = null; // close the server socket and return if (serversocket != null) { try { serversocket.close(); } catch (ioexception e) { // ignore } } } }
tomcat容器启动之后,下面就是springmvc模块的内容了
到此这篇关于idea中tomcat启动源码调试进入到tomcat内部进行调试的方法的文章就介绍到这了,更多相关idea中tocmat启动源码调试内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!