使用idea開發(fā)工具調(diào)試代碼的時(shí)候,如果是java的web項(xiàng)目,使用的是tomcat作為web容器,打斷點(diǎn)debug調(diào)試跟蹤,當(dāng)跟蹤到org.apache.catalina包下的時(shí)候,則無(wú)法進(jìn)入,這是因?yàn)閕dea運(yùn)行的tomcat是通過(guò)插件的方式集成的,tomcat里面的lib包不再項(xiàng)目的依賴路徑中,所以不能跟蹤進(jìn)去
首先在自己項(xiàng)目中被tomcat回調(diào)的接口實(shí)現(xiàn)類中,標(biāo)記一個(gè)斷點(diǎn)信息,通過(guò)idea啟動(dòng)web項(xiàng)目,當(dāng)出現(xiàn)如圖所示的斷點(diǎn)信息的時(shí)候,因?yàn)閿帱c(diǎn)位置標(biāo)記的是tomcat回調(diào)的接口類,所以按照調(diào)用堆棧網(wǎng)上則是tomcat內(nèi)部代碼,但此時(shí)雙擊org.apache.catalina包下面的類名,是沒(méi)有任何反應(yīng)的,因?yàn)槲覀冞€沒(méi)有將tomcat對(duì)應(yīng)的依賴文件添加到classpath下面
添加依賴
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.5.55</version>
<scope>provided</scope>
</dependency>
因?yàn)檫\(yùn)行時(shí)使用的是tomcat的lib目錄下面的jar文件,所以此處的scope使用provided方式
下面就可以進(jìn)入到tomcat源碼調(diào)試了
tomcat啟動(dòng)日志是怎么執(zhí)行打出來(lái)的?
03-Jun-2020 10:31:30.929 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server.服務(wù)器版本: Apache Tomcat/8.5.55
03-Jun-2020 10:31:30.938 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 服務(wù)器構(gòu)建: May 5 2020 22:10:54 UTC
03-Jun-2020 10:31:30.938 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 服務(wù)器版本號(hào)(: 8.5.55.0
03-Jun-2020 10:31:30.938 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 操作系統(tǒng)名稱: 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 架構(gòu): amd64
03-Jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java 環(huán)境變量: C:\Program Files\Java\jdk1.8.0_212\jre
03-Jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java虛擬機(jī)版本: 1.8.0_212-b10
03-Jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM.供應(yīng)商: 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
通過(guò)定位到 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()));
}
}
}
發(fā)現(xiàn)是通過(guò)鍵值對(duì)的方式獲取的,再通過(guò)字符串全局搜索發(fā)現(xiàn)
但是匹配的是英文,那么中文是怎么打出來(lái)的呢?
最后通過(guò)調(diào)試發(fā)現(xiàn)找的是這個(gè)
類似上面的東西,調(diào)試的時(shí)候發(fā)現(xiàn)tocmat的東西啟動(dòng)還是相當(dāng)多的
且看下面這個(gè)
idea tomcat啟動(dòng)后的數(shù)據(jù)
03-Jun-2020 10:31:30.939 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-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 命令行參數(shù):-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
03-Jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-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 命令行參數(shù):-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 命令行參數(shù):-Dfile.encoding=UTF-8
03-Jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-Dcom.sun.management.jmxremote=
03-Jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-Dcom.sun.management.jmxremote.port=1099
03-Jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-Dcom.sun.management.jmxremote.ssl=false
03-Jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-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 命令行參數(shù):-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 命令行參數(shù):-Djava.rmi.server.hostname=127.0.0.1
03-Jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-Djdk.tls.ephemeralDHKeySize=2048
03-Jun-2020 10:31:30.940 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-Djava.protocol.handler.pkgs=org.apache.catalina.webresources
03-Jun-2020 10:31:30.941 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-Dignore.endorsed.dirs=
03-Jun-2020 10:31:30.941 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行參數(shù):-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 命令行參數(shù):-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 命令行參數(shù):-Djava.io.tmpdir=D:\tomcat8.5.55\apache-tomcat-8.5.55\temp
以上還只是基本的環(huán)境配置等啟動(dòng)
然后是準(zhǔn)備鏈接到tomcat服務(wù)
03-Jun-2020 10:31:30.941 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent 使用APR版本[1.7.0]加載了基于APR的Apache Tomcat本機(jī)庫(kù)[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 初始化協(xié)議處理器 ["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 正在啟動(dòng)服務(wù)[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 開始協(xié)議處理句柄["http-nio-8080"]
03-Jun-2020 10:31:31.102 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 47 ms
tomat啟動(dòng)主要是在Catalina中
然后是
啟動(dòng)
/**
* Await and shutdown.
*/
public void await() {
getServer().await();
}
其實(shí)tomcat啟動(dòng)本質(zhì)上就是socket的服務(wù)器罷了
@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后面就開始執(zhí)行了
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容器啟動(dòng)之后,下面就是Springmvc模塊的內(nèi)容了
到此這篇關(guān)于Idea中tomcat啟動(dòng)源碼調(diào)試進(jìn)入到tomcat內(nèi)部進(jìn)行調(diào)試的方法的文章就介紹到這了,更多相關(guān)Idea中tocmat啟動(dòng)源碼調(diào)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!