网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringBoot深入理解之内置web容器及配置的总结

前言

目前成都创新互联已为千余家的企业提供了网站建设、域名、虚拟空间、网站托管、服务器租用、企业网站设计、宜兴网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

在学会基本运用SpringBoot同时,想必搭过SSH、SSM等开发框架的小伙伴都有疑惑,SpringBoot在spring的基础上做了些什么,使得使用SpringBoot搭建开发框架能如此简单,便捷,快速。本系列文章记录网罗博客、分析源码、结合微薄经验后的总结,以便日后翻阅自省。

正文

使用SpringBoot时,首先引人注意的便是其启动方式,我们熟知的web项目都是需要部署到服务容器上,例如tomcat、weblogic、widefly(以前叫JBoss),然后启动web容器真正运行我们的系统。而SpringBoot搭建的系统却是运行***Application.class中的main方法启动。这是为什么?

原因是SpringBoot除了高度集成封装了Spring一系列框架之外,还封装了web容器,SpringBoot启动时会根据配置启动相应的上下文环境,查看EmbeddedServletContainerAutoConfiguration源码可知(这里SpringBoot启动过程会单独总结分析),如下。

@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"?>

  4.0.0
  
    org.springframework.boot
    spring-boot-starters
    1.5.8.RELEASE
  
  spring-boot-starter-web
  Spring Boot Web Starter
  Starter for building web, including RESTful, applications using Spring
    MVC. Uses Tomcat as the default embedded container
  http://projects.spring.io/spring-boot/
  
    Pivotal Software, Inc.
    http://www.spring.io
  
  
    ${basedir}/../..
  
  
    
      org.springframework.boot
      spring-boot-starter
    
    
      org.springframework.boot
      spring-boot-starter-tomcat
    
    ...
    ...

若我们使用其他容器,该如何配置,例如该篇文章Tomcat vs. Jetty vs. Undertow: Comparison of Spring Boot Embedded Servlet Containers详细比较了SpringBoot中三种容器的性能、稳定性等,结果证明了Undertow在性能和内存使用上是最好的。

显然,更换内置容器,能提高SpringBoot项目的性能,由于SpringBoot插拔式的模块设计,配置Undertow只需要两步,如下。

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


  org.springframework.boot
  spring-boot-starter-web
  
    
      org.springframework.boot
      spring-boot-starter-tomcat
    
  


  org.springframework.boot
  spring-boot-starter-undertow

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.

其余对容器的更多配置,调优等等不作介绍,可以自行百度Undertow。

到这里,肯定会有很多人有疑惑,非得用SpringBoot集成的容器作为运行环境吗?答案是:NO! SpringBoot同样提供了像往常一样打war包部署的解决方案。

1.将项目的启动类Application.java继承SpringBootServletInitializer并重写configure方法。

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

war

3.在pom.xml文件中,去除tomcat依赖,或者将其标记为provided(打包时排除),provided方式有一点好处是调试是可以用内置tomcat。


    org.springframework.boot
    spring-boot-starter-tomcat
    provided

至此,以上3个配置便可以完成war方式部署,注意war包部署后访问时需要加上项目名称。

最后,对比传统应用容器和springboot容器架构图。

传统应用容器:

SpringBoot深入理解之内置web容器及配置的总结

springboot容器:

SpringBoot深入理解之内置web容器及配置的总结

SpringBoot这种设计在微服务架构下有明显的优点:

  • 可以创建独立、自启动的应用容器
  • 不需要构建War包并发布到容器中,构建和维护War包、容器的配置和管理也是需要成本和精力的
  • 通过Maven的定制化标签,可以快速创建SpringBoot的应用程序
  • 可以最大化地自动化配置Spring,而不需要人工配置各项参数
  • 提供了产品化特点,例如:性能分析、健康检查和外部化配置
  • 全程没有XML配置,也不需要代码生成

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对创新互联的支持。如果你想了解更多相关内容请查看下面相关链接


当前标题:SpringBoot深入理解之内置web容器及配置的总结
链接URL:http://bjjierui.cn/article/ihjcji.html

其他资讯