java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot配置Undertow服务器

springboot配置Undertow服务器全过程

作者:linab112

Spring Boot默认使用内嵌Tomcat容器简化部署,但部分开发者选择性能更优的Undertow替代,集成Undertow需排除Tomcat依赖,直接引入相关模块即可实现高并发、低内存的Web服务运行

说明

作为springboot开发者,使用最多的就是Tomcat,这是springboot默认的容器技术,而且是内嵌式的Tomcat,springboot作为目前最火的Java Web框架,可以说容器也起到了绝对的优势。

对于一个应用,我们也只需要将应用打包成一个jar包,以java -jar直接运行,而无需再打成war包的形式外搭tomcat服务器的方式运行应用。但是也会考虑使用Undertow容器去替代Tomcat。

Undertow概述

Undertow作为SpringBoot默认集成的三大容器之一(Tomcat、Jetty、Undertow);

首先它是Red Hat公司旗下的开源产品, 由Java语言开发,是一款灵活的高性能Web服务器;

不仅支持阻塞IO还支持非阻塞IO。由于Undertow通过java语言开发,即在java项目中使用可直接嵌入。

而且,Undertow完全支持Servlet和Web Socket,在高并发情况下表现非常出色,总之,Undertow在性能和内存使用方面都优于Jetty、Tomcat。

所以这就是为何业界内很多springboot开发者放弃Tomcat,选择Undertow。

springboot集成undertow

由于springboot优先默认内嵌的是tomcat,所以当你在引入Undertow容器时,你就需要先排除掉Tomcat,也就是它 spring-boot-starter-tomcat,

因此你需要找到spring-boot-starter-web这个starter,把Tomcat去掉,然后再引入undertow的依赖,然后直接启动项目即可。

<!--web依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!--排除spring-boot-starter-tomcat-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 添加Undertow容器 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文