docker快速安装fastdfs服务springboot访问「建议收藏」

docker快速安装fastdfs服务springboot访问「建议收藏」拉取镜像dockerpullmorunchang/fastdfs运行tracker跟踪器dockerrun-d–nametracker–net=hostmorunchang/fastdfsshtracker.sh运行storage存储器【注意:修改IP为自己的IP端口不变】dockerrun-d–namestorage–net=host-eTRACKER_IP=192.168.61.200:22122-eGROUP_NAME=gr

大家好,又见面了,我是你们的朋友全栈君。

  • 拉取镜像
docker pull morunchang/fastdfs
  • 运行tracker 跟踪器
 docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
  • 运行storage 存储器【注意:修改IP为自己的IP 端口不变】
  docker run -d --name storage --net=host -e TRACKER_IP=192.168.61.200:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
  • nginx的配置
docker exec -it storage  /bin/bash
cd data
vi nginx/conf/nginx.conf
  • 将红色框的内容添加进去(后面提供了可复制的内容)

docker快速安装fastdfs服务springboot访问「建议收藏」

location /group1/M00 {
	   proxy_next_upstream http_502 http_504 error timeout invalid_header;
		 proxy_cache http-cache;
		 proxy_cache_valid  200 304 12h;
		 proxy_cache_key $uri$is_args$args;
		 proxy_pass http://fdfs_group1;
		 expires 30d;
	 }

编辑完后:wq退出编辑

  • 然后退出docker
exit
  •  重启storage服务
docker restart storage

至此服务安装完毕.

借鉴博客:使用Docker快速搭建FastDFS_米斯特尔.W-CSDN博客_docker fastdfs搭建

集成springboot

<dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.2</version>
        </dependency>
# 分布式文件系统fastdfs配置
fdfs:
  # socket连接超时时长
  soTimeout: 1500
  # 连接tracker服务器超时时长
  connectTimeout: 600
  pool:
    # 从池中借出的对象的最大数目
    max-total: 153
    # 获取连接时的最大等待毫秒数100
    max-wait-millis: 102
  # 缩略图生成参数,可选
  thumbImage:
    width: 150
    height: 150
  # 跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加- x.x.x.x:port
  trackerList:
  - 192.168.0.1:22122
  #
  # 存储服务器storage_server访问地址
  web-server-url: http://192.168.0.1/
在springboot启动类上加

@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)

docker快速安装fastdfs服务springboot访问「建议收藏」

工具类:

package com.itheima.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

import com.github.tobato.fastdfs.domain.MataData;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.service.FastFileStorageClient;

/**
 * FastDFS客户端包装类
 *
 * @author CL
 *
 */
@Component
public class FdfsClientWrapper {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    public String uploadFile(MultipartFile file) throws IOException {
        if (file != null) {
            byte[] bytes = file.getBytes();
            long fileSize = file.getSize();
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            return this.uploadFile(bytes, fileSize, extension);
        }
        return null;
    }

    /**
     * 文件上传
     *
     * @param bytes     文件字节
     * @param fileSize  文件大小
     * @param extension 文件扩展名
     * @return 返回文件路径(卷名和文件名)
     */
    public String uploadFile(byte[] bytes, long fileSize, String extension) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        // 元数据
        Set<MataData> metaDataSet = new HashSet<MataData>();
        metaDataSet.add(new MataData("dateTime", LocalDateTime.now().toString()));
        StorePath storePath = fastFileStorageClient.uploadFile(bais, fileSize, extension, metaDataSet);
        return storePath.getFullPath();
    }

    /**
     * 下载文件
     *
     * @param filePath 文件路径
     * @return 文件字节
     * @throws IOException
     */
    public byte[] downloadFile(String filePath) throws IOException {
        byte[] bytes = null;
        if (StringUtils.isNotBlank(filePath)) {
            String group = filePath.substring(0, filePath.indexOf("/"));
            String path = filePath.substring(filePath.indexOf("/") + 1);
            DownloadByteArray byteArray = new DownloadByteArray();
            bytes = fastFileStorageClient.downloadFile(group, path, byteArray);
        }
        return bytes;
    }

    /**
     * 删除文件
     *
     * @param filePath 文件路径
     */
    public void deleteFile(String filePath) {
        if (StringUtils.isNotBlank(filePath)) {
            fastFileStorageClient.deleteFile(filePath);
        }
    }

}

测试接口:


import com.itheima.util.FdfsClientWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class TestController {
    private final FdfsClientWrapper fdfsClientWrapper;

    @Autowired
    public TestController(FdfsClientWrapper fdfsClientWrapper) {
        this.fdfsClientWrapper = fdfsClientWrapper;
    }

    @RequestMapping("upload")
    public String upload(@RequestParam MultipartFile file) {
        String filePath = null;
        try {
            filePath = fdfsClientWrapper.uploadFile(file);
        } catch (IOException e) {
            return "上传文件失败";
        }
        return filePath;
    }

    @RequestMapping("del")
    public String del(@RequestParam String filePath) {
        fdfsClientWrapper.deleteFile(filePath);
        return "删除成功";
    }
}

docker快速安装fastdfs服务springboot访问「建议收藏」

访问地址:

fastdfs服务器IP:8080/group1/M00/00/00/rBMvdGFAtNGAVy55AAyEK5YJQm8841.png

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149484.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月24日 下午8:36
下一篇 2022年6月24日 下午8:46


相关推荐

  • 使用 docker 部署 vlmcsd

    使用 docker 部署 vlmcsd使用 docker 部署 vlmcsd 激活 Windows10 专业版 激活 Office

    2026年3月19日
    1
  • docker(7)docker-compose容器集群编排「建议收藏」

    docker(7)docker-compose容器集群编排「建议收藏」前言实际工作中我们部署一个应用,一般不仅仅只有一个容器,可能会涉及到多个,比如用到数据库,中间件MQ,web前端和后端服务,等多个容器。我们如果一个个去启动应用,当项目非常多时,就很难记住了,所有

    2022年7月29日
    8
  • 卸载Docker方法

    卸载Docker方法卸载步骤在安装 Autoware 库的时候安装了 Docker 发现电脑硬盘容量被占用不少 现在想卸载一下 docker 查找了很多资料 最终使用以下方法完整卸载 1 在配置 autoware 的时候其实安装的 docker ce 所以需要执行 sudoapt getpurgedock ce 此时可以执行 dockerversio 查看 docker 是否被卸载 2 卸载安装依赖 sudo

    2026年3月26日
    2
  • docker项目经验_如何培育与指导部署

    docker项目经验_如何培育与指导部署每个人的前半生,都在不停地做加法。可到了后半生,我们就要学会不断地做减法。目录前置工作1、需要准备的东西2、连接云服务器安装Docker环境1、安装Docker的依赖库。2、添加DockerCE的软件源信息。3、安装DockerCE。4、启动Docker服务。准备Dockerfile并部署项目(构建新的业务镜像)1、准备nginx.conf.template、Dockerfile、dist(前端项目build后的包)2、部署项目知识点(需要…

    2022年10月19日
    4
  • 如何在docker容器中运行docker命令

    如何在docker容器中运行docker命令欢迎关注个人微信公众号:devopscube前言​Docker作为目前炙手可热的容器运行环境,越来越多的应用到应用的部署当中。这种一次打包,随处运行的模式备受好评,也节约了很多环境配置的麻烦。很多软件运行时都提供了docker的镜像部署方式,我们可以看到常用的组件,开源的项目,都会提供docker镜像,或者用于打包镜像的dockerfile。所以Docker已然成为了软件…

    2022年5月17日
    164
  • Docker安装Jenkins教程

    Docker安装Jenkins教程Docker安装Jenkins教程前言一、安装Jenkins1.下载Jenkins2.创建Jenkins挂载目录并授予权限3.启动Jenkins容器4.验证Jenkins容器是否启动二、浏览器访问Jenkins页面1.输入http://192.168.XX.XX:102402.获取管理员密码前言Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。提示:如果没有安装Docker,传送门在这里:链接:

    2022年5月15日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号