SpringBoot使用Undertow替换Tomcat容器

947 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

一、为什么要使用Undertow容器

简单来说就是性能更加强劲,高并发下面性能要优于Jetty和Tomcat,具体性能测评网上有相关的报告。

二、使用Undertow

在pom文件中添加如下配置即可

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
    <!--去除spring boot默认使用的tomcat容器-->
        <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>

三、配置application.yml

server:
  port: 8081
  # 下面是配置undertow作为服务器的参数
  undertow:
    # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
    # 每块buffer的空间大小,越小的空间被利用越充分
    buffer-size: 1024
    # 是否分配的直接内存
    direct-buffers: true

四、查看结果

启动项目控制台看到如下信息基本就是使用成功了

Undertow started on port(s) 8080 (http)