替换掉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)

相关推荐
XMYX-01 小时前
Spring Boot + Prometheus 实现应用监控(基于 Actuator 和 Micrometer)
spring boot·后端·prometheus
@yanyu6663 小时前
springboot实现查询学生
java·spring boot·后端
酷爱码3 小时前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
AI小智4 小时前
Google刀刃向内,开源“深度研究Agent”:Gemini 2.5 + LangGraph 打造搜索终结者!
后端
java干货4 小时前
虚拟线程与消息队列:Spring Boot 3.5 中异步架构的演进与选择
spring boot·后端·架构
一只叫煤球的猫4 小时前
MySQL 8.0 SQL优化黑科技,面试官都不一定知道!
后端·sql·mysql
写bug写bug5 小时前
如何正确地对接口进行防御式编程
java·后端·代码规范
不超限5 小时前
Asp.net core 使用EntityFrame Work
后端·asp.net
豌豆花下猫5 小时前
Python 潮流周刊#105:Dify突破10万星、2025全栈开发的最佳实践
后端·python·ai