一:核心流程源码剖析

Tomcat中的各容器组件都会涉及创建、销毁等,因此设计了生命周期接口Lifecycle进行统一规范,各容 器组件实现该接口。
Lifecycle生命周期接口主要方法示意
image.png
Lifecycle生命周期接口继承体系示意

二、核心流程源码剖析

源码追踪部分我们关注两个流程:
Tomcat启动流程和Tomcat请求处理流程 Tomcat启动流程

1、Tomcat启动流程

涉及到很多容器,初始化、启动。下面的各个容器、容器有生命周期,设计时候高端设计,都继承了Lifecycle接口,
同一管理生命周期。
image.png

2、Tomcat请求处理流程

请求处理流程分析
image.png

请求处理流程示意图
image.png

3、Mapper组件体系结构

image.png

三、点击查看源码流程

Tomcat初始化部分

参考Tomcat启动时序图来看源码

1

时序图:
image.png
源码:
image.png
image.png

2

时序图:
image.png
源码:
image.png

3、

image.png

4、

image.png

  1. protected Digester createStartDigester() {
  2. long t1=System.currentTimeMillis();
  3. // Initialize the digester
  4. Digester digester = new Digester();
  5. digester.setValidating(false);
  6. digester.setRulesValidation(true);
  7. Map<Class<?>, List<String>> fakeAttributes = new HashMap<>();
  8. List<String> objectAttrs = new ArrayList<>();
  9. objectAttrs.add("className");
  10. fakeAttributes.put(Object.class, objectAttrs);
  11. // Ignore attribute added by Eclipse for its internal tracking
  12. List<String> contextAttrs = new ArrayList<>();
  13. contextAttrs.add("source");
  14. fakeAttributes.put(StandardContext.class, contextAttrs);
  15. digester.setFakeAttributes(fakeAttributes);
  16. digester.setUseContextClassLoader(true);
  17. // Configure the actions we will be using
  18. digester.addObjectCreate("Server",
  19. "org.apache.catalina.core.StandardServer",
  20. "className");
  21. digester.addSetProperties("Server");
  22. digester.addSetNext("Server",
  23. "setServer",
  24. "org.apache.catalina.Server");
  25. digester.addObjectCreate("Server/GlobalNamingResources",
  26. "org.apache.catalina.deploy.NamingResourcesImpl");
  27. digester.addSetProperties("Server/GlobalNamingResources");
  28. digester.addSetNext("Server/GlobalNamingResources",
  29. "setGlobalNamingResources",
  30. "org.apache.catalina.deploy.NamingResourcesImpl");
  31. digester.addObjectCreate("Server/Listener",
  32. null, // MUST be specified in the element
  33. "className");
  34. digester.addSetProperties("Server/Listener");
  35. digester.addSetNext("Server/Listener",
  36. "addLifecycleListener",
  37. "org.apache.catalina.LifecycleListener");
  38. digester.addObjectCreate("Server/Service",
  39. "org.apache.catalina.core.StandardService",
  40. "className");
  41. digester.addSetProperties("Server/Service");
  42. digester.addSetNext("Server/Service",
  43. "addService",
  44. "org.apache.catalina.Service");
  45. digester.addObjectCreate("Server/Service/Listener",
  46. null, // MUST be specified in the element
  47. "className");
  48. digester.addSetProperties("Server/Service/Listener");
  49. digester.addSetNext("Server/Service/Listener",
  50. "addLifecycleListener",
  51. "org.apache.catalina.LifecycleListener");
  52. //Executor
  53. digester.addObjectCreate("Server/Service/Executor",
  54. "org.apache.catalina.core.StandardThreadExecutor",
  55. "className");
  56. digester.addSetProperties("Server/Service/Executor");
  57. digester.addSetNext("Server/Service/Executor",
  58. "addExecutor",
  59. "org.apache.catalina.Executor");
  60. digester.addRule("Server/Service/Connector",
  61. new ConnectorCreateRule());
  62. digester.addRule("Server/Service/Connector",
  63. new SetAllPropertiesRule(new String[]{"executor", "sslImplementationName"}));
  64. digester.addSetNext("Server/Service/Connector",
  65. "addConnector",
  66. "org.apache.catalina.connector.Connector");
  67. digester.addObjectCreate("Server/Service/Connector/SSLHostConfig",
  68. "org.apache.tomcat.util.net.SSLHostConfig");
  69. digester.addSetProperties("Server/Service/Connector/SSLHostConfig");
  70. digester.addSetNext("Server/Service/Connector/SSLHostConfig",
  71. "addSslHostConfig",
  72. "org.apache.tomcat.util.net.SSLHostConfig");
  73. digester.addRule("Server/Service/Connector/SSLHostConfig/Certificate",
  74. new CertificateCreateRule());
  75. digester.addRule("Server/Service/Connector/SSLHostConfig/Certificate",
  76. new SetAllPropertiesRule(new String[]{"type"}));
  77. digester.addSetNext("Server/Service/Connector/SSLHostConfig/Certificate",
  78. "addCertificate",
  79. "org.apache.tomcat.util.net.SSLHostConfigCertificate");
  80. digester.addObjectCreate("Server/Service/Connector/SSLHostConfig/OpenSSLConf",
  81. "org.apache.tomcat.util.net.openssl.OpenSSLConf");
  82. digester.addSetProperties("Server/Service/Connector/SSLHostConfig/OpenSSLConf");
  83. digester.addSetNext("Server/Service/Connector/SSLHostConfig/OpenSSLConf",
  84. "setOpenSslConf",
  85. "org.apache.tomcat.util.net.openssl.OpenSSLConf");
  86. digester.addObjectCreate("Server/Service/Connector/SSLHostConfig/OpenSSLConf/OpenSSLConfCmd",
  87. "org.apache.tomcat.util.net.openssl.OpenSSLConfCmd");
  88. digester.addSetProperties("Server/Service/Connector/SSLHostConfig/OpenSSLConf/OpenSSLConfCmd");
  89. digester.addSetNext("Server/Service/Connector/SSLHostConfig/OpenSSLConf/OpenSSLConfCmd",
  90. "addCmd",
  91. "org.apache.tomcat.util.net.openssl.OpenSSLConfCmd");
  92. digester.addObjectCreate("Server/Service/Connector/Listener",
  93. null, // MUST be specified in the element
  94. "className");
  95. digester.addSetProperties("Server/Service/Connector/Listener");
  96. digester.addSetNext("Server/Service/Connector/Listener",
  97. "addLifecycleListener",
  98. "org.apache.catalina.LifecycleListener");
  99. digester.addObjectCreate("Server/Service/Connector/UpgradeProtocol",
  100. null, // MUST be specified in the element
  101. "className");
  102. digester.addSetProperties("Server/Service/Connector/UpgradeProtocol");
  103. digester.addSetNext("Server/Service/Connector/UpgradeProtocol",
  104. "addUpgradeProtocol",
  105. "org.apache.coyote.UpgradeProtocol");
  106. // Add RuleSets for nested elements
  107. digester.addRuleSet(new NamingRuleSet("Server/GlobalNamingResources/"));
  108. digester.addRuleSet(new EngineRuleSet("Server/Service/"));
  109. digester.addRuleSet(new HostRuleSet("Server/Service/Engine/"));
  110. digester.addRuleSet(new ContextRuleSet("Server/Service/Engine/Host/"));
  111. addClusterRuleSet(digester, "Server/Service/Engine/Host/Cluster/");
  112. digester.addRuleSet(new NamingRuleSet("Server/Service/Engine/Host/Context/"));
  113. // When the 'engine' is found, set the parentClassLoader.
  114. digester.addRule("Server/Service/Engine",
  115. new SetParentClassLoaderRule(parentClassLoader));
  116. addClusterRuleSet(digester, "Server/Service/Engine/Cluster/");
  117. long t2=System.currentTimeMillis();
  118. if (log.isDebugEnabled()) {
  119. log.debug("Digester for server.xml created " + ( t2-t1 ));
  120. }
  121. return digester;
  122. }

5&6、

时序图
image.png

源码:
image.png
image.png
image.png

7、

image.png
image.png

8、

时序图:
image.png
源码:
点进services[i].init(); 也会出现7的步骤。
image.png
image.png

9、

image.png

12、

image.png
image.png

13、

点击protocolHandler.init();进去
image.png
image.png
image.png
点击进去endpoint.init();
image.png
点击进去bind();
image.png
image.png

Tomcat启动部分

14、

image.png

15、

Catalina.start启动
image.png

16、

image.png

17、

点进getServer().start();
image.png
image.png

image.png

image.png

18&21&22、

点进services[i].start();
image.png
image.png
image.png

image.png

19、

点进engine.start();
image.png
image.png
image.png

23、

image.png
image.png
image.png

image.png
protocolHandler是一个组合组件

点进去protocolHandler.start();
image.png

点进endpoint.start();

image.png
image.png

image.png

点进去startAcceptorThreads();
image.png
image.png
image.png
点进return new Acceptor();
image.png

四、Debug断点追踪看源码流程

1、打断点

image.png

image.png

image.png

image.png

image.png
image.png

后续的流程和点击查看的差不太多。