docker快速安装fastdfs服务springboot访问(推荐使用Minio文件服务器)

一、安装FastDFS

防火墙开放端口:8080、22122、23000 

实际使用过程中发现,Minio文件服务更好用,安装便捷、稳定、自带可视化。MinIO下载 

  • 拉取镜像(此镜像已经配置好了nginx,访问端口为8080)
docker pull morunchang/fastdfs
  • 运行tracker 跟踪器(端口为:22122)
 docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
  • 运行storage 存储器(端口为:23000)【注意:修改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的配置(是storage服务里面自带的nginx服务,不是你主机上的nginx
docker exec -it storage  /bin/bash
cd data
vi nginx/conf/nginx.conf
  • 将红色框的内容添加进去(后面提供了可复制的内容)
    (键盘方向键↓↓↓↓↓↓↓↓↓↓↓就能看见下边的内容了)

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搭建

二、集成Java、Python

集成python

pip install py3Fdfs==2.2.0

 配置文件:client.conf

connect_timeout=30
network_timeout=60
tracker_server = 0.0.40.17:22122
http.tracker_server_port = 8088

上传文件:

from fdfs_client.client import get_tracker_conf, Fdfs_client

if __name__ == '__main__':
    tracker_conf = get_tracker_conf('./client.conf')
    client = Fdfs_client(tracker_conf)
    filename = client.upload_by_filename(filename='1.jpg')
    print(filename)

 

集成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)

工具类:

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 "删除成功";
    }
}

访问地址:

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

三、文件清理

 进入容器

docker exec -it storage  /bin/bash
cd /data/fast_data/data

这就是文件所在位置,根据需要进行删除即可。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文子阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值