SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
SpringBoot默認(rèn)使用的內(nèi)置Servlet容器是Tomcat
然而 SpringBoot還支持其它的Servlet容器 默認(rèn)的Tomcat只是其中一種
SpringBoot還支持Jetty和Undertow
Jetty
適合用于長(zhǎng)鏈接應(yīng)用 例如聊天Undertow
是一個(gè)高性能非阻塞的Servlet容器 并發(fā)性能非常好 但不支持jsp
若要切換 只需要在pom文件中排除自帶的Tomcat啟動(dòng)器 再引入其它Servlet容器的啟動(dòng)器即可
spring-boot-starter-web里面引入了spring-boot-starter-tomcat 因此默認(rèn)使用Tomcat
因此 只需排除原先的Tomcat 再引入要添加的Servlet容器的依賴(lài) 即可:
切換成Jetty:
<!-- 引入Web模塊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 排除tomcat容器 --> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <!-- 引入其它的Servlet容器 --> <dependency> <artifactId>spring-boot-starter-jetty</artifactId> <groupId>org.springframework.boot</groupId> </dependency>
Jetty啟動(dòng)成功
同理 切換成undertow:
<!-- 引入Web模塊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- 排除tomcat容器 --> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <!-- 引入其它的Servlet容器 --> <dependency> <artifactId>spring-boot-starter-undertow</artifactId> <groupId>org.springframework.boot</groupId> </dependency>
Undertow啟動(dòng)成功
SpringBoot web開(kāi)發(fā)_嵌入式Servlet容器
1、切換嵌入式Servlet容器
1.1、SpringBoot支持的Servlet種類(lèi)及其切換
1)默認(rèn)支持的webServer:Tomcat、Jrtty、Undertow
2)切換服務(wù)器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
1.2、原理
1)SpringBoot應(yīng)用啟動(dòng)發(fā)現(xiàn)當(dāng)前是Web應(yīng)用。web場(chǎng)景包-導(dǎo)入tomcat
2)web應(yīng)用會(huì)創(chuàng)建一個(gè)web版的ioc容器 ServletWebServerApplicationContext
3)ServletWebServerApplicationContext 啟動(dòng)的時(shí)候?qū)ふ?ServletWebServerFactory(Servlet 的web服務(wù)器工廠---> Servlet 的web服務(wù)器)
4)SpringBoot底層默認(rèn)有很多的WebServer工廠;
TomcatServletWebServerFactory
JettyServletWebServerFactory
UndertowServletWebServerFactory
5)底層直接會(huì)有一個(gè)自動(dòng)配置類(lèi)。
ServletWebServerFactoryAutoConfiguration導(dǎo)入了ServletWebServerFactoryConfiguration(配置類(lèi))
6)ServletWebServerFactoryConfiguration 配置類(lèi) 根據(jù)動(dòng)態(tài)判斷系統(tǒng)中到底導(dǎo)入了那個(gè)Web服務(wù)器的包。(默認(rèn)是web-starter導(dǎo)入tomcat包),容器中就有 TomcatServletWebServerFactory
7)TomcatServletWebServerFactory 創(chuàng)建出Tomcat服務(wù)器并啟動(dòng);TomcatWebServer 的構(gòu)造器擁有初始化方法initialize---this.tomcat.start();
8)內(nèi)嵌服務(wù)器,就是手動(dòng)把啟動(dòng)服務(wù)器的代碼調(diào)用(tomcat核心jar包存在)
2、定制Servlet容器
2.1、修改配置文件
通過(guò)對(duì)application.properties配置文件的設(shè)置,定制Servlet容器
#修改servlet容器 server.port=8000 #server.undertow.accesslog.dir=/tmp
2.2、實(shí)現(xiàn) WebServerFactoryCustomizer
xxxxxCustomizer:定制化器,可以改變xxxx的默認(rèn)規(guī)則
通過(guò)WebServerFactoryCustomizer修改 Servlet 容器的響應(yīng)端口號(hào):
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.stereotype.Component; @Component public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override public void customize(ConfigurableServletWebServerFactory server) { server.setPort(9000); } }
2.3、ConfigurableServletWebServerFactory
直接自定義 ConfigurableServletWebServerFactory 的接口實(shí)現(xiàn)類(lèi)
public interface ConfigurableWebServerFactory extends WebServerFactory, ErrorPageRegistry { void setPort(int port); void setAddress(InetAddress address); void setErrorPages(Set<? extends ErrorPage> errorPages); void setSsl(Ssl ssl); void setSslStoreProvider(SslStoreProvider sslStoreProvider); void setHttp2(Http2 http2); void setCompression(Compression compression); void setServerHeader(String serverHeader); default void setShutdown(Shutdown shutdown) { } }
ConfigurableServletWebServerFactory 接口實(shí)現(xiàn)類(lèi)(框架中默認(rèn)實(shí)現(xiàn)類(lèi)):
通過(guò)自定義 ConfigurableServletWebServerFactory 接口的實(shí)現(xiàn)類(lèi),即可定制Servlet容器
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot集成WebServlet出現(xiàn)自定義servlet請(qǐng)求失敗的問(wèn)題解決方案
- SpringBoot里使用Servlet進(jìn)行請(qǐng)求的實(shí)現(xiàn)示例
- springboot掃描自定義的servlet和filter代碼詳解
- Springboot注入成員變量HttpServletRequest的原理分析
- SpringBoot3.1.2 引入Swagger報(bào)錯(cuò)Type javax.servlet.http.HttpServletRequest not present解決辦法
- 解決IDEA啟動(dòng)springboot項(xiàng)目報(bào)錯(cuò)java.lang.ClassNotFoundException:?javax.servlet.ServletContext
- SpringBoot獲取HttpServletRequest的3種方式總結(jié)
- Springboot如何添加server.servlet.context-path相關(guān)使用
- SpringBoot項(xiàng)目找不到j(luò)avax.servlet.Filter的問(wèn)題及解決
相關(guān)文章
關(guān)于Nacos和Eureka的區(qū)別及說(shuō)明
這篇文章主要介紹了關(guān)于Nacos和Eureka的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Java高效實(shí)現(xiàn)excel轉(zhuǎn)pdf(支持帶圖片的轉(zhuǎn)換)
這篇文章主要為大家詳細(xì)介紹了如何用java實(shí)現(xiàn)excel轉(zhuǎn)pdf文件,并且支持excel單元格中帶有圖片的轉(zhuǎn)換,文中的示例代碼講解詳細(xì),需要的可以參考下2024-01-01Java 高并發(fā)的三種實(shí)現(xiàn)案例詳解
這篇文章主要介紹了Java 高并發(fā)的三種實(shí)現(xiàn)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Java的Hibernate框架中復(fù)合主鍵映射的創(chuàng)建和使用教程
復(fù)合主鍵映射用起來(lái)比普通的增加主鍵字段要復(fù)雜,這里我們就來(lái)共同學(xué)習(xí)Java的Hibernate框架中復(fù)合主鍵映射的創(chuàng)建和使用教程,需要的朋友可以參考下2016-07-07java實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06如何解決@PutMapping或@PostMapping接收String類(lèi)型參數(shù)多兩個(gè)“引號(hào)問(wèn)題
這篇文章主要介紹了如何解決@PutMapping或@PostMapping接收String類(lèi)型參數(shù)多兩個(gè)“引號(hào)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08頁(yè)面的緩存與不緩存設(shè)置及html頁(yè)面中meta的作用
這篇文章主要介紹了頁(yè)面的緩存與不緩存設(shè)置及html頁(yè)面中meta的作用的相關(guān)資料,需要的朋友可以參考下2016-05-05SpringController返回值和異常自動(dòng)包裝的問(wèn)題小結(jié)
今天遇到一個(gè)需求,在不改動(dòng)原系統(tǒng)代碼的情況下,將Controller的返回值和異常包裝到一個(gè)統(tǒng)一的返回對(duì)象中去,下面通過(guò)本文給大家介紹SpringController返回值和異常自動(dòng)包裝的問(wèn)題,需要的朋友可以參考下2024-03-03Java的@Transactional、@Aysnc、事務(wù)同步問(wèn)題詳解
這篇文章主要介紹了Java的@Transactional、@Aysnc、事務(wù)同步問(wèn)題詳解,現(xiàn)在我們需要在一個(gè)業(yè)務(wù)方法中插入一個(gè)用戶(hù),這個(gè)業(yè)務(wù)方法我們需要加上事務(wù),然后插入用戶(hù)后,我們要異步的方式打印出數(shù)據(jù)庫(kù)中所有存在的用戶(hù),需要的朋友可以參考下2023-11-11