前言
在學(xué)會(huì)基本運(yùn)用springboot同時(shí),想必搭過(guò)ssh、ssm等開(kāi)發(fā)框架的小伙伴都有疑惑,springboot在spring的基礎(chǔ)上做了些什么,使得使用springboot搭建開(kāi)發(fā)框架能如此簡(jiǎn)單,便捷,快速。本系列文章記錄網(wǎng)羅博客、分析源碼、結(jié)合微薄經(jīng)驗(yàn)后的總結(jié),以便日后翻閱自省。
正文
使用springboot時(shí),首先引人注意的便是其啟動(dòng)方式,我們熟知的web項(xiàng)目都是需要部署到服務(wù)容器上,例如tomcat、weblogic、widefly(以前叫jboss),然后啟動(dòng)web容器真正運(yùn)行我們的系統(tǒng)。而springboot搭建的系統(tǒng)卻是運(yùn)行***application.class中的main方法啟動(dòng)。這是為什么?
原因是springboot除了高度集成封裝了spring一系列框架之外,還封裝了web容器,springboot啟動(dòng)時(shí)會(huì)根據(jù)配置啟動(dòng)相應(yīng)的上下文環(huán)境,查看embeddedservletcontainerautoconfiguration源碼可知(這里springboot啟動(dòng)過(guò)程會(huì)單獨(dú)總結(jié)分析),如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
@autoconfigureorder (- 2147483648 ) @configuration @conditionalonwebapplication @import ({embeddedservletcontainerautoconfiguration.beanpostprocessorsregistrar. class }) public class embeddedservletcontainerautoconfiguration { ... ...(中間省略部分) @configuration @conditionalonclass ({servlet. class , undertow. class , sslclientauthmode. class }) //undertow配置判斷 @conditionalonmissingbean ( value = {embeddedservletcontainerfactory. class }, search = searchstrategy.current ) public static class embeddedundertow { public embeddedundertow() { } @bean public undertowembeddedservletcontainerfactory undertowembeddedservletcontainerfactory() { return new undertowembeddedservletcontainerfactory(); } } @configuration @conditionalonclass ({servlet. class , server. class , loader. class , webappcontext. class }) //jetty配置判斷 @conditionalonmissingbean ( value = {embeddedservletcontainerfactory. class }, search = searchstrategy.current ) public static class embeddedjetty { public embeddedjetty() { } @bean public jettyembeddedservletcontainerfactory jettyembeddedservletcontainerfactory() { return new jettyembeddedservletcontainerfactory(); } } @configuration @conditionalonclass ({servlet. class , tomcat. class }) //tomcat配置判斷,默認(rèn)為tomcat @conditionalonmissingbean ( value = {embeddedservletcontainerfactory. class }, search = searchstrategy.current ) public static class embeddedtomcat { public embeddedtomcat() { } @bean public tomcatembeddedservletcontainerfactory tomcatembeddedservletcontainerfactory() { return new tomcatembeddedservletcontainerfactory(); } } } |
該自動(dòng)配置類表明springboot支持封裝tomcat、jetty和undertow三種web容器,查看spring-boot-starter-web的pom.xml(如下),其默認(rèn)配置為tomcat。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starters</artifactid> <version> 1.5 . 8 .release</version> </parent> <artifactid>spring-boot-starter-web</artifactid> <name>spring boot web starter</name> <description>starter for building web, including restful, applications using spring mvc. uses tomcat as the default embedded container</description> <url>http: //projects.spring.io/spring-boot/</url> <organization> <name>pivotal software, inc.</name> <url>http: //www.spring.io</url> </organization> <properties> <main.basedir>${basedir}/../..</main.basedir> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> </dependency> ... ... |
若我們使用其他容器,該如何配置,例如該篇文章Tomcat vs. Jetty vs. Undertow: Comparison of Spring Boot Embedded Servlet Containers詳細(xì)比較了springboot中三種容器的性能、穩(wěn)定性等,結(jié)果證明了undertow在性能和內(nèi)存使用上是最好的。
顯然,更換內(nèi)置容器,能提高springboot項(xiàng)目的性能,由于springboot插拔式的模塊設(shè)計(jì),配置undertow只需要兩步,如下。
1.第一步,去除原容器依賴,加入undertow依賴。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-undertow</artifactid> </dependency> |
2.第二步,在application.yml中配置undertow。
1
2
3
4
5
6
7
8
9
10
11
12
|
server.undertow.accesslog.dir= # undertow access log directory. server.undertow.accesslog.enabled= false # enable access log. server.undertow.accesslog.pattern=common # format pattern for access logs. server.undertow.accesslog.prefix=access_log. # log file name prefix. server.undertow.accesslog.rotate= true # enable access log rotation. server.undertow.accesslog.suffix=log # log file name suffix. server.undertow.buffer-size= # size of each buffer in bytes. server.undertow.buffers-per-region= # number of buffer per region. server.undertow.direct-buffers= # allocate buffers outside the java heap. server.undertow.io-threads= # number of i/o threads to create for the worker. server.undertow.max-http-post-size= 0 # maximum size in bytes of the http post content. server.undertow.worker-threads= # number of worker threads. |
其余對(duì)容器的更多配置,調(diào)優(yōu)等等不作介紹,可以自行百度undertow。
到這里,肯定會(huì)有很多人有疑惑,非得用springboot集成的容器作為運(yùn)行環(huán)境嗎?答案是:no! springboot同樣提供了像往常一樣打war包部署的解決方案。
1.將項(xiàng)目的啟動(dòng)類application.java繼承springbootservletinitializer并重寫(xiě)configure方法。
1
2
3
4
5
6
7
8
9
10
|
@springbootapplication public class application extends springbootservletinitializer { @override protected springapplicationbuilder configure(springapplicationbuilder application) { return application.sources(application. class ); } public static void main(string[] args) throws exception { springapplication.run(application. class , args); } } |
2.在pom.xml文件中,< project >標(biāo)簽下面添加war包支持的< package >標(biāo)簽,或者將原標(biāo)簽值jar改成war。
1
|
<packaging>war</packaging> |
3.在pom.xml文件中,去除tomcat依賴,或者將其標(biāo)記為provided(打包時(shí)排除),provided方式有一點(diǎn)好處是調(diào)試是可以用內(nèi)置tomcat。
1
2
3
4
5
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> <scope>provided</scope> </dependency> |
至此,以上3個(gè)配置便可以完成war方式部署,注意war包部署后訪問(wèn)時(shí)需要加上項(xiàng)目名稱。
最后,對(duì)比傳統(tǒng)應(yīng)用容器和springboot容器架構(gòu)圖。
傳統(tǒng)應(yīng)用容器:
springboot容器:
springboot這種設(shè)計(jì)在微服務(wù)架構(gòu)下有明顯的優(yōu)點(diǎn):
- 可以創(chuàng)建獨(dú)立、自啟動(dòng)的應(yīng)用容器
- 不需要構(gòu)建war包并發(fā)布到容器中,構(gòu)建和維護(hù)war包、容器的配置和管理也是需要成本和精力的
- 通過(guò)maven的定制化標(biāo)簽,可以快速創(chuàng)建springboot的應(yīng)用程序
- 可以最大化地自動(dòng)化配置spring,而不需要人工配置各項(xiàng)參數(shù)
- 提供了產(chǎn)品化特點(diǎn),例如:性能分析、健康檢查和外部化配置
- 全程沒(méi)有xml配置,也不需要代碼生成
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/u011961421/article/details/79732924