温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-25 12:46:04 来源:脚本之家 阅读:220 作者:Super_PF 栏目:编程语言

前言

在学会基本运用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"?>
<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依赖。

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

其余对容器的更多配置,调优等等不作介绍,可以自行百度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。

<packaging>war</packaging>

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

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

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

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

传统应用容器:

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

springboot容器:

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

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

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

总结

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI