avatar

一条在知识海洋的咸鱼

这个家伙很懒,啥也没有留下😋

  • Linux
  • OCJP
  • Java核心技术卷
  • J2EE相关标准
  • 深入理解Java虚拟机
  • NIO与SOcket编程技术指南
  • Java多线程编程核心技术
  • Redis开发与运维
  • Spring Cloud Alibaba 微服务原理与实践
  • DevOps
  • Docker
  • MySQL必知必会
  • AI自学路线
  • Spring Boot 编程思想(核心篇)
  • 首页
主页 嵌入式Web容器
文章

嵌入式Web容器

发表于 2021-02-24 更新于 24天前
作者 Administrator
13~17 分钟 阅读
  • 嵌入式Servlet Web容器
    • 默认Tomcat作为嵌入式Servlet Web 容器
    • Jetty作为嵌入式Servlet Web容器
    • Undertow作为嵌入式Servlet Web容器
  • 嵌入式Reactive Web容器
    • UndertowWebServer作为嵌入式Reactive Web容器
    • Jetty作为嵌入式Reactive Web容器
    • Tomcat作为嵌入式Reactive Web容器
    • 自动装配

image.png
Spring Boot应用直接嵌入Tomcat、Jetty和Undertow
Spring Boot项目可通过指定容器的Maven依赖来切换Spring Boot应用的嵌入式容器类型,无须代码层面的调整,不同的嵌入式容器存在专属的配置属性,不需要以WAR文件方式进行部署

嵌入式Servlet Web容器和嵌入式Reactive Web容器
Servlet Web容器:Tomcat、Jetty
Reactive Web容器:Netty Web Server

嵌入式Servlet Web容器

image.png
spring-boot-starter-web依赖spring-boot-starter-tomcat依赖tomcat-embed-core等
image.png
支持Tomcat、Jetty、Undertow三种嵌入式Servlet容器

默认Tomcat作为嵌入式Servlet Web 容器

image.png

嵌入式Tomcat作为Web应用的一部分,结合其API实现Servlet容器的引导。
Apache Maven插件官网也提供了Apache Tomcat的Maven插件,与嵌入式Tomcat的差异在于,其不需要编码,也不需要外置Tomcat容器,将当前应用直接打包为可运行的JAR或WAR文件,通过Java -jar命令启动。类似于Spring Boot FAE JAR、FAT WAR。

Spring Boot2.0利用Tomcat API构建为TomcatWebServer Bean,由Spring应用上下文将其引导,其嵌入式Tomcat组件的运行(Context、Connector)及ClassLoader的装在均有Spring Boot框架代码实现

Spring Boot Maven插件spring-boot-maven-plugin采用零压缩模式,将应用目录归档到JAR或WAR文件,相当于jar -0
image.png
所以传统Servlet容器将压缩的WAR文件解压到对应目录,再加载该目录中的资源。而Spring Boot可执行WAR文件则需要在不解压当前WAR文件的前提下读取其中的资源
image.png

Jetty作为嵌入式Servlet Web容器

在spring-boot-starter-web依赖中排除Tomcat相关依赖,再添加Jetty相关依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<!-- Exclude the Tomcat dependency -->
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

image.png

Undertow作为嵌入式Servlet Web容器

同样地排除tomcat,添加undertow依赖即可

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<!-- Exclude the Tomcat dependency -->
				<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>

image.png
其中
o.s.b.w.e.undertow.UndertowWebServer : Undertow started on port(s) 8080 (http)是Reactive Web的实现类

嵌入式Reactive Web容器

webflux是一个完全的reactive并且非阻塞的web框架
Reactive Web容器处于被动激活状态,如果添加了spring-boot-starter-webflux依赖且没有添加spring-boot-starter-web则会被激活。二者同时拥有则不会被激活。

UndertowWebServer作为嵌入式Reactive Web容器

只需要更改pom文件删除spring-boot-starter-web依赖,增加spring-boot-starter-flux依赖和undertow依赖即可

Jetty作为嵌入式Reactive Web容器

更改pom文件删除spring-boot-starter-web依赖,增加spring-boot-starter-flux依赖和jetty依赖即可
image.png

Tomcat作为嵌入式Reactive Web容器

更改pom文件删除spring-boot-starter-web依赖,增加spring-boot-starter-flux依赖和tomcat依赖即可
image.png

自动装配

以上均已spring-boot-satrter-作为前缀。
当这些starter添加到应用的Class Path中后,其关联的特性随着应用的启动而自动装载,称为“Automatically configure”即自动装配

Spring Boot 编程思想(核心篇)
Spring
许可协议: 
分享

相关文章

2月 26, 2021

Spring注解驱动设计模式

Spring #Enable模块驱动@Enable模块驱动Spring #Enable模块驱动从Spring Framework 3.x开始,支持“@Enable模块驱动”模块:具备相同另据的功能组件集合,组合形成的一个独立的单元,如MVC、AspwctJ代理、Async异步处理模块等@Enable

2月 25, 2021

注解驱动编程

注解驱动发展史注解驱动启蒙时代:Spring Framework 1.x注解驱动过渡时代:Spring Framework 2.x注解驱动黄金时代:Spring Framework 3.xSpring Framework的IOC(控制反转)和DI(依赖注入)目的是为了应用系统不应以Java代码的方式

2月 24, 2021

Production-Ready特性

Spring Boot的自动装配及starter的引入,大量的Spring Bean的装配变成黑盒。为了支持以配置化的方式调整应用行为,如Web服务端口等,Spring Boot提供了Production-Ready

下一篇

自动装配

上一篇

固化的Maven依赖

最近更新

  • 28 RAG 第一版交付:从功能拼接到可演示系统
  • 27 对话历史:让多轮 RAG 正确理解追问
  • 26 权限隔离:让无权内容无法进入 RAG 上下文
  • 25 引用来源:让 RAG 答案可以核对和追溯
  • 24 基础 RAG 问答

热门标签

java基础 微服务 maven Spring Tomcat DDD Linux Linux基础 SQL基础 数据结构算法

目录

©2026 一条在知识海洋的咸鱼. 保留部分权利。

使用 Halo 主题 Chirpy