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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

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