FastDFS的搭建和相关使用

1.简介

FastDFS是一个开源的高性能分布式文件系统。它的主要功能包括:文件存储,文件同步和文件访问(文件上传和文件下载),它可以解决高容量和负载平衡问题。 FastDFS应该满足基于照片共享站点和视频共享站点等文件的网站的要求。

-- 引用自相关GITHUB项目

源GIT项目地址:
https://github.com/happyfish100/fastdfs

2.fastDfs的搭建

操作环境:CentOS7 X64,以下操作都是单机环境。

服务列表:

hostname ip Node Type username
m1 192.168.142.128 tracker server,storage server root
m2 192.168.142.134 tracker server,storage server root
①安装libfastcommon

获取libfastcommon安装包:

wget https://github.com/happyfish100/libfastcommon/archive/V1.0.38.tar.gz

解压安装包:

tar -zxvf V1.0.38.tar.gz

进入目录:

cd libfastcommon-1.0.38

执行编译:

./make.sh

安装:

./make.sh install
②安装FastDFS

获取fdfs安装包:

wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz

解压安装包:

tar -zxvf V5.11.tar.gz

进入目录:

cd fastdfs-5.11

执行编译:

./make.sh

安装:

./make.sh install

查看可执行命令:

ls -la /usr/bin/fdfs*
③配置Tracker服务
  1. 进入/etc/fdfs目录,有三个.sample后缀的文件(自动生成的fdfs模板配置文件),通过cp命令拷贝tracker.conf.sample,删除.sample后缀作为正式文件:

    cd /etc/fdfs/
    
    cp tracker.conf.sample tracker.conf  
    
    
  2. 编辑tracker.conf:vi tracker.conf,修改相关参数

    base_path=/data/log/fastdfs/tracker  #tracker存储data和log的跟路径,必须提前创建好
    port=22122 #tracker默认22122
    http.server_port=80 #http端口,需要和nginx相同
    
  3. 启动tracker(支持start|stop|restart):

    /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
    
  4. 查看tracker启动日志:进入刚刚指定的base_path(/data/log/fastdfs/tracker)中有个logs目录,查看tracker.log文件

     tail -200f /data/log/fastdfs/tracker/logs/trackerd.log![J5BqaR.png](https://s1.ax1x.com/2020/04/28/J5BqaR.png)
    

    J5BqaR.png

  5. 查看端口情况:netstat -apn|grep fdfs

J5BVu4.png

④配置Storage服务
  1. 进入/etc/fdfs目录,有cp命令拷贝storage.conf.sample,删除.sample后缀作为正式文件;

    cd /etc/fdfs/
    
    cp storage.conf.sample storage.conf
    
    
  2. 编辑storage.conf:vi storage.conf,修改相关参数:

    base_path=/data/log/fastdfs/storage   #storage存储data和log的跟路径,必须提前创建好
    port=23000  #storge默认23000,同一个组的storage端口号必须一致
    group_name=group1  #默认组名,根据实际情况修改
    store_path_count=1  #存储路径个数,需要和store_path个数匹配
    store_path0=/data/log/fastdfs/storage  #如果为空,则使用base_path
    tracker_server=192.168.142.128:22122 #配置该storage监听的tracker的ip和port
    
  3. 启动storage(支持start|stop|restart):

    /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
    
    // 如果现实文件夹不存在 则使用下面命令新建
    mkdir -p /data/log/fastdfs/storage
    
  4. 查看storage启动日志:进入刚刚指定的base_path(/data/log/fastdfs/storage)中有个logs目录,查看storage.log文件

    tail -200f /data/log/fastdfs/storage/logs/storaged.log 
    

5.此时再查看tracker日志:发现已经开始选举,并且作为唯一的一个tracker,被选举为leader

6.查看端口情况:

netstat -apn|grep fdfs

7.通过monitor来查看storage是否成功绑定:

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

J5ygyT.png

⑤安装Nginx和fastdfs-nginx-module模块
  1. 下载Nginx安装包

    wget http://nginx.org/download/nginx-1.15.2.tar.gz
    
  2. 下载fastdfs-nginx-module安装包

    wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz
    
  3. 解压nginx:

    tar -zxvf nginx-1.15.2.tar.gz
    
  4. 解压fastdfs-nginx-module:

    tar -xvf V1.20.tar.gz
    
  5. 进入nginx目录:

    cd nginx-1.15.2
    
  6. 安装依赖的库

    // 命令找不到请先安装gcc
    yum -y install gcc
        
    // centos请使用如下命令
    yum update
    yum install libpcre3 libpcre3-dev openssl libssl-dev libperl-dev
        
    // ubuntu使用如下命令
    apt-get update
    apt-get install libpcre3 libpcre3-dev openssl libssl-dev libperl-dev
        
        
    
  7. 配置,并加载fastdfs-nginx-module模块:

    ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/fastdfs-nginx-module-1.20/src/
    
  8. 编译安装:

    make
    make install
    
  9. 查看安装路径:

    whereis nginx
    

    J5coM6.png

  10. 启动、停止:

    cd /usr/local/nginx/sbin/
    ./nginx 
    ./nginx -s stop #此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程
    ./nginx -s quit #此方式停止步骤是待nginx进程处理任务完毕进行停止
    ./nginx -s reload
    
  11. 验证启动状态:

    wget "http://127.0.0.1"
    

    J5gEJs.png

  12. 查看此时的nginx版本:发现fastdfs模块已经安装好了

⑥配置Nginx和fastdfs-nginx-module模块
  1. 配置mod-fastdfs.conf,并拷贝到/etc/fdfs文件目录下

    cd /data/fastdfs
    cd fastdfs-nginx-module-1.20/src/
    cp mod_fastdfs.conf /etc/fdfs
    
  2. 进入/etc/fdfs修改mod-fastdfs.conf:

    base_path=/data/log/fastdfs
    tracker_server=192.168.142.128:22122 #tracker的地址
    url_have_group_name=true #url是否包含group名称
    storage_server_port=23000 #需要和storage配置的相同
    store_path_count=1  #存储路径个数,需要和store_path个数匹配
    store_path0=/data/fastdfs/storage #文件存储的位置
    
  3. 配置nginx,80端口server增加location如图:

    cd /usr/local/nginx/conf/
    vim nginx.conf
    
    location ~/group[0-9]/ {
                root   /data/fastdfs/storage;
                ngx_fastdfs_module;
            }
    
  4. 最后需要拷贝fastdfs解压目录中的http.conf和mime.types:

    cd /data/fastdfs/fastdfs-5.11/conf
    cp mime.types http.conf /etc/fdfs/
    

5.刷新nginx

cd /usr/local/nginx/sbin 
./nginx -s reload
⑦相关问题
返回错误码28,表示磁盘空间不足。注意FastDFS中有预留空间的概念,在tracker.conf中设置,配置项为:reserved_storage_space,缺省值为4GB,即预留4GB的空间。请酌情设置reserved_storage_space这个参数,比如可以设置为磁盘总空间的20%左右。

6.相关SpringBoot的Demo

maven依赖:
<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>


        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>

        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.5</version>
            <exclusions>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>commons-io</artifactId>
                    <groupId>commons-io</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>commons-beanutils</artifactId>
                    <groupId>commons-beanutils</groupId>
                </exclusion>
            </exclusions>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
application.properties:
#fastdfs配置
fastdfs.file.url=http://192.168.142.128/
fdfs.soTimeout=150000
fdfs.connectTimeout=60000
fdfs.thumbImage.width=150
fdfs.thumbImage.height=150
fdfs.trackerList[0]=192.168.142.128:22122
FastdfsDemoApplication:
@SpringBootApplication
//@Import(FdfsClientConfig.class) //使用配置中心
public class FastdfsDemoApplication {

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

}

FastDFSClient:
@Repository
@Slf4j
public class FastDFSClient {


    @Autowired
    private FastFileStorageClient storageClient;

    @Value("${fastdfs.file.url}")
    private String fastdfsFileUrl;

    /**
     * 上传文件
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadMultipartFile(MultipartFile file) {
        InputStream inputStream =null;
        try {
            inputStream = file.getInputStream();
            StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
            return getResAccessUrl(storePath);
        } catch (IOException e) {
            log.error("uploadMultipartFile error",e);
            return null;
        } finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    
    public String uploadFile(File file) {
        FileInputStream fileInputStream =null;
        try {
            fileInputStream = new FileInputStream(file);
            StorePath storePath = storageClient.uploadFile(fileInputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
            return getResAccessUrl(storePath);
        } catch (FileNotFoundException e) {
            log.error("uploadFile error",e);
            return null;
        } finally {
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * 将一段字符串生成一个文件上传
     *
     * @param content       文件内容
     * @param fileExtension
     * @return
     */
    public String uploadStringFile(String content, String fileExtension) {
        ByteArrayInputStream stream =null;
        try {
            byte[] buff = content.getBytes(Charset.forName("UTF-8"));
            stream = new ByteArrayInputStream(buff);
            StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
            return getResAccessUrl(storePath);
        } catch (Exception e) {
            log.error("uploadStringFile error",e);
            return null;
        } finally {
            if (stream!=null){
                try {
                    stream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = fastdfsFileUrl + storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 删除文件
     *
     * @param fileUrl 文件访问地址
     * @return
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            log.warn(e.getMessage());
        }
    }

}
TestDfsController:
/**
 * 相关的测试类
 */
@RestController
public class TestDfsController {

    @Autowired
    private FastDFSClient fastDFSClient;

    @RequestMapping("/test")
    public void testUpload() {
        File file = new File("C:\Users\admin\Desktop\test.txt");
        String path = fastDFSClient.uploadFile(file);
        System.out.println(path);
    }
}
原文地址:https://www.cnblogs.com/charlypage/p/12817731.html