国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié)

2024-07-14 08:43:43
字體:
供稿:網(wǎng)友

前言

在學(xué)會基本運用SpringBoot同時,想必搭過SSH、SSM等開發(fā)框架的小伙伴都有疑惑,SpringBoot在spring的基礎(chǔ)上做了些什么,使得使用SpringBoot搭建開發(fā)框架能如此簡單,便捷,快速。本系列文章記錄網(wǎng)羅博客、分析源碼、結(jié)合微薄經(jīng)驗后的總結(jié),以便日后翻閱自省。

正文

使用SpringBoot時,首先引人注意的便是其啟動方式,我們熟知的web項目都是需要部署到服務(wù)容器上,例如tomcat、weblogic、widefly(以前叫JBoss),然后啟動web容器真正運行我們的系統(tǒng)。而SpringBoot搭建的系統(tǒng)卻是運行***Application.class中的main方法啟動。這是為什么?

原因是SpringBoot除了高度集成封裝了Spring一系列框架之外,還封裝了web容器,SpringBoot啟動時會根據(jù)配置啟動相應(yīng)的上下文環(huán)境,查看EmbeddedServletContainerAutoConfiguration源碼可知(這里SpringBoot啟動過程會單獨總結(jié)分析),如下。

@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。

<?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中三種容器的性能、穩(wěn)定性等,結(jié)果證明了Undertow在性能和內(nèi)存使用上是最好的。

顯然,更換內(nèi)置容器,能提高SpringBoot項目的性能,由于SpringBoot插拔式的模塊設(shè)計,配置Undertow只需要兩步,如下。

1.第一步,去除原容器依賴,加入Undertow依賴。

<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。

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.

其余對容器的更多配置,調(diào)優(yōu)等等不作介紹,可以自行百度Undertow。

到這里,肯定會有很多人有疑惑,非得用SpringBoot集成的容器作為運行環(huán)境嗎?答案是:NO! SpringBoot同樣提供了像往常一樣打war包部署的解決方案。

1.將項目的啟動類Application.java繼承SpringBootServletInitializer并重寫configure方法。

@SpringBootApplicationpublic 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。

<packaging>war</packaging>

3.在pom.xml文件中,去除tomcat依賴,或者將其標(biāo)記為provided(打包時排除),provided方式有一點好處是調(diào)試是可以用內(nèi)置tomcat。

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-tomcat</artifactId>    <scope>provided</scope></dependency>

至此,以上3個配置便可以完成war方式部署,注意war包部署后訪問時需要加上項目名稱。

最后,對比傳統(tǒng)應(yīng)用容器和springboot容器架構(gòu)圖。

傳統(tǒng)應(yīng)用容器:

SpringBoot,web容器,配置

springboot容器:

SpringBoot,web容器,配置

SpringBoot這種設(shè)計在微服務(wù)架構(gòu)下有明顯的優(yōu)點:

  • 可以創(chuàng)建獨立、自啟動的應(yīng)用容器
  • 不需要構(gòu)建War包并發(fā)布到容器中,構(gòu)建和維護War包、容器的配置和管理也是需要成本和精力的
  • 通過Maven的定制化標(biāo)簽,可以快速創(chuàng)建SpringBoot的應(yīng)用程序
  • 可以最大化地自動化配置Spring,而不需要人工配置各項參數(shù)
  • 提供了產(chǎn)品化特點,例如:性能分析、健康檢查和外部化配置
  • 全程沒有XML配置,也不需要代碼生成

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 余江县| 安达市| 江津市| 贺兰县| 金华市| 广宁县| 开封县| 阜平县| 邓州市| 遂平县| 晋中市| 崇仁县| 桂东县| 海盐县| 集安市| 长春市| 临江市| 宜川县| 那坡县| 吉林省| 阳原县| 重庆市| 仁化县| 天气| 宜春市| 宝鸡市| 乌兰察布市| 唐海县| 北辰区| 法库县| 阜康市| 黔西| 彭州市| 延津县| 滦平县| 汉川市| 邵阳市| 文成县| 梧州市| 梅州市| 增城市|