0
点赞
收藏
分享

微信扫一扫

CentOS-7系统 docker 安装fastdfs

倪雅各 2022-04-25 阅读 73

CentOS-7系统 docker 安装fastdfs

环境

安装步骤

安装docker

这里使用官方脚本一键安装curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

拉取镜像

  • docker pull season/fastdfs:1.2

  • 创建需要的目录

  • 启动

    sudo docker run -id --name tracker  -p 22122:22122  --restart=always --net host  -v ~/fdfs/tracker/data:/fastdfs/tracker/data  season/fastdfs:1.2 tracker
    
    sudo docker run -id --name storage --restart=always --net host -v ~/fdfs/storage/data:/fastdfs/store_path -e TRACKER_SERVER="192.168.175.128:22122" season/fastdfs:1.2 storage
    
  • 复制nginx文件sudo docker cp storage:/etc/nginx/conf/nginx.conf ~/fdfs/nginx/

  • 修改nginx.conf配置文件

  •  location / {
    		    root /fastdfs/store_path/data;
    		    ngx_fastdfs_module;
    		 }
    
    sudo docker run -id --name fdfs_nginx --restart=always -v ~/fdfs/storage/data:/fastdfs/store_path -v ~/fdfs/nginx/nginx.conf:/etc/nginx/conf/nginx.conf -p 80:80 -e TRACKER_SERVER=192.168.175.128:22122 season/fastdfs:1.2 nginx
    

测试

代码:

package com.example.demo;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    private FastFileStorageClient storageClient;

    @Test
    void contextLoads() {

        InputStream is = null;
        try{
            // 获取文件源
            File source = new File("C:\\Users\\CNKI\\Pictures\\wallhaven-xlljqv.jpg");
            // 获取文件流
            is = new FileInputStream(source);
            // 进行文件上传
            StorePath storePath = storageClient.uploadFile(is, source.length(), FilenameUtils.getExtension(source.getName()), null);
            // 获得文件上传后访问地址
            String fullPath = storePath.getFullPath();
            // 打印访问地址
            System.out.println("fullPath = " + fullPath);
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }
        finally{
            try
            {
                if(is != null){
                    // 关闭流资源
                    is.close();
                }
            }
            catch (IOException e){
                e.printStackTrace();
            }
        }
    }

}

结果:
在这里插入图片描述
在这里插入图片描述

举报

相关推荐

0 条评论