SpringBoot2使用Undertow来提高应用性能(spring-boot-starter-undertow)

版权声明:powered by 大狼狗郑锴/Moshow魔手 https://blog.csdn.net/moshowgame/article/details/84985765

Undertow

Undertow是一个Java开发的灵活的高性能Web服务器,提供包括阻塞和基于NIO的非阻塞机制。Undertow是红帽公司的开源产品,是Wildfly默认的Web服务器。
SpringBoot2中可以将Web服务器切换到Undertow来提高应用性能。

Untertow 的特点

  • Servlet4.0 支持:它提供了对 Servlet4.0 的支持。
  • WebSocket 支持:对 Web Socket 完全支持,包括JSR-356,用以满足 Web 应用巨大数量的客户端。
  • 嵌套性:它不需要容器,只需通过 API 即可快速搭建 Web 服务器。
  • 灵活性:交由链式Handler配置和处理请求,可以最小化按需加载模块,无须加载多余功能。
  • 轻量级:它是一个 Web 服务器,但不像传统的 Web 服务器有容器概念,它由两个核心 Jar 包组成,加载一个 Web 应用可以小于 10MB 内存。

Untertow 的性能

默认情况下 Spring Cloud 使用 Tomcat 作为内嵌 Servlet 容器,可启动一个 Tomcat 的 Spring Boot 程序与一个 Undertow 的 Spring Boot 程序,通过 VisualVM 工具进行比较,可看到 Undertow 性能优于 Tomcat。
在这里插入图片描述
在这里插入图片描述

SpringBoot2启用Undertow

第一步,排除Tomcat依赖。
第二部,添加Undertow依赖。

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			 <!-- 排除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>

缺少包导致的报错

如果你的undertow也会这样:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call the method io.undertow.servlet.api.DeploymentInfo.addServletContainerInitializer(Lio/undertow/servlet/api/ServletContainerInitializerInfo;)Lio/undertow/servlet/api/DeploymentInfo; but it does not exist. Its class, io.undertow.servlet.api.DeploymentInfo, is available from the following locations:

    jar:file:/C:/Users/Administrator/.m2/repository/io/undertow/undertow-servlet/1.4.25.Final/undertow-servlet-1.4.25.Final.jar!/io/undertow/servlet/api/DeploymentInfo.class

It was loaded from the following location:

    file:/C:/Users/Administrator/.m2/repository/io/undertow/undertow-servlet/1.4.25.Final/undertow-servlet-1.4.25.Final.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of io.undertow.servlet.api.DeploymentInfo

请加上以下maven依赖。因为我的SpringBoot2.0.7会这样。

<!-- https://mvnrepository.com/artifact/io.undertow/undertow-core -->
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-core</artifactId>
            <version>2.0.16.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.undertow/undertow-servlet -->
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-servlet</artifactId>
            <version>2.0.16.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.undertow/undertow-websockets-jsr -->
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-websockets-jsr</artifactId>
            <version>2.0.16.Final</version>
        </dependency>

运行成功

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/moshowgame/article/details/84985765