前言
在學會基本運用springboot同時,想必搭過ssh、ssm等開發框架的小伙伴都有疑惑,springboot在spring的基礎上做了些什么,使得使用springboot搭建開發框架能如此簡單,便捷,快速。本系列文章記錄網羅博客、分析源碼、結合微薄經驗后的總結,以便日后翻閱自省。
正文
使用springboot時,首先引人注意的便是其啟動方式,我們熟知的web項目都是需要部署到服務容器上,例如tomcat、weblogic、widefly(以前叫jboss),然后啟動web容器真正運行我們的系統。而springboot搭建的系統卻是運行***application.class中的main方法啟動。這是為什么?
原因是springboot除了高度集成封裝了spring一系列框架之外,還封裝了web容器,springboot啟動時會根據配置啟動相應的上下文環境,查看embeddedservletcontainerautoconfiguration源碼可知(這里springboot啟動過程會單獨總結分析),如下。
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配置判斷,默認為tomcat @conditionalonmissingbean ( value = {embeddedservletcontainerfactory. class }, search = searchstrategy.current ) public static class embeddedtomcat { public embeddedtomcat() { } @bean public tomcatembeddedservletcontainerfactory tomcatembeddedservletcontainerfactory() { return new tomcatembeddedservletcontainerfactory(); } } } |
該自動配置類表明springboot支持封裝tomcat、jetty和undertow三種web容器,查看spring-boot-starter-web的pom.xml(如下),其默認配置為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詳細比較了springboot中三種容器的性能、穩定性等,結果證明了undertow在性能和內存使用上是最好的。
顯然,更換內置容器,能提高springboot項目的性能,由于springboot插拔式的模塊設計,配置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. |
其余對容器的更多配置,調優等等不作介紹,可以自行百度undertow。
到這里,肯定會有很多人有疑惑,非得用springboot集成的容器作為運行環境嗎?答案是:no! springboot同樣提供了像往常一樣打war包部署的解決方案。
1.將項目的啟動類application.java繼承springbootservletinitializer并重寫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 >標簽下面添加war包支持的< package >標簽,或者將原標簽值jar改成war。
1
|
<packaging>war</packaging> |
3.在pom.xml文件中,去除tomcat依賴,或者將其標記為provided(打包時排除),provided方式有一點好處是調試是可以用內置tomcat。
1
2
3
4
5
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> <scope>provided</scope> </dependency> |
至此,以上3個配置便可以完成war方式部署,注意war包部署后訪問時需要加上項目名稱。
最后,對比傳統應用容器和springboot容器架構圖。
傳統應用容器:
springboot容器:
springboot這種設計在微服務架構下有明顯的優點:
- 可以創建獨立、自啟動的應用容器
- 不需要構建war包并發布到容器中,構建和維護war包、容器的配置和管理也是需要成本和精力的
- 通過maven的定制化標簽,可以快速創建springboot的應用程序
- 可以最大化地自動化配置spring,而不需要人工配置各項參數
- 提供了產品化特點,例如:性能分析、健康檢查和外部化配置
- 全程沒有xml配置,也不需要代碼生成
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/u011961421/article/details/79732924