SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)_第1頁(yè)
SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)_第2頁(yè)
SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)_第3頁(yè)
SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)_第4頁(yè)
SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)目錄如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)SpringBoot默認(rèn)使用的內(nèi)置Servlet容器是TomcatSpringBoot還支持Jetty和UndertowSpringBootweb開發(fā)_嵌入式Servlet容器1、切換嵌入式Servlet容器2、定制Servlet容器

如何切換成其它的嵌入式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容器的依賴即可:

切換成Jetty:

!--引入Web模塊--

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

exclusions

!--排除tomcat容器--

exclusion

artifactIdspring-boot-starter-tomcat/artifactId

groupIdorg.springframework.boot/groupId

/exclusion

/exclusions

/dependency

!--引入其它的Servlet容器--

dependency

artifactIdspring-boot-starter-jetty/artifactId

groupIdorg.springframework.boot/groupId

/dependency

Jetty啟動(dòng)成功

同理切換成undertow:

!--引入Web模塊--

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

exclusions

!--排除tomcat容器--

exclusion

artifactIdspring-boot-starter-tomcat/artifactId

groupIdorg.springframework.boot/groupId

/exclusion

/exclusions

/dependency

!--引入其它的Servlet容器--

dependency

artifactIdspring-boot-starter-undertow/artifactId

groupIdorg.springframework.boot/groupId

/dependency

Undertow啟動(dòng)成功

SpringBootweb開發(fā)_嵌入式Servlet容器

1、切換嵌入式Servlet容器

1.1、SpringBoot支持的Servlet種類及其切換

1)默認(rèn)支持的webServer:Tomcat、Jrtty、Undertow

2)切換服務(wù)器

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

exclusions

exclusion

groupIdorg.springframework.boot/groupId

artifactIdspring-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ū)ふ襍ervletWebServerFactory(Servlet的web服務(wù)器工廠Servlet的web服務(wù)器)

4)SpringBoot底層默認(rèn)有很多的WebServer工廠;

TomcatServletWebServerFactoryJettyServletWebServerFactoryUndertowServletWebServerFactory

5)底層直接會(huì)有一個(gè)自動(dòng)配置類。

ServletWebServerFactoryAutoConfiguration導(dǎo)入了ServletWebServerFactoryConfiguration(配置類)

6)ServletWebServerFactoryConfiguration配置類根據(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)造器擁有初始化方法initializethis.tomcat.start();

8)內(nèi)嵌服務(wù)器,就是手動(dòng)把啟動(dòng)服務(wù)器的代碼調(diào)用(tomcat核心jar包存在)

2、定制Servlet容器

2.1、修改配置文件

通過(guò)對(duì)perties配置文件的設(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):

importorg.springframework.boot.web.server.WebServerFactoryCustomizer;

importorg.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;

importorg.springframework.stereotype.Component;

@Component

publicclassCustomizationBeanimplementsWebServerFactoryCustomizerConfigurableServletWebServerFactory{

@Override

publicvoidcustomize(ConfigurableServletWebServerFactoryserver){

server.setPort(9000);

}

2.3、ConfigurableServletWebServerFactory

直接自定義ConfigurableServletWebServerFactory的接口實(shí)現(xiàn)類

publicinterfaceConfigurableWebServerFactoryextendsWebServerFactory,ErrorPageRegistry{

voidsetPort(intport);

voidsetAddress(InetAddressaddress);

voidsetErrorPages(SetextendsErrorPageerrorPages);

voidsetSsl(Sslssl);

voidsetSslStoreProvider(SslStoreProvidersslStoreProvider);

voidsetHttp2(Http2http2);

voidsetCompression(Compressioncompression);

voidsetServerHeader(StringserverHeader);

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論