原创

Spring Boot如何替换默认的tomcat服务器?

1.为什么要替换默认tomcat?

Tomcat是Apache基金下的一个轻量级的Servlet容器,支持Servlet和JSP。Tomcat具有Web服务器特有的功能,包括 Tomcat管理和控制平台、安全局管理和Tomcat阀等。Tomcat本身包含了HTTP服务器,因此也可以视作单独的Web服务器。在SpringBoot框架中,我们使用最多的是Tomcat,这是SpringBoot默认的容器技术,而且是内嵌式的Tomcat。

Tomcat与Undertow的优劣对比

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

37d9e5277e348927ba254023dc737332e645d7

我们在相同机器配置下压测Tomcat和Undertow,得到的测试结果如下所示:

QPS测试结果对比:

Tomcat

6637a1f82a69943a9ee90567e76785f0eea998

Undertow

86f29ac96150c21e89e387a389fd8c425164d5

通过测试发现,在高并发系统中,Tomcat相对来说比较弱。在相同的机器配置下,模拟相等的请求数,Undertow在性能和内存使用方面都是最优的。并且Undertow新版本默认使用持久连接,这将会进一步提高它的并发吞吐能力。所以,如果是高并发的业务系统,Undertow是最佳选择。

2.代码工程

实验目标

用undertow替换默认的tomcat

pom.xml

先排除tomcat,然后添加undertow依赖包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Undertow</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>

        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <!--jetty-->
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>-->
    </dependencies>
</project>

controller

package com.et.undertow.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public Map<String, Object> showHelloWorld(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld");
        return map;
    }
}

DemoApplication.java

package com.et.undertow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

3.测试

启动Spring Boot应用,控制台日志发现已经使用undertow了
 . ____ _ __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot :: (v2.2.5.RELEASE)

2024-08-12 13:40:21.429 INFO 34216 --- [ main] com.et.undertow.DemoApplication : Starting DemoApplication on BJDPLHHUAPC with PID 34216 (D:\IdeaProjects\ETFramework\Undertow\target\classes started by Dell in D:\IdeaProjects\ETFramework)
2024-08-12 13:40:21.433 INFO 34216 --- [ main] com.et.undertow.DemoApplication : No active profile set, falling back to default profiles: default
2024-08-12 13:40:22.663 WARN 34216 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2024-08-12 13:40:22.684 INFO 34216 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext
2024-08-12 13:40:22.684 INFO 34216 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1196 ms
2024-08-12 13:40:22.827 INFO 34216 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2024-08-12 13:40:22.947 INFO 34216 --- [ main] io.undertow : starting server: Undertow - 2.0.29.Final
2024-08-12 13:40:22.956 INFO 34216 --- [ main] org.xnio : XNIO version 3.3.8.Final
2024-08-12 13:40:22.966 INFO 34216 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final
2024-08-12 13:40:23.065 INFO 34216 --- [ main] o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 8088 (http) with context path ''
2024-08-12 13:40:23.067 INFO 34216 --- [ main] com.et.undertow.DemoApplication : Started DemoApplication in 2.021 seconds (JVM running for 2.409)

4.引用

正文到此结束
Loading...