時間:2023-07-15 05:30:01 | 來源:網(wǎng)站運(yùn)營
時間:2023-07-15 05:30:01 來源:網(wǎng)站運(yùn)營
Tomcat 的工作原理萬字總結(jié),這回終于搞懂了!:SpringBoot 就像一條巨蟒,慢慢纏繞著我們,使我們麻痹。不得不承認(rèn),使用了 SpringBoot 確實(shí)提高了工作效率,但同時也讓我們遺忘了很多技能。剛?cè)肷鐣臅r候,我還是通過 Tomcat 手動部署 JavaWeb 項(xiàng)目,還經(jīng)常對 Tomcat 進(jìn)行性能調(diào)優(yōu)。除此之外,還需要自己理清楚各 Jar 之間的關(guān)系,以避免 Jar 丟失和各版本沖突導(dǎo)致服務(wù)啟動異常的問題。到如今,這些繁瑣而又重復(fù)的工作已經(jīng)統(tǒng)統(tǒng)交給 SpringBoot 處理,我們可以把更多的精力放在業(yè)務(wù)邏輯上。但是,清楚 Tomcat 的工作原理和處理請求流程和分析 Spring 框架源碼一樣的重要。至少面試官特別喜歡問這些底層原理和設(shè)計(jì)思路。希望這篇文章能給你一些幫助。
org.apache.coyote
。對應(yīng)的結(jié)構(gòu)圖如下org.apache.coyote
。對應(yīng)的結(jié)構(gòu)圖如下<?xml version="1.0" encoding="UTF-8"?><Server port="8005" shutdown="SHUTDOWN"> <Service name="Catalina"> <!-- 連接器監(jiān)聽端口是 8080,默認(rèn)通訊協(xié)議是 HTTP/1.1 --> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- 名字為 Catalina 的引擎,其默認(rèn)的虛擬主機(jī)是 localhost --> <Engine name="Catalina" defaultHost="localhost"> <!-- 名字為 localhost 的虛擬主機(jī),其目錄是 webapps--> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> </Host> </Engine> </Service></Server>
SpringApplication.run
從 run 方法點(diǎn)進(jìn)去,找到刷新應(yīng)用上下文的方法。this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);this.refreshContext(context);this.afterRefresh(context, applicationArguments);
從 refreshContext 方法點(diǎn)進(jìn)去,找 refresh 方法。并一層層往上找其父類的方法。this.refresh(context);
在 AbstractApplicationContext 類的 refresh 方法中,有一行調(diào)用子容器刷新的邏輯。this.postProcessBeanFactory(beanFactory);this.invokeBeanFactoryPostProcessors(beanFactory);this.registerBeanPostProcessors(beanFactory);this.initMessageSource();this.initApplicationEventMulticaster();this.onRefresh();this.registerListeners();this.finishBeanFactoryInitialization(beanFactory);this.finishRefresh();
從 onRefresh 方法點(diǎn)進(jìn)去,找到 ServletWebServerApplicationContext 的實(shí)現(xiàn)方法。在這里終于看到了希望。protected void onRefresh() { super.onRefresh(); try { this.createWebServer(); } catch (Throwable var2) { throw new ApplicationContextException("Unable to start web server", var2); }}
從 createWebServer 方法點(diǎn)進(jìn)去,找到從工廠類中獲取 WebServer的代碼。if (webServer == null && servletContext == null) { ServletWebServerFactory factory = this.getWebServerFactory(); // 獲取 web server this.webServer = factory.getWebServer(new ServletContextInitializer[]{this.getSelfInitializer()});} else if (servletContext != null) { try { // 啟動 web server this.getSelfInitializer().onStartup(servletContext); } catch (ServletException var4) { throw new ApplicationContextException("Cannot initialize servlet context", var4); }}
從 getWebServer 方法點(diǎn)進(jìn)去,找到 TomcatServletWebServerFactory 的實(shí)現(xiàn)方法,與之對應(yīng)的還有 Jetty 和 Undertow。這里配置了基本的連接器、引擎、虛擬站點(diǎn)等配置。public WebServer getWebServer(ServletContextInitializer... initializers) { Tomcat tomcat = new Tomcat(); File baseDir = this.baseDirectory != null ? this.baseDirectory : this.createTempDir("tomcat"); tomcat.setBaseDir(baseDir.getAbsolutePath()); Connector connector = new Connector(this.protocol); tomcat.getService().addConnector(connector); this.customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(false); this.configureEngine(tomcat.getEngine()); Iterator var5 = this.additionalTomcatConnectors.iterator(); while(var5.hasNext()) { Connector additionalConnector = (Connector)var5.next(); tomcat.getService().addConnector(additionalConnector); } this.prepareContext(tomcat.getHost(), initializers); return this.getTomcatWebServer(tomcat);}
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8900 (http)o.apache.catalina.core.StandardService : Starting service [Tomcat]org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal ...o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContexto.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 16858 ms
作者:ITDragon龍
鏈接:https://www.cnblogs.com/itdragon/p/13657104.html
來源:cnblogs
作者:zxl_Dragon最后,照舊安利一波我們的公眾號:「終端研發(fā)部」,目前每天都會推薦一篇優(yōu)質(zhì)的技術(shù)相關(guān)的文章,主要分享java相關(guān)的技術(shù)與面試技巧,我們的目標(biāo)是: 知道是什么,為什么,打好基礎(chǔ),做好每一點(diǎn)!這個主創(chuàng)技術(shù)公眾號超級值得大家關(guān)注。
原文鏈接:https://blog.csdn.net/zxl_Dragon/article/details/83584667
來源:csdn
關(guān)鍵詞:總結(jié),工作,原理
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。