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