搭建FastDFS单节点对象存储【通过Nginx下载】

  |   0 评论   |   0 浏览

前提

系统:Cent OS 7

下载压缩包:https://gitsilence.lanzoui.com/iDYmVq4nsab
密码:6ho9

安装Fast DFS

解压下载的压缩包

进入文件夹

FastDFSMack.sh 脚本加上执行权限

chmod +x FastDFSMack.sh

执行脚本

./FastDFSMack.sh

运行成功,服务自动给我们运行了

从脚本中可以看出,tracker和storage的路径在 /home/data/...

关闭防火墙,或者设置端口白名单 22122和23000

将conf中的配置文件 移动到 /etc/fdfs 文件夹下

mv ./conf/* /etc/fdfs/

FastDFS与Nginx整合

解压fastdfs-nginx-module

tar -zxvf fastdfs-nginx-module-1.20.tar.gz

将这个模块的 mod_fastdfs.conf 文件 移动到 /etc/fdfs

mv ./fastdfs-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs/

创建文件夹

mkdir -p /home/data/fastdfs-nginx/logs

修改配置文件

vim /etc/fdfs/mod_fastdfs.conf
base_path=/home/data/fastdfs-nginx/logs
tracker_server=192.168.44.131:22122
store_path0=/home/data/storage
url_have_group_name = true

修改config文件

 vim fastdfs-nginx-module-1.20/src/config

image.png

fastdfs-nginx-module 所在目录

image.png

安装Nginx

解压nginx的压缩包,然后进入nginx目录

tar -zxvf nginx-1.17.9.tar.gz

加入模块命令

./configure --add-module=/root/fastdfs/fastdfs/fastdfs-nginx-module-1.20/src

报错信息

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

安装 相关依赖

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

然后再重新执行 加入模块命令

执行成功后,再执行 编译安装命令

make && make install 

配置Nginx

进入配置文件目录

cd /usr/local/nginx/conf
vim nginx.conf
        location ~/group([0-9])/M00 {
            ngx_fastdfs_module;
        }

Fast DFS Java测试

创建一个springboot 项目

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

配置文件

fdfs.connect-timeout=1501
fdfs.so-timeout=601
fdfs.tracker-list[0]=192.168.44.131:22122

启动类

@SpringBootApplication
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class SpringbootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }

}

工具类

package cn.lacknb.springbootdemo.config;

import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Component
public class FastDFSClientUtil {

    private static final Logger logger = LoggerFactory.getLogger(FastDFSClientUtil.class);

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * FastDFS 文件上传
     *
     * @param bsnNo
     * @param file
     * @return
     */
    public synchronized String uploadByFile(String bsnNo, File file) {
        FileInputStream fis = null;
        String uploadByFile = null;
        try {
            fis = new FileInputStream(file);
            int retryTimes = 3;
            while (uploadByFile == null && retryTimes > 0) {
                --retryTimes;
                uploadByFile = uploadByFile(bsnNo, fis, file.getName(), file.length());
                if (uploadByFile == null) {
                    Thread.sleep(2000);
                    logger.info("{},{},{},{},{},retryTimes {}",
                            bsnNo,
                            Thread.currentThread().getName(),
                            System.currentTimeMillis(),
                            file.getName(),
                            file.length(),
                            retryTimes);
                }
            }
        } catch (Exception e) {
            logger.error("{},{},{},{},{},file to insteam error",
                    bsnNo,
                    Thread.currentThread().getName(),
                    System.currentTimeMillis(),
                    file.getName(),
                    file.length());
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                }
            }
        }
        return uploadByFile;
    }

    /**
     * FastDFS 文件上传
     *
     * @param bsnNo
     * @param fis
     * @param fileName
     * @param size
     * @return
     */
    public synchronized String uploadByFile(String bsnNo, InputStream fis, String fileName, long size) {
        try {
            String suffix = StringUtils.substringAfterLast(fileName, ".");
            logger.info("{},{},{},{},{}",
                    bsnNo,
                    Thread.currentThread().getName(),
                    System.currentTimeMillis(),
                    fileName,
                    size);
            StorePath storePath = storageClient.uploadFile(null, fis, size, suffix);
            return storePath.getFullPath();
        } catch (Exception e) {
            logger.error("{},{},{},{},{}, file upload error, {}",
                    bsnNo,
                    Thread.currentThread().getName(),
                    System.currentTimeMillis(),
                    fileName,
                    size, e);
        }
        return null;
    }

    public FileInfo queryFileInfo (String systemPath) {
        String group = systemPath.substring(0, systemPath.indexOf("/"));
        String path = systemPath.substring(systemPath.indexOf("/") + 1);
        return storageClient.queryFileInfo(group, path);
    }

    /**
     * 获取文件字节流
     *
     * @param systemPath fastdfs 上传成功后返回得路径
     * @return
     */
    public byte[] getBytes(String systemPath, long offset, long downloadBytes) {
        if (StringUtils.isBlank(systemPath)) {
            return null;
        }
        byte[] bytes = null;
        try {
            String group = systemPath.substring(0, systemPath.indexOf("/"));
            String path = systemPath.substring(systemPath.indexOf("/") + 1);
            logger.info("get file bytes, systemPath={}", systemPath);
            int retryTimes = 3;
            while (bytes == null && retryTimes > 0) {
                --retryTimes;
                bytes = storageClient.downloadFile(group, path, offset, downloadBytes, new DownloadByteArray());
                if (bytes == null) {
                    Thread.sleep(2000);
                    logger.info("get file bytes, systemPath={},retryTimes {}", systemPath, retryTimes);
                }
            }
        } catch (Exception e) {
            logger.error(" fastdfs file down error, systemPath={}, message={}, {}", systemPath, e.getMessage(), e);
        }
        return bytes;
    }

    /**
     * byte 转文件 下载到本地
     *
     * @param fileName
     * @param
     */
    public String conserveFile(String path, String fileName, String systemPath, byte[] size) {
        long startTime = System.currentTimeMillis();
        String filePath = "";
        try {
            String filePathDir = path + File.separator + File.separator + UUID.randomUUID().toString() + File.separator + File.separator;
            File folder = new File(filePathDir);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            filePath = filePathDir + fileName;
            File file = new File(filePath);
            if (file.exists()) {
                //如果文件存在,则删除文件
                file.delete();
            }
            logger.info("fastdfs file down filePath={},{},systemPath={},size={}",
                    filePath,
                    fileName,
                    systemPath,
                    size);
            FileUtils.writeByteArrayToFile(file, size);
            return filePath;
        } catch (Exception e) {
            logger.error(" fastdfs file down error, filePath={},{},systemPath={},size={}, {}",
                    filePath,
                    fileName,
                    systemPath,
                    size, e);
        } finally {
            long endTime = System.currentTimeMillis();
            logger.info(" fastdfs file down , filePath={},{},systemPath={},size={},time={}",
                    filePath,
                    fileName,
                    systemPath,
                    size,
                    (endTime - startTime));
        }
        return null;
    }

}

    @GetMapping("/upload")
    public String upload () {
        // group1/M00/00/00/wKgsgmDDiouASaFKPjaYZm5PBqM714.zip  995M
        // group1/M00/00/00/wKgsgmDDjSaARVNRqUvW6JNWGqU595.zip  2.6G
        return fastDFSClientUtil.uploadByFile(UUID.randomUUID().toString(), new File("D:\\hello.txt"));
    }

image.png

image.png


标题:搭建FastDFS单节点对象存储【通过Nginx下载】
作者:gitsilence
地址:https://lacknb.cn/articles/2021/06/12/1623494092946.html