博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
项目开发中经常有一些被嫌弃的小数据,现在全丢给 FastDFS
阅读量:4035 次
发布时间:2019-05-24

本文共 5189 字,大约阅读时间需要 17 分钟。

在我们开发项目的时候,经常会遇到大块数据的问题(2M-100M),比如说保存报表中1w个人的ID号,说实话,这些数据存储在服务器哪里都被嫌弃,放在redis,mongodb中吧,一下子你就会收到报警信息,因为内存满了。。。放在mysql吧???你还得建立一个text字段,也没人这么玩,而且还担心受怕别人来一个 select * ,这速度你懂的。。。直接放到硬盘吧,没扩展性,你1T大小的硬盘又能怎样,照样撑爆你,放在hadoop里面吧,对.net程序员来说,没有这个缘分,好不容易微软有一个.net hadoop sdk,说放弃就放弃了,兼具以上各种特性,最后目光只能落到FastDFS上了。

一:FastDFS

fastDFS的本意是一个分布式的文件系统,所以大家可以上传各种小文件,包括这篇聊到的那些一个点一个点的数据,同样你也可以认为是一些小文件,接下来我画一下它的大概架构图:

我来解释一下:

  • fastDFS是按照Group的形式对file进行分组存储的,这里的group1你可以理解成C盘,group2理解成D盘,所有的数据都是在Group来划分的。

  • 为了提高读取性能和热备份,我们把group1放到了两台机器上,大概可能觉得有点浪费,对吧,事实就是这样。

  • 为了提高扩展性,因为单机是有存储上限的,这时候你可以再新建一个group2,也就是D盘,放到另外机器上,这样你就扩容了,对吧。

  • trackerServer主要用来保存group和storage的一些状态信息,主要和client端进行交互,返回正确的storeage server地址,这个和hadoop的namenode其实是同一个角色的。

  • 这里要注意的一个地方就是,client端在存储file的时候,需要告诉trackerserver,你需要存储到哪一个group中,比如group1还是group2?

二:下载安装【CentOS】

为了方便测试,这里我部署到一台CentOS上

1. 安装步骤

1) 下载fastDFS基础包:https://github.com/happyfish100/libfastcommon/releases

2) 然后下载fast源码包:https://github.com/happyfish100/fastdfs/releases

3) wget之后,先把libfastcommon给安装一下

tar -xzvf V1.0.36cd libfastcommon-1.0.36./make.sh && ./make.sh install

再把fastdfs安装一下。

tar -xzvf V5.11cd fastdfs-5.11./make.sh &&./make.sh install

这样的话,我们的fast就算安装好了,因为是默认安装,所以配置文件是在 /etc/fdfs 目录下,启动服务在/etc/init.d下。

[root@localhost ~]# cd /etc/fdfs[root@localhost fdfs]# ls client.conf client.conf.sample storage.conf.sample storage_ids.conf.sample tracker.conf.sample[root@localhost fdfs]# cd /etc/init.d[root@localhost init.d]# lsfdfs_storaged  fdfs_trackerd  functions  netconsole  network  README[root@localhost init.d]#

然后再把两个storage.conf.sample 和 tracker.conf.sample中copy出我们需要配置的文件。

[root@localhost fdfs]# cp storage.conf.sample storage.conf[root@localhost fdfs]# cp tracker.conf.sample tracker.conf[root@localhost fdfs]# lsclient.conf  client.conf.sample  storage.conf  storage.conf.sample  storage_ids.conf.sample  tracker.conf  tracker.conf.sample[root@localhost fdfs]# 

4) tracker.conf 配置

这个配置文件,主要是配置里面的base_path。

# the base path to store data and log filesbase_path=/usr/fast/fastdfs-5.11/data/tracker

指定完路径之后,我们创建一个data文件夹和tracker文件夹。

5) storage.conf 配置

这个配置文件,我们主要配置三样东西。

  • 本storage服务器的groupname,大家看过架构图应该也明白了,对吧。

  • 为了提高磁盘读写,可以指定本groupname的file存储在哪些磁盘上。

  • 指定和哪一台trackerserver进行交互。

# the name of the group this storage server belongs to## comment or remove this item for fetching from tracker server,# in this case, use_storage_id must set to true in tracker.conf,# and storage_ids.conf must be configed correctly.group_name=group1# the base path to store data and log filesbase_path=/usr/fast/fastdfs-5.11/data/storage# path(disk or mount point) count, default value is 1store_path_count=1# store_path#, based 0, if store_path0 not exists, it's value is base_path# the paths must be existstore_path0=/usr/fast/fastdfs-5.11/data/storage/0#store_path1=/home/yuqing/fastdfs2# tracker_server can ocur more than once, and tracker_server format is#  "host:port", host can be hostname or ip addresstracker_server=192.168.23.152:22122

然后在data目录下创建storage和0文件夹

6) 启动 FastDFS,可以看到22122的端口已经启动了,说明搭建成功

[root@localhost ~]# /etc/init.d/fdfs_trackerd startStarting fdfs_trackerd (via systemctl):                    [  OK  ][root@localhost ~]# /etc/init.d/fdfs_storaged startStarting fdfs_storaged (via systemctl):                    [  OK  ][root@localhost 0]# netstat -tlnpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      4346/fdfs_trackerd  tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1786/dnsmasq        tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1129/sshd           tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1128/cupsd          tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      4171/fdfs_storaged  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1556/master         tcp6       0      0 :::22                   :::*                    LISTEN      1129/sshd           tcp6       0      0 ::1:631                 :::*                    LISTEN      1128/cupsd          tcp6       0      0 ::1:25                  :::*                    LISTEN      1556/master         [root@localhost 0]# 

三:使用C#客户端

在github上有一个C#的客户端,大概可以使用一下:https://github.com/smartbooks/FastDFS.Client   或者通过nuget上搜一下:

class Program    {        static void Main(string[] args)        {            ConnectionManager.InitializeForConfigSection(new FastDfsConfig()            {                FastDfsServer = new List
()                {                    new FastDfsServer()                    {                         IpAddress="192.168.2.25",                          Port=22122                    }                }            });            var storageNode = FastDFSClient.GetStorageNode("group1");            var path = FastDFSClient.UploadFile(storageNode, new byte[10000], ".txt");            var rsp = FastDFSClient.DownloadFile(storageNode, path);            Debug.WriteLine("上传的文件返回路径:{0}, 下载获取文件大小:{1}", path, rsp.Length);        }    }

好了,本篇就说这么多,希望对你有帮助。

转载地址:http://btkdi.baihongyu.com/

你可能感兴趣的文章
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>