当前位置: 代码网 > it编程>编程语言>Java > springboot中使用undertow踩坑记(最新推荐)

springboot中使用undertow踩坑记(最新推荐)

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

场景:准备基于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的容器的有多少?欢迎大家反馈。

到此这篇关于springboot中使用undertow踩坑记的文章就介绍到这了,更多相关springboot使用undertow内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com