FastDFS---分布式文件存储系统安装与使用

  最早接触FastDFS还是一年之前,那会儿我们的一个项目中就用到了这个技术,但是关于FastDFS的一些基础知识也一直没有去研究,今天趁着有时间想啃一下这块骨头。

概念
1.什么是FastDFS?

  FastDFS是阿里的技术大佬余庆在2008年用C语言实现的一款分布式文件管理系统。它主要是用来解决大容量存储和负载均衡问题,支持横向组扩展和同步热备。其主要的功能有以下四点:

  • 文件存储
  • 文件同步
  • 文件上传
  • 文件下载
2.为什么要用FastDFS?

  FastDFS的开发初衷是希望解决互联网大数据量、高访问量的环境下,文件的高效访问和高效存储管理等问题。比如图片文件、视频文件我们都能够使用FastDFS这种存储管理系统来统一处理。

3.FastDFS系统结构?

  如下图所示:FastDFS系统结构分为两部分:Tracker clusterStorage cluster,其中tracker用来负责文件访问的调度和负载平衡。而storage用来存储文件。storage作为最终的存储部分一般会以集群的形式进行搭建,一个storage cluster又分为很多个组,每个组又相当于是一个小的集群。同组内的storage server之间会进行数据同步,而不同组之间不会进行通信。 我们通过tracker可以实现对storage的负载均衡。

安装
4.FastDFS安装?

连接Linux系统:

1. 安装gcc环境

yum install gcc-c++

2. 安装libevent

yum -y install libevent

3. 下载libfastcommon

下载libfastcommon连接

将下载好的libfastcommon使用Xftp上传到linux系统的/usr/local/下,并进行解压缩tar -zxvf libfastcommon 1.0.43.tar.gz

4. 依次执行以下命令:

cd /usr/local/libfastcommon-1.0.43

./make.sh

./make.sh install

5. 下载fastdfs安装包

FastDFS下载连接

使用Xftp将安装包上传到linux的/usr/local/目录下

tar -zxvf fastdfs-6.06.tar.gz

cd /usr/local//fastdfs-6.06

./make.sh

./make.sh install

使用命令查看是否安装成功:
ll /usr/bin/fdfs*

cd conf

cp http.conf /etc/fdfs/

cp mime.types /etc/fdfs/

6. 修改配置

FastDFS安装后配置文件位于/etc/fdfs/目录下,修改该目录下的配置文件;

cd /etc/fdfs

把所有的.sample后缀都去掉,使用 mv 命令改文件名,去掉文件名后缀;

mv client.conf.sample client.conf

mv client.conf.sample client.conf

mv storage.conf.sample storage.conf

mv storage_ids.conf.sample storage_ids.conf

mv tracker.conf.sample tracker.conf

修改tracker.conf文件

vim tracker.conf

修改如下:
在这里插入图片描述
创建存储数据的文件夹

mkdir -p /home/fdfs/storage/files

修改storage.conf文件

vim storage.conf

分别修改下面几个地方:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7. 启动FastDFS

启动FastDFS一定需要先启动tracker,然后再启动storage。

在任意目录下执行:

fdfs_trackerd /etc/fdfs/tracker.conf

fdfs_storaged /etc/fdfs/storage.conf

然后去/home/fdfs/storage下面看看应该会新增加两个文件夹(多一个data和logs
在这里插入图片描述
查看storage是否已经登记到了tracker下:

fdfs_monitor /etc/fdfs/storage.conf 

8. 测试文件上传

依次执行以下命令:

mkdir /home/fdfs/client

vim /etc/fdfs/client.conf

在这里插入图片描述
在这里插入图片描述
然后在/root目录下准备一个a.txt,然后输入以下命令

fdfs_test /etc/fdfs/client.conf upload /root/a.txt

在这里插入图片描述
返回的url粘贴到浏览器中就可以访问到该文件了

9. 下载FastDFS的Nginx模块扩展包

cd /usr/local/

fastdfs-nginx-module下载连接

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

cd /usr/local/fastdfs-nginx-module-1.22/src

cp mod_fastdfs.conf /etc/fdfs/

vim /etc/fdfs/mod_fastdfs.conf

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

cd /usr/local/nginx-1.17.0

./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src

make

make install

配置Nginx请求转发:

vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

10. 启动Nginx

cd /usr/local/nginx/sbin/

./nginx

11. IDEA中的文件上传测试:
创建一个springboot项目,并在项目的resource目录下新建一个fastdfs-client.properties配置文件
在这里插入图片描述
将下列配置粘贴到配置文件中:

fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = 47.93.181.229:22122
fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000

pom.xml文件中引入依赖:

 <dependency>
      <groupId>net.oschina.zcx7878</groupId>
      <artifactId>fastdfs-client-java</artifactId>
      <version>1.27.0.0</version>
    </dependency>

然后在Test类中写文件上传代码进行测试:


  @Test
  void upload() {
    try {
      ClientGlobal.initByProperties("fastdfs-client.properties");
      TrackerClient trackerClient = new TrackerClient();
      TrackerServer trackerServer = trackerClient.getConnection();
      StorageClient1 client = new StorageClient1(trackerServer, null);
      NameValuePair nvp[] = null;
      //上传到文件系统
      String[] fileId = client.upload_file("D:\\1.jpg", "jpg",
          nvp);
      System.out.print("地址为:");
      String path = "";
      for (String str : fileId) {   // 组名+磁盘地址
        path = path + str + "/";
      }
      // 进行地址处理并输出
      System.out.println(path.substring(0,path.length()-1));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

在这里插入图片描述
在浏览器中访问该文件:
在这里插入图片描述成功!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值