阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

固化的Maven依赖和嵌入式Servlet容器

104次阅读
没有评论

共计 3052 个字符,预计需要花费 8 分钟才能阅读完成。

1、理解固化的 Maven 依赖

打开 firts-springboot 应用的 pom.xml 文件,我们可以发现我们在引入依赖时并没有指定版本号。比如引入 spring-boot-starter-web 的时候。

这些版本信息均继承于 spring-boot-starter-parent,降低了 Spring Boot 应用管理依赖的成本。

1.1、查看 spring-boot-starter-parent

重要的部分:

<properties> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <resource.delimiter>@</resource.delimiter> <maven.compiler.source>${java.version}</maven.compiler.source> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>${java.version}</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.6.RELEASE</version> </dependency> </dependencies>

我们发现只处只限定了 spring-boot-maven-plugin 的版本信息,其它均无限定。

1.2、查看 spring-boot-dependencies

部分截图:

固化的 Maven 依赖和嵌入式 Servlet 容器

在 spring-boot-dependencies 中锁定的大量的版本信息

2、嵌入式 Web 容器

2.1、嵌入式 Web 容器概述

Spring Boot 应用直接嵌入 Tomcat、Jetty 和 Undertow 作为其核心特性,可通过指定容器的 Maven 依赖来切换 Spring Boot 应用的嵌入式容器类型,无须代码层面上的调整,不同的嵌入器存在专属的配置属性,自然也不再需要以 WAR 文件进行部署。

2.2、嵌入式 Servlet Web 容器

Spring Boot 支持三种嵌入式 Servlet3.1+ 容器

如表:

Name Servlet Version
Tomcat8.5 3.1
Jetty 9.4 3.1
Undertow 1.4 3.1

2.2.1、Tomcat 作为嵌入式 Servlet Web 容器

Spring Boot 2.0 的实现是它利用嵌入式 Tomcat API 构建为 TomcatWebServer Bean, 由 Spring 应用上下文将其引导,嵌入式 Tomcat 组件的运行,以及 ClassLoader 的装载均由 Spring Boot 框架代码实现。

Spring Boot Maven 插件 spring-boot-maven-plugin 采用零压缩模式,将应用目录归档到 JAR 或 WAR 文件,相当于 jar 命令归档的过程中添加 - 0 参数

案例:使用 jar 命令归档文件

jar 用法: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ... 选项: -c 创建新档案 -t 列出档案目录 -x 从档案中提取指定的 (或所有) 文件 -u 更新现有档案 -v 在标准输出中生成详细输出 -f 指定档案文件名 -m 包含指定清单文件中的清单信息 -n 创建新档案后执行 Pack200 规范化 -e 为捆绑到可执行 jar 文件的独立应用程序 指定应用程序入口点 -0 仅存储; 不使用任何 ZIP 压缩 -P 保留文件名中的前导 '/' (绝对路径) 和 ".." (父目录) 组件 -M 不创建条目的清单文件 -i 为指定的 jar 文件生成索引信息 -C 更改为指定的目录并包含以下文件 如果任何文件为目录, 则对其进行递归处理。清单文件名, 档案文件名和入口点名称的指定顺序 与 'm', 'f' 和 'e' 标记的指定顺序相同。示例 1: 将两个类文件归档到一个名为 classes.jar 的档案中: jar cvf classes.jar Foo.class Bar.class 示例 2: 使用现有的清单文件 'mymanifest' 并 将 foo/ 目录中的所有文件归档到 'classes.jar' 中: jar cvfm classes.jar mymanifest -C foo/ .
jar -0cvf first.jar FirstSpringbootApplication.class

2.2.2、Jetty 作为嵌入式 Servlet Web 容

修改 pom.xml 文件如下:

<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> <!--jetty--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>

经验:

Jetty 可以同时处理大量连接而且可以长时间保持这些连接。例如像一些 web 聊天应用非常适合用 Jetty 做服务器

2.2.3、Undertow 作为嵌入式 Servlet Web 容器

修改 pom.xml 文件如下:

<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> <!--jetty--> <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>--> <!--undertow--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>

经验:

SpringBoot2 中可以将 Web 服务器切换到 Undertow 来提高应用性能

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-06发表,共计3052字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中