1. 程式人生 > >把項目中那些惡心的無處存儲的大塊數據都丟到FastDFS之快速搭建

把項目中那些惡心的無處存儲的大塊數據都丟到FastDFS之快速搭建

git books 讀取 服務 only 默認安裝 幫助 set dfs

原文:把項目中那些惡心的無處存儲的大塊數據都丟到FastDFS之快速搭建

   在我們開發項目的時候,經常會遇到大塊數據的問題(2M-100M),比如說保存報表中1w個人的ID號,他就像一個腫瘤一樣,存儲在服務器哪裏都

覺得惡心,放在redis,mongodb中吧,一下子你就會收到報警,因為內存滿了。。。放在mysql吧???你還得建立一個text字段,和一些小字段混在一起,

還是有點惡心,還得單獨拆出來,還得怕有些sql不規範的人挺喜歡select * 的,這速度挺惡心的呀。。。直接放到硬盤吧,沒擴展性,你1T大小的硬盤又能

怎樣,照樣撐爆你,放在hadoop裏面吧,對.net程序員來說,沒有這個緣分,好不容易微軟有一個.net hadoop sdk,說放棄就放棄了,兼具以上各種特性,

最後目光只能落到FastDFS上了。

一: FastDFS

fastDFS的本意是一個分布式的文件系統,所以大家可以上傳各種小文件,包括這篇和大家說到的那些一坨一坨的數據,同樣你也可以認為是一些小文件,

下面我畫一下它的大概架構圖:

技術分享圖片

我來解釋一下:

1. fastDFS是按照Group的形式對file進行分組存儲的,這裏的group1你可以理解成C盤,group2理解成D盤,所有的數據都是在Group來劃分的。

2. 為了提高讀取性能和熱備份,我們把group1放到了兩臺機器上,大概可能覺得有點浪費,對吧,事實就是這樣。

3. 為了提高擴展性,因為單機是有存儲上限的,這時候你可以再新建一個group2,也就是D盤,放到另外機器上,這樣你就擴容了,對吧。

4. trackerServer主要用來保存group和storage的一些狀態信息,主要和client端進行交互,返回正確的storeage server地址,這個和hadoop的

namenode其實是同一個角色的。

5. 這裏要註意的一個地方就是,client端在存儲file的時候,需要告訴trackerserver,你需要存儲到哪一個group中,比如group1還是group2?

二:下載安裝【CentOS】

為了方便測試,這裏我部署到一臺CentOS了。

1. 下載fastDFS基礎包: https://github.com/happyfish100/libfastcommon/releases

技術分享圖片

2. 然後下載fast源碼包:https://github.com/happyfish100/fastdfs/releases

技術分享圖片

3. wget之後,先把libfastcommon給安裝一下

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

再把fastdfs安裝一下。

tar -xzvf V5.11
cd 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]# ls
fdfs_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]# ls
client.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 files
base_path=/usr/fast/fastdfs-5.11/data/tracker

指定完路徑之後,我們創建一個data文件夾和tracker文件夾。

5. storage.conf 配置

這個配置文件,我們主要配置三樣東西。

1. 本storage服務器的groupname,大家看過架構圖應該也明白了,對吧。

2. 為了提高磁盤讀寫,可以指定本groupname的file存儲在哪些磁盤上。

3. 指定和哪一臺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 files
base_path=/usr/fast/fastdfs-5.11/data/storage

# path(disk or mount point) count, default value is 1
store_path_count=1

# store_path#, based 0, if store_path0 not exists, its value is base_path
# the paths must be exist
store_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 address
tracker_server=192.168.23.152:22122

然後在data目錄下創建storage和0文件夾

6.啟動 FastDFS,可以看到22122的端口已經啟動了,說明搭建成功

[root@localhost ~]# /etc/init.d/fdfs_trackerd start
Starting fdfs_trackerd (via systemctl):                    [  OK  ]
[root@localhost ~]# /etc/init.d/fdfs_storaged start
Starting fdfs_storaged (via systemctl):                    [  OK  ]
[root@localhost 0]# netstat -tlnp
Active 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<FastDfsServer>()
                {
                    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);
        }
    }

技術分享圖片

好了,本篇就說這麽多了,希望對你有幫助。

把項目中那些惡心的無處存儲的大塊數據都丟到FastDFS之快速搭建