替换掉Springboot框架中的Tomcat,使用undertow

替换掉Springboot框架中的Tomcat,使用undertow

1、前言

在SpringBoot框架中,我们使用最多的是Tomcat,这是SpringBoot默认的容器技术,而且是内嵌式的Tomcat。

同时,SpringBoot也支持Undertow容器,我们可以很方便的用Undertow替换Tomcat,而Undertow的性能和内存使用方面都优于Tomcat。

2、四种WEB服务器介绍

2.1、Tomcat

Tomcat是Apache基金下的一个轻量级的Servlet容器,支持Servlet和JSP。Tomcat具有Web服务器特有的功能,包括 Tomcat管理和控制平台、安全局管理和Tomcat阀等。Tomcat本身包含了HTTP服务器,因此也可以视作单独的Web服务器。但是,Tomcat和ApacheHTTP服务器不是一个东西,ApacheHTTP服务器是用C语言实现的HTTP Web服务器。Tomcat是完全免费的,深受开发者的喜爱。

2.2、Undertow

Undertow是Red Hat公司的开源产品, 它完全采用Java语言开发,是一款灵活的高性能Web服务器,支持阻塞IO和非阻塞IO。由于Undertow采用Java语言开发,可以直接嵌入到Java项目中使用。同时, Undertow完全支持Servlet和Web Socket,在高并发情况下表现非常出色。

2.3、Jetty

Jetty: 一个快速、高性能且轻量级的 Servlet 容器和 HTTP 服务器。若要使用 Jetty 替换 Tomcat,只需在项目中排除 Tomcat 并引入 Jetty 的起步依赖 spring-boot-starter-jetty。

2.4、Netty

Netty: 虽然 Netty 不是传统的基于 Servlet 的 Web 容器,但在 Spring Boot 中可以用于构建响应式的 Web 应用,尤其是通过 Reactive Stack(如 WebFlux)时,会使用 Netty 作为底层网络通信层。要使用 Netty,通常引入的是 spring-boot-starter-webflux 依赖。

3、如何替换Tomcat?并使用Undertow

3.1、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>

	<dependency>
		<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-undertow</artifactId>
	</dependency>

替换完成后我们启动项目并查看日志打印:

bash 复制代码
2024-05-09 10:31:30.965  INFO 3456 --- [           main] io.undertow                              : starting server: Undertow - 2.2.20.Final
2024-05-09 10:31:30.968  INFO 3456 --- [           main] org.xnio                                 : XNIO version 3.8.7.Final
2024-05-09 10:31:30.971  INFO 3456 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.8.7.Final
2024-05-09 10:31:30.995  INFO 3456 --- [           main] org.jboss.threads                        : JBoss Threads version 3.1.0.Final
2024-05-09 10:31:31.026  INFO 3456 --- [           main] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port(s) 8080 (http)
2024-05-09 10:31:31.032  INFO 3456 --- [           main] c.example.undertwo.UndertwoApplication   : Started UndertwoApplication in 1.125 seconds (JVM running for 2.583)

4、参考目录

1\]:[【官方】在 Spring Boot 中使用 Undertow 作为嵌入式服务器](https://springdoc.cn/spring-boot-undertow/) \[2\]:[【CSDN优秀文章】 在 Spring Boot 中使用 Undertow 作为嵌入式服务器](https://blog.csdn.net/qq_33204709/article/details/135837835) \[3\]:[【评论区比较有意思】 在 Spring Boot 中使用 Undertow 作为嵌入式服务器](https://zhuanlan.zhihu.com/p/106559336)

相关推荐
苏三说技术7 分钟前
为什么大厂都不推荐在MySQL中使用NULL值?
后端
techdashen11 分钟前
Rust 模块和文件不是一回事:一次讲清 `mod`、`use`、`pub use`
开发语言·后端·rust
爱勇宝25 分钟前
别焦虑,也别躺平:给年轻程序员的一封信
前端·后端·架构
Full Stack Developme40 分钟前
Spring 发展历史
java·后端·spring
ClouGence1 小时前
TiCDC 够用吗?聊聊 TiDB 同步的几个关键问题
数据库·分布式·后端
音符犹如代码1 小时前
Docker 一键部署带有 TimescaleDB 插件的 PostgreSQL
java·运维·数据库·后端·docker·postgresql·容器
LucianaiB1 小时前
从模型护栏到工程门禁:基于 XGuard 二创一个 Agent/CI 动态策略安全护栏
后端
铁皮饭盒2 小时前
同样是算力巨头,为什么华为死磕英伟达,AMD 却 "躺平看戏"?
前端·后端
文心快码BaiduComate2 小时前
用Comate 7天完成”鹅鸭杀”游戏网站开发
前端·后端·程序员
倚栏听风雨2 小时前
Spring AI Alibaba 接入 OpenAI 兼容协议第三方大模型实战
后端