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容器
spring-boot-starter-web依赖spring-boot-starter-tomcat依赖tomcat-embed-core等
支持Tomcat、Jetty、Undertow三种嵌入式Servlet容器
默认Tomcat作为嵌入式Servlet Web 容器
嵌入式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
所以传统Servlet容器将压缩的WAR文件解压到对应目录,再加载该目录中的资源。而Spring Boot可执行WAR文件则需要在不解压当前WAR文件的前提下读取其中的资源
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>
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>
其中
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依赖即可
Tomcat作为嵌入式Reactive Web容器
更改pom文件删除spring-boot-starter-web依赖,增加spring-boot-starter-flux依赖和tomcat依赖即可
自动装配
以上均已spring-boot-satrter-作为前缀。
当这些starter添加到应用的Class Path中后,其关联的特性随着应用的启动而自动装载,称为“Automatically configure”即自动装配
Comments | 0 条评论