SpringBoot集成Tomcat服務(wù)架構(gòu)配置
一、Tomcat集成
使用的成本越低,內(nèi)部封裝越復(fù)雜;
1、依賴(lài)層級(jí)
在SpringBoot框架的web依賴(lài)包中,引入的是內(nèi)嵌Tomcat組件,基于SpringBoot的版本,Tomcat集成的是9.0版本;
<!-- 1、項(xiàng)目工程依賴(lài) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.5.RELEASE</version> </dependency> <!-- 2、starter-web依賴(lài) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.2.5.RELEASE</version> <scope>compile</scope> </dependency> <!-- 3、starter-tomcat依賴(lài) --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>9.0.31</version> <scope>compile</scope> </dependency>
2、自動(dòng)化配置
在SpringBoot框架的自動(dòng)配置類(lèi)中,Web項(xiàng)目中不顯式更換其他服務(wù)依賴(lài)時(shí),默認(rèn)提供了對(duì)Tomcat服務(wù)的管理;
@ConditionalOnWebApplication(type = Type.SERVLET) @EnableConfigurationProperties(ServerProperties.class) @Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, ServletWebServerFactoryConfiguration.EmbeddedTomcat.class}) public class ServletWebServerFactoryAutoConfiguration { @Bean @ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat") public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer( ServerProperties serverProperties) { return new TomcatServletWebServerFactoryCustomizer(serverProperties); } }
二、Tomcat架構(gòu)
Server:代表整個(gè)Tomcat容器;
Service:服務(wù)器內(nèi)部的中間組件,將一個(gè)或多個(gè)Connector綁定到一個(gè)Engine上;
Engine:表示特定服務(wù)的請(qǐng)求處理管道,接收Connector的請(qǐng)求并響應(yīng);
Host:網(wǎng)絡(luò)主機(jī)名稱(chēng);
Connector:連接器處理與客戶(hù)端的通信;
Context:代表一個(gè)Web應(yīng)用程序的上下文;
參考Tomcat9.0版本的核心組件描述,對(duì)于框架有大致的了解后,再去分析集成原理,會(huì)更容易把握主線邏輯;
三、Tomcat配置
1、基礎(chǔ)配置
在配置文件中,對(duì)Tomcat做一些基礎(chǔ)性的設(shè)置,查看下面的配置類(lèi)可以知道,這些屬性存在默認(rèn)值;
server: port: 8082 # 端口號(hào) tomcat: # Tomcat組件 uri-encoding: UTF-8 # URI編碼 max-threads: 100 # 最大工作線程 min-spare-threads: 10 # 最小工作線程
2、屬性配置類(lèi)
在服務(wù)配置中,提供多種服務(wù)器的適配,像Tomcat、Jetty、Netty、Undertow,從策略上看,配置分為公共屬性以及各種服務(wù)器的適配屬性;
更多配置信息,可以參考完整的源碼和注釋說(shuō)明;
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties { private Integer port; public static class Tomcat { private Charset uriEncoding = StandardCharsets.UTF_8; private int maxThreads = 200; private int minSpareThreads = 10; } }
3、配置加載分析
- 基于配置的屬性,定制化管理Tomcat服務(wù)的信息;
public class TomcatWebServerFactoryCustomizer implements WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory> { @Override public void customize(ConfigurableTomcatWebServerFactory factory) { ServerProperties properties = this.serverProperties; ServerProperties.Tomcat tomcatProperties = properties.getTomcat(); PropertyMapper propertyMapper = PropertyMapper.get(); customizeStaticResources(factory); } }
- TomcatWeb服務(wù)工廠,這里在創(chuàng)建WebServer時(shí),使用的是Tomcat,需要適當(dāng)?shù)牧私庖幌耇omcat架構(gòu);
public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory implements ConfigurableTomcatWebServerFactory, ResourceLoaderAware { @Override public WebServer getWebServer(ServletContextInitializer... initializers) { Tomcat tomcat = new Tomcat(); Connector connector = new Connector(this.protocol); connector.setThrowOnFailure(true); tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(false); configureEngine(tomcat.getEngine()); prepareContext(tomcat.getHost(), initializers); return getTomcatWebServer(tomcat); } }
四、周期管理方法
1、控制類(lèi)
- WebServer的簡(jiǎn)單接口,只聲明端口獲取,服務(wù)啟動(dòng)和停止相關(guān)方法;
public interface WebServer { // 獲取監(jiān)聽(tīng)的端口 int getPort(); // 服務(wù)啟動(dòng) void start() throws WebServerException; // 服務(wù)停止 void stop() throws WebServerException; }
- SpringBoot中,Tomcat服務(wù)核心控制類(lèi),通過(guò)TomcatServletWebServerFactory工廠類(lèi)創(chuàng)建,對(duì)Tomcat生命周期的管理提供了一層包裝;
public class TomcatWebServer implements WebServer { private final Tomcat tomcat; private final Map<Service, Connector[]> serviceConnectors = new HashMap<>(); }
- Apache組件中,輕量級(jí)Tomcat啟動(dòng)器,提供了Tomcat基礎(chǔ)配置,比如默認(rèn)的Port和HostName,以及生命周期管理的方法,TomcatWebServer類(lèi)中調(diào)用的就是該API中的具體方法;
public class Tomcat { protected Server server; protected int port = 8080; protected String hostname = "localhost"; // 初始化服務(wù) public void init() throws LifecycleException { getServer(); server.init(); } // 啟動(dòng)服務(wù) public void start() throws LifecycleException { getServer(); server.start(); } // 停止服務(wù) public void stop() throws LifecycleException { getServer(); server.stop(); } }
2、核心方法
2.1 初始化,初始化時(shí),調(diào)用Apache-Tomcat類(lèi)中啟動(dòng)方法;
public class TomcatWebServer implements WebServer { /** * 初始化方法 */ private void initialize() throws WebServerException { // 控制臺(tái)日志 logger.info("Tomcat initialized with port(s): " + getPortsDescription(false)); synchronized (this.monitor) { // 調(diào)用Apache-Tomcat類(lèi)中啟動(dòng)方法 this.tomcat.start(); } } }
2.2 啟動(dòng),在初始化的方法中,調(diào)用的Tomcat啟動(dòng)方法,這里對(duì)狀態(tài)進(jìn)行校驗(yàn)并輸出日志;
public class TomcatWebServer implements WebServer { /** * 啟動(dòng)方法 */ public void start() throws WebServerException { synchronized (this.monitor) { if (this.started) { return; } checkThatConnectorsHaveStarted(); // 啟動(dòng)狀態(tài)的標(biāo)識(shí) this.started = true; // 控制臺(tái)日志 logger.info("Tomcat started on port(s): " + getPortsDescription(true) + " with context path '" + getContextPath() + "'"); } } }
2.3 停止,在組件生命周期的常規(guī)管理邏輯中,停止服務(wù)之后進(jìn)行銷(xiāo)毀動(dòng)作的執(zhí)行,其中自然涉及到多個(gè)狀態(tài)標(biāo)識(shí)的轉(zhuǎn)換;
public class TomcatWebServer implements WebServer { /** * 停止方法 */ public void stop() throws WebServerException { synchronized (this.monitor) { // 狀態(tài)變化 boolean wasStarted = this.started; this.started = false; // Tomcat服務(wù)停止 stopTomcat(); this.tomcat.destroy(); } } }
參考源碼
編程文檔:
https://gitee.com/cicadasmile/butte-java-note
應(yīng)用倉(cāng)庫(kù):
https://gitee.com/cicadasmile/butte-flyer-parent
以上就是SpringBoot集成Tomcat服務(wù)架構(gòu)配置的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot集成Tomcat的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springmvc+kindeditor文件上傳實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了springmvc+kindeditor文件上傳實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08SpringBoot內(nèi)置tomcat啟動(dòng)原理詳解
這篇文章主要介紹了SpringBoot內(nèi)置tomcat啟動(dòng)原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04uploadify java實(shí)現(xiàn)多文件上傳和預(yù)覽
這篇文章主要為大家詳細(xì)介紹了java結(jié)合uploadify實(shí)現(xiàn)多文件上傳和預(yù)覽的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Java實(shí)現(xiàn)自定義自旋鎖代碼實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)自定義自旋鎖代碼實(shí)例,Java自旋鎖是一種線程同步機(jī)制,它允許線程在獲取鎖時(shí)不立即阻塞,而是通過(guò)循環(huán)不斷嘗試獲取鎖,直到成功獲取為止,自旋鎖適用于鎖競(jìng)爭(zhēng)激烈但持有鎖的時(shí)間很短的情況,需要的朋友可以參考下2023-10-10