springboot+fastdfs+docker

1.Fastdfs介绍

看博客:https://www.jianshu.com/p/b7c330a87855

说明:本系统是deepin系统,已经关闭了防火墙,其他centos等系统要自行判断关防火墙

2、docker安装Fastdfs (https://www.cnblogs.com/provence666/p/10987156.html

2.1、拉取镜像

docker pull delron/fastdfs:latest

2.2、查看镜像

docker images

 2.3、创建tracker文件夹

mkdir -p /data/tracker              :该文件夹为了和tracker服务器目录挂载

2.3、创建tracker容器

docker run -id --name tracker --restart=always --net host -v /etc/localtime:/etc/localtime -v /data/tracker:/var/fdfs delron/fastdfs tracker

2.4、创建storage文件夹

mkdir -p /data/storage         :该文件夹和storage服务器挂载,存储上传的文件

2.5、创建storage容器

docker run -id --name storage --restart=always --net host -v /etc/localtime:/etc/localtime -v /data/storage:/var/fdfs -e TRACKER_SERVER="192.168.226.14:22122" delron/fastdfs storage

2.6、查看容器

docker ps 看到如下,说明容器启动成功。

 2.7、docker exec -it 容器id bash 进入storage的容器

i1、了解storage.conf内容  /etc/fdfs/storage.conf

文件存储的位置(storage容器的)

上传的文件存储位置(storage容器的)

tracker服务器的地址

http访问的端口,默认是8888,可以不修改。

 i2、去/var/fdfs/ 目录下看,可以看到这个就是我们文件上传到的地方:data

 i3、配置nginx

进入/usr/local/ngxin/conf   默认配置,不修改也是可以的。

 i4、进入到/etc/fdfs/client.conf 看

 i5、测试文件上传

在我们宿主机的/data/storage 下放一张图片

 进入storage容器,/var/fdfs目录下,

执行命令

fdfs_upload_file  /etc/fdfs/client.conf b.jpg         看到返回了group1/xxx/xx  的字符串。

url访问: http://localhost:8888/group1/M00/00/00/wKjObV9Iy-mAZIE9AACGWRCnSVA093.jpg   看到如下图说明成功。

2.8、我们既然是进入了storage容器,看其配置,那么我们也进入tracker容器看看配置。

i1、docker exec -it tracker bash 进入tracker容器

进入 /etc/fdfs 看到如下

我们打开tracker.conf看看

(truacker服务器内容放置的地方)

 

 i2、看看放置内容的地方 /var/fdfs  ,虽然和storage服务器都是data和logs,但两者记录内容是不同的。

 3、上面docker安装FastDfs已经成功,那么接下来整springboot+fastdfs

3.1、导入依赖

     <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.5</version>
        </dependency>

3.2、加入配置类,FastdfsConfig

@Configuration
@Import(FdfsClientConfig.class) // 导入FastDFS-Client组件
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) // 解决jmx重复注册bean的问题
public class FasfdfsConfig {
    
}

3.3、FileDfsUtil

package com.yiyezhiqiu.fastdfs_hlh.utils;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;


/**
 * Created with IDEA
 * Date:2020/8/28
 * Time:上午10:37
 *
 * @author:lianhui.he
 */
@Component
public class FileDfsUtil {

    @Resource
    private FastFileStorageClient storageClient ;
    /**
     * 上传文件
     */
    public String upload(MultipartFile multipartFile) throws Exception{
        String originalFilename = multipartFile.getOriginalFilename().
                substring(multipartFile.getOriginalFilename().
                        lastIndexOf(".") + 1);
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                multipartFile.getInputStream(),
                multipartFile.getSize(),originalFilename , null);
        return storePath.getFullPath() ;
    }

    /**
     * 删除文件
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            System.out.println("fileUrl == >>文件路径为空...");
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }


}
3.4、FileController
@RestController
public class FileController {

    @Resource
    private FileDfsUtil fileDfsUtil ;
    /**
     * 文件上传
     */
    @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){
        String result ;
        try{
            String path = fileDfsUtil.upload(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "上传失败" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服务异常" ;
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 文件删除
     */
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public ResponseEntity<String> deleteByPath (){
        String filePathName = "group1/M00/00/00/wKjObV9Iy-mAZIE9AACGWRCnSVA093.jpg" ;
        fileDfsUtil.deleteFile(filePathName);
        return ResponseEntity.ok("SUCCESS") ;
    }

}

3.5、配置文件application.yml

server:
  port: 8080

#分布式文件系统Fdfsdfs配置
fdfs:
  soTimeout: 1500 #socket连接超时时长
  connectTimeout: 600 #连接tracker服务器超时时长
  thumbImage: #缩略图生成参数,可选
     150
    height: 150
  trackerList: #TrackerList参数,支持多个,我这里只有一个,如果有多个在下方加- x.x.x.x:port
    - 192.168.222.22:22122
#    - 192.168.8.102:22122

spring:
  application:
    name: FastDFS-Test
  servlet:
    multipart:
      max-file-size: 100MB # 最大支持文件大小
      max-request-size: 100MB # 最大支持请求大小

3.6、postman测试

 结果:

 码云:https://gitee.com/hlh_yiyezhiqiu/fastdfs.git 





原文地址:https://www.cnblogs.com/yiyezhiqiuwuchen/p/13579748.html