FastDFS学习笔记

本文详细介绍了FastDFS的架构、环境搭建、Java API操作、Nginx集成、FastDHT去重以及集群搭建等内容。从本地存储的局限性引入分布式文件系统FastDFS,讲解了FastDFS的安装配置,包括在虚拟机上的部署,FastDFS与Nginx的集成以提供Http服务,以及FastDFS集成FastDHT实现文件去重。还涵盖了FastDFS集群搭建与测试,确保了高可用性和数据一致性。
摘要由CSDN通过智能技术生成

FastDFS学习笔记

文章目录

一、FastDFS引言

本地存储与分布式文件系统

在这里插入图片描述
本地存储的缺点:

  1. 是否有备份? 没有

  2. 成本角度? 贵

    ​ 服务器 :用于计算 ---- cpu/内存

    ​ 用于存储 ---- 硬盘大

  3. 存储瓶颈? 容量有限

  4. 是否使用于应用服务器的集群环境? 不适用

  5. 。。。等等

解决本地存储的缺点 : 使用分布式文件系统 — 引入存储服务的概念

支持冗余备份;可以增加节点,所以理论上没有瓶颈;支持集群环境;支持负载均衡特性,可以在一个存储节点压力大时,将请求分给其他存储节点。

数据存储的方式:

  1. 关系型数据库(mysql,oracle…):用于强事务环境
  2. 非关系型数据库(redis…):用于弱化事务环境,性能高
  3. 分布式文件系统(FastDFS…):用于存储二进制数据(图片/视频/音频)

FastDFS概述

​ FastDFS(Distributed File System (DFS))是一个开源的分布式文件系统,她对文件进行管理,功能包括:文件存储、文件同步(冗余备份)、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等

​ FastDFS服务端有两个角色:跟踪器(tracker)存储节点(storage)

跟踪器主要做调度工作,在访问上起负载均衡的作用

存储节点存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,FastDFS同时对文件的meta data(元数据)进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value pair)方式表示,如:width=1024,其中的key为width,value为1024。文件meta data是文件属性列表,可以包含多个键值对。

FastDFS架构

FastDFS架构图:

架构图

FastDFS架构图详解 :
在这里插入图片描述

注:图中文件上传到存储节点后返回的文件在文件系统中的path,即为文件在FastDFS中的文件标识,分为两个部分:卷名文件名二者缺一不可

跟踪器和存储节点都可以由一台至多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。

​ 为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷 的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起 到了冗余备份和负载均衡的作用。

​ 在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。

​ 当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。

​ 以上内容来自:https://www.oschina.net/p/fastdfs

FastDFS环境搭建(虚拟机)

1. 创建虚拟机克隆机

在这里插入图片描述

2. 配置虚拟机
  1. 删除保存有原始虚拟机网卡初始化信息的文件:

    # CentOS 6 中执行以下命令
    [root@localhost ~]# rm -rf /etc/udev/rules.d/70-persistent-net.rules
    
  2. 修改主机名:

    1. CentOS 6 中修改主机名

      # CentOS 6 中执行以下命令
      [root@localhost ~]# vi /etc/hosts
      

      在这里插入图片描述

      # CentOS 6 中执行以下命令
      [root@localhost ~]# vi /etc/sysconfig/network
      

      在这里插入图片描述

    2. CentOS 7 中修改主机名(参考:https://www.itzgeek.com/how-tos/linux/centos-how-tos/change-hostname-in-centos-7-rhel-7.html)

      1. 查看当前主机名

        [root@localhost ~]# hostname
        localhost.localdomain
        
      2. 检查当前主机名

        [root@localhost ~] #hostnamectl status
        Static hostname: localhost.localdomain
        Icon name: computer-vm
        Chassis: vm
        Machine ID: 565ea8b749544aca9d5563308f9e4bc2
        Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad
        Virtualization: vmware
        Operating System: CentOS Linux 7 (Core)
        CPE OS Name: cpe:/o:centos:centos:7
        Kernel: Linux 3.10.0-123.el7.x86_64
        Architecture: x86_64
        //==================翻译====================
        静态主机名:localhost.localdomain
        图标名称:computer-vm
        底盘:vm
        机器ID:565ea8b749544aca9d5563308f9e4bc2
        引导ID:5c979d9b5f754df8b75a4e3aeabf2bad
        虚拟化:vmware
        操作系统:CentOS Linux 7(核心)
        CPE操作系统名称:cpe:/ o:centos:centos:7
        内核:Linux 3.10.0-123.el7.x86_64
        架构:x86_64
        //==========================================
        
      3. 设置主机名

        [root@localhost ~]# hostnamectl set-hostname tracker.fastdfs
        
      4. 再次检查主机名

        [root @ client~] #hostnamectl status
        Static hostname: tracker.fastdfs
        Icon name: computer-vm
        Chassis: vm
        Machine ID: 565ea8b749544aca9d5563308f9e4bc2
        Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad
        Virtualization: vmware
        Operating System: CentOS Linux 7 (Core)
        CPE OS Name: cpe:/o:centos:centos:7
        Kernel: Linux 3.10.0-123.el7.x86_64
        Architecture: x86_64
        //==================翻译====================
        静态主机名:tracker.fastdfs
        图标名称:computer-vm
        底盘:vm
        机器ID:565ea8b749544aca9d5563308f9e4bc2
        引导ID:5c979d9b5f754df8b75a4e3aeabf2bad
        虚拟化:vmware
        操作系统:CentOS Linux 7(核心)
        CPE操作系统名称:cpe:/ o:centos:centos:7
        内核:Linux 3.10.0-123.el7.x86_64
        架构:x86_64
        //==========================================
        
  3. 修改完毕,需要重启虚拟机

    # CentOS 6 与 7 中执行以下命令
    [root@localhost ~]#  reboot
    
  4. 网卡信息的配置

    i. 进入到网卡信息编辑文件:

    # CentOS 6 中执行以下命令
    [root@fastdfs ~]# vi  /etc/sysconfig/network-scripts/ifcfg-eth0
    # CentOS 7 中执行以下命令
    [root@tracker ~]# vi  /etc/sysconfig/network-scripts/ifcfg-ens33
    

    ii. 在当前窗口: 按 i 键,进入编辑 通过上下左右箭头按键,控制光标

    编辑完成按 esc, 然后输入 冒号 :wq! 回车 ,进行保存
    在这里插入图片描述

    设置静态IP可参考博客:https://blog.csdn.net/u010452388/article/details/86481868

    iii. 重新加载网卡配置:

    # CentOS 6与7 中执行以下命令
    [root@fastdfs ~]# service network restart 
    # CentOS 7 中执行以下命令
    [root@tracker ~]# systemctl restart network 
    
  5. 检查防火墙状态

    # CentOS 6 中执行以下命令
    [root@fastdfs ~]# service iptables status
    # CentOS 7 中执行以下命令
    [root@tracker ~]# systemctl status firewalld
    

    在这里插入图片描述

  6. 关闭防火墙

    # CentOS 6 中执行以下命令
    [root@fastdfs ~]# service iptables stop
    # CentOS 7 中执行以下命令
    [root@tracker ~]# systemctl stop firewalld
    

    在这里插入图片描述

  7. 关闭防火墙开机自启功能

    # CentOS 6 中执行以下命令
    [root@fastdfs ~]# chkconfig iptables off
    # CentOS 7 中执行以下命令
    [root@tracker ~]# systemctl disable firewalld
    

    在这里插入图片描述

  8. 查看防火墙状态

    # CentOS 6 中执行以下命令
    [root@fastdfs ~]# service iptables status
    # CentOS 7 中执行以下命令
    [root@tracker ~]# systemctl status firewalld
    

    在这里插入图片描述

3. 下载安装gcc

​ 安装方式为yum安装(需网络):

[root@tracker ~]# yum install gcc-c++ perl-devel pcre-devel openssl-devel zlib-devel wget
4. 下载安装FastDFS
[root@tracker ~]# wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz 
5. 下载安装FastDFS依赖
[root@tracker ~]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.36.tar.gz 
6. 解压缩依赖tar包

解压缩libfastcommon-1.0.36的依赖tar包到指定文件夹 /usr

[root@tracker ~]# tar -zxvf V1.0.36.tar.gz -C /usr
7. 解压缩FastDFS的tar包

解压缩fastdfs-5.11的依赖tar包到指定文件夹 /usr

[root@tracker ~]# tar -zxvf V5.11.tar.gz -C /usr
8. 进入libfastcommon-1.0.36
[root@tracker ~]# cd /usr/libfastcommon-1.0.36/
9. 编译并安装libfastcommon
[root@tracker libfastcommon-1.0.36]# ./make.sh && ./make.sh install
10. 进入fastdfs-5.11
[root@tracker libfastcommon-1.0.36]# cd ../fastdfs-5.11/
11. 编译并安装FastDFS
[root@tracker fastdfs-5.11]# ./make.sh && ./make.sh install
12. 进入etc目录下的fdfs目录
[root@tracker fastdfs-5.11]# cd /etc/fdfs/
13. 进行FastDFS的配置
#分别将配置文件复制一份
[root@tracker fdfs]# cp tracker.conf.sample tracker.conf
[root@tracker fdfs]# cp storage.conf.sample storage.conf
[root@tracker fdfs]# cp client.conf.sample client.conf

## ==============修改tracker.conf配置文件内容=============
[root@tracker fdfs]# vi tracker.conf
# 修改追踪服务的数据与日志文件的存储路径
base_path=/data/fastdfs/tracker
# 保存退出编辑
esc-->:wq!-->Enter
# -------创建追踪服务的数据与日志文件的存储目录-------
[root@tracker fdfs]# mkdir -p /data/fastdfs/tracker
## ==============修改storage.conf配置文件内容=============
[root@tracker fdfs]# vi storage.conf
# 修改存储节点的数据与日志文件的存储路径
base_path=/data/fastdfs/storage
# 修改上传文件在存储节点的存储路径
store_path0=/data/fastdfs/storage/store
# 修改为tracker服务器的ip(此处为本机ip)
tracker_server=192.168.:q43.136:22122
# 保存退出编辑
esc-->:wq!-->Enter
# -------创建存储节点的数据与日志文件及上传文件的存储目录-------
[root@tracker fdfs]# mkdir -p /data/fastdfs/storage/store
## ==============修改client.conf配置文件内容==============
[root@tracker fdfs]# vi client.conf
# 修改
base_path=/tmp
# 修改为tracker服务器的ip(此处为本机ip)
tracker_server=192.168.43.136:22122
# 保存退出编辑
esc键-->输入:wq!-->Enter键
14. 启动服务
  1. 启动 tracker server

    [root@tracker fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf start
    
  2. 启动 storage server

    [root@tracker fdfs]# fdfs_storaged /etc/fdfs/storage.conf start
    
15. 查看启动的服务
[root@tracker fdfs]# ps -ef | grep fdfs

在这里插入图片描述
++++++++++++++++++++++++++++ 环境搭建完成 ++++++++++++++++++++++++++++

FastDFS部分指令简介

​ 输入fdfs_ —> 连按两下tab键 —> 显示所有FastDFS的fdfs_xxx指令
在这里插入图片描述

1. 监控指令 — fdfs_monitor
  1. 指令参数

    fdfs_monitor <config_file> [-h <tracker_server>] [list|delete|set_trunk_server <group_name> [storage_id]]
    

    注: 参数含义:

    1. <config_file> :配置文件路径
    2. [-h <tracker_server>] :(可选参数)
    3. [list|delete|set_trunk_server [storage_id]] :(可选参数)
  2. 指令使用

    [root@tracker fdfs]# fdfs_monitor /etc/fdfs/client.conf
    

    在这里插入图片描述

2. 上传指令 — fdfs_upload_file
  1. 指令参数

    fdfs_upload_file <config_file> <local_filename> [storage_ip:port] [store_path_index]
    

    注: 参数含义:

    1. <config_file> :配置文件路径
    2. <local_filename> :本地文件路径
    3. [storage_ip:port] :(可选参数)
    4. [store_path_index] :(可选参数)
  2. 指令使用

    [root@tracker fdfs]# fdfs_upload_file /etc/fdfs/client.conf 上传的文件路径
    

    在这里插入图片描述
    注: 上传文件后会返回文件在FastDFS中的唯一文件标识,即卷名+文件名

3. 下载指令 — fdfs_download_file
  1. 指令参数

    fdfs_download_file <config_file> <file_id> [local_filename] [<download_offset> <download_bytes>]
    

    注: 参数含义:

     1. <config_file> :配置文件路径
     2. <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名
     3. [local_filename] :文件下载地址
     4. <download_offset> :(可选参数)文件下载开始时间
     5. <download_bytes> :(可选参数)文件下载的字节数
    
  2. 指令使用

    [root@tracker fdfs]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKhygVwnUUOAcgYHCCZp2Ahsb3g964.rpm /root/java/xxx.rpm
    

    在这里插入图片描述

4. 查看文件信息指令 — fdfs_file_info
  1. 指令参数

    fdfs_file_info <config_file> <file_id>
    

    注: 参数含义:

    1. <config_file> :配置文件路径
    2. <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名
  2. 指令使用

    [root@tracker fdfs]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKhygVwnUUOAcgYHCCZp2Ahsb3g964.rpm
    

    在这里插入图片描述

5. 删除指令 — fdfs_delete_file
  1. 指令参数

    fdfs_delete_file <config_file> <file_id>
    

    注: 参数含义:

    1. <config_file> :配置文件路径
    2. <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名
  2. 指令使用

    [root@tracker fdfs]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKhygVwnUUOAcgYHCCZp2Ahsb3g964.rpm
    

    在这里插入图片描述

    注: 删除指令使用后,文件在该卷中的所有备份都会被删除,因为卷内的存储节点会相互同步,故慎用。

6. 文件追加上传及追加内容指令 — fdfs_upload_appender & fdfs_append_file

注意: 文件追加命令 fdfs_append_file 的使用前提是 要追加内容的文件是通过 文件追加上传命令 fdfs_upload_appender 上传的

  1. 指令参数

    fdfs_upload_appender <config_file> <local_filename>
    

    注: 参数含义:

    1. <config_file> :配置文件路径
    2. <local_filename> :将要上传的可追加内容的文件的本地路径
    fdfs_append_file <config_file> <appender_file_id> <local_filename>
    

    注: 参数含义:

    1. <config_file> :配置文件路径
    2. <appender_file_id> :需要追加内容的文件在FastDFS中的唯一文件标识,即卷名+文件名
    3. <local_filename> :将要追加到其他文件的文件的本地路径
  2. 指令使用

    # ====== 上传需要追加内容的文件 ======
    [root@tracker fdfs]# fdfs_upload_appender /etc/fdfs/client.conf /root/java/a.txt
    # ====== 追加内容 ======
    [root@tracker fdfs]# fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/wKhygVwnhXWEcq9VAAAAAJXO_uM135.txt /root/java/b.txt
    

    在这里插入图片描述
    在这里插入图片描述
    注: 上传文件后会返回文件在FastDFS中的唯一文件标识,即**卷名+文件名 **

二、通过javaAPI操作FastDFS

普通java环境下的javaAPI操作

​ FastDFS由ali的架构师yuqing开发的开源项目,并发布在GitHub上,他同时提供了操作FastDFS的 java client SDK—fastdfs-client-java 以及其他附加开源项目(https://github.com/happyfish100)。

1. 获取 fastdfs-client-java的源码
  1. 复制GitHub上源码的URL
    在这里插入图片描述

  2. 在Git的本地版本库打开git的
    在这里插入图片描述

  3. 下载源码到Git的本地版本库

    $ git clone https://github.com/happyfish100/fastdfs-client-java.git
    

    在这里插入图片描述

2. 安装到maven的本地仓库(源码安装)
  1. 进入fastdfs-client-java目录
$ cd fastdfs-client-java/

在这里插入图片描述

  1. 将fastdfs-client-java安装到maven的本地仓库
mvn clean install

在这里插入图片描述

3. 开发实战
1. 创建maven项目
2. 导入fastdfs-client-java的jar包
<dependency>
	<groupId>org.csource</groupId>
	<artifactId>fastdfs-client-java</artifactId>
	<version>1.27-SNAPSHOT</version>
</dependency>
3. 创建配置文件.conf

配置文件名为:fdfs_client.conf(或使用其它文件名xxx_yyy.conf)

内容为:

# 设置追踪服务器ip
tracker_server = 192.168.114.129:22122
4. 创建测试类
package com.dome.test;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * 通过java API操作分布式文件系统FastDFS
 */
public class FastDFSTest {
   
    /**
     * 单元测试之前执行
     */
    //存储节点客户端对象,通过此对象完成对分布式文件系统的种种操作
    private StorageClient storageClient = null;
    @Before
    public void before(){
   
        try {
   
            //加载配置文件
            ClientGlobal.init("fdfs_client.conf");
        } catch (IOException e) {
   
            e.printStackTrace();
        } catch (MyException e) {
   
            e.printStackTrace();
        }
        //创建一个追踪服务器的客户端对象
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = null;
        try {
   
            //获取追踪服务器连接
            trackerServer = tracker.getConnection();
        } catch (IOException e) {
   
            e.printStackTrace(<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值