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

相关推荐
苏三说技术5 分钟前
推荐几个牛逼的AI Agent项目
后端
polaris06306 分钟前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
卓怡学长21 分钟前
m280本科生导师指导平台
java·数据库·spring·tomcat·maven·intellij-idea
武子康23 分钟前
大数据-253 离线数仓 - Airflow 入门与任务调度实战:DAG、Operator、Executor 部署排错指南
大数据·后端·apache hive
IT_陈寒1 小时前
深入理解JavaScript:核心原理与最佳实践
前端·人工智能·后端
树獭叔叔1 小时前
GRPO:比PPO更简单的RLHF算法
后端·aigc·openai
shelter1 小时前
并发操作session对象导致登录闪退问题
后端
兆子龙1 小时前
TypeScript高级类型编程:从入门到精通
前端·后端
IT_陈寒1 小时前
Python开发者的效率革命:这5个技巧让你的代码提速50%!
前端·人工智能·后端
MekoLi291 小时前
Spring AI 与 LangChain4j 从入门到精通:Java 后端开发者的 AI 实战手册
后端·面试