慎重!springboot中用undertow踩坑记

场景:准备基于springboot的静态资源实现mp4资源的播放,不同版本的springboot下效果不一样,可能导致正常的资源不可用。本文测试了几个版本,也针对这种情况提出了解决建议,希望对你的工作有所帮助。

众所周知,springboot内置类web中间件,将web服务器管理权交给了容器。在使用时只需要进行申明即可。

本文实验的环境如下:

windows7+JDK1.8+Eclipse+Maven3.3.9+SpringBoot2.2.x+Undertow2.2.x

一、环境准备

第一步、配置maven环境

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.yelang</groupId>
  <artifactId>undertowdemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Undertow测试</name>
  <description>Undertow中间件测试</description>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.10.RELEASE</version>
        <relativePath />
    </parent>

  <dependencies>
    <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>
  </dependencies>
</project>

第二步、配置申明

# 开发环境配置
server:
  # 服务器的HTTP端口,默认为8080
  port: 8080
  servlet:
    # 应用的访问路径
    context-path: /
  # undertow 配置
  undertow:
    # HTTP post内容的最大大小。当值为-1时,默认值为大小是无限的
    max-http-post-size: -1
    # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
    # 每块buffer的空间大小,越小的空间被利用越充分
    buffer-size: 512
    # 是否分配的直接内存
    direct-buffers: true
    threads:
      # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程
      io: 8
      # 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载
      worker: 256
#  # tomcat 配置
#  tomcat:
#    # tomcat的URI编码
#    uri-encoding: UTF-8
#    # tomcat最大线程数,默认为200
#    max-threads: 500
#    # Tomcat启动初始化的线程数,默认值25
#    min-spare-threads: 30

第三步、静态资源映射

package com.yelang.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 通用配置
 * @author wzh
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    /** 本地文件上传路径 */
    registry.addResourceHandler("/profile/**").addResourceLocations("file:D:/wzh/uploadPath/");
    /** swagger配置 */
    registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }
}

以上代码标注了系统对外开放的静态资源,正常情况下,将资源拷贝到相应的目录下,就可以访问相应资源。

http://localhost:8080/profile/2.mp4

图片

二、使用springboot2.2.11、springboot2.2.12、springboot2.2.13这三个版本正常mp4也会无法加载。估计是这几个版本存在一些设置。

三、如果是生产采用了上述几个版本的sringboot,如果需要对mp4等资源进行预览查看的话。

建议如下:第一、调整springboot的版本,调整到支持的版本。第二、不再使用profile的方式提供视频资源,采用nginx等组件。第三、采用第三方文件系统。第四种、将undertow容器替换成tomcat等其他容器也可以。

小调查:在你的生产环境中,是使用内置容器吗?使用undertow这种nio的容器的有多少?欢迎大家反馈。

Guess you like

Origin blog.csdn.net/yelangkingwuzuhu/article/details/118072360