天天看點

【C#|.NET】利用FastDFS打造分布式檔案系統

關于分布式檔案系統 之前已經寫過一些随筆 不過沒怎麼用心 本篇詳細的整理一下

背景

  海量存儲、系統負載的遷移、伺服器吞吐的瓶頸等等 讓檔案系統獨立于業務系統 提高整個項目的擴充性以及可維護性

  目前主流的方案 MFS FASTDFS GFS LUSTRE HADOOP等等

  我選擇的是FASTDFS 用一句廣告語來說 “免費、快速、找得到”。FASTDFS的作者是淘寶的資深架構師餘慶,很诙諧、很有愛!!!其他方案還沒玩過 暫不評論。

簡介 

  FastDFS是一款開源的輕量級分布式檔案系統純C實作,支援Linux、FreeBSD等UNIX系統類google FS,不是通用的檔案系統,隻能通過專有API通路,目前官方提供了C、Java和PHP API為網際網路應用量身定做,追求高性能和高擴充性,FastDFS可以看做是基于檔案的key value pair存儲系統,稱作分布式檔案存儲服務更為合适。

  特點:

  分組存儲,靈活簡潔

對等結構,不存在單點

檔案ID由FastDFS生成,作為檔案通路憑證。FastDFS不需要傳統的name server

和流行的web server無縫銜接,FastDFS已提供apache和nginx擴充子產品

大中小檔案均可以很好支援,支援海量小檔案存儲

存儲伺服器上可以儲存檔案附加屬性

  名詞解釋:

  Tracker Server:跟蹤伺服器,主要做排程工作,在通路上起負載均衡的作用。在記憶體中記錄叢集中group和storage server的狀态資訊,是連接配接Client和Storage server的樞紐。 因為相關資訊全部在記憶體中,Tracker server的性能非常高,一個較大的叢集(比如上百個group)中有3台就足夠了。

   Storage Server:存儲伺服器,檔案和檔案屬性(meta data)都儲存到存儲伺服器上。

實踐-服務端

  系統:ubuntu

  開發工具:vim

  web服務:nginx

  基于socket自定義通信協定

  服務端的安裝參考官方文檔 有不懂的可以聯系蟲子 這裡說下問題比較多的2個地方 一個是libevent的版本問題 另一個是ubuntu最新版本中對于libpthread等庫檔案的存放位置問題

  安裝完fastdfs以後 假設你的服務端程式安裝在/usr/local目錄 

  我們會在bin目錄下找到以下檔案

  

  storage伺服器啟動指令 /usr/local/bin/fdfs_storaged /FastDFS/conf/storage.conf

  tracker伺服器啟動指令 /usr/local/bin/fdfs_trackerd /FastDFS/conf/tracker.conf

  我們運作monitor檢視下配置資訊

<a href="http://blog.51cto.com/dubing/760388#">?</a>

<code>group count: 1</code>

<code>Group 1:</code>

<code>group name = test</code>

<code>free space = 5 GB</code>

<code>storage server count = 2</code>

<code>active server count = 2</code>

<code>storage_port = 23000</code>

<code>storage_http_port = 0</code>

<code>store path count = 1</code>

<code>subdir count per path= 3</code>

<code>current write server index = 0</code>

<code>    </code><code>Host 1:</code>

<code>        </code><code>ip_addr = 192.168.234.139 (ubuntu)  ACTIVE</code>

<code>        </code><code>total storage = 9GB</code>

<code>        </code><code>free storage = 5GB</code>

<code>        </code><code>total_upload_count = 2</code>

<code>        </code><code>success_upload_count = 2</code>

<code>        </code><code>total_set_meta_count = 0</code>

<code>        </code><code>success_set_meta_count = 0</code>

<code>        </code><code>total_delete_count = 0</code>

<code>        </code><code>success_delete_count = 0</code>

<code>        </code><code>total_download_count = 0</code>

<code>        </code><code>success_download_count = 0</code>

<code>        </code><code>total_get_meta_count = 0</code>

<code>        </code><code>success_get_meta_count = 0</code>

<code>        </code><code>total_create_link_count = 0</code>

<code>        </code><code>success_create_link_count = 0</code>

<code>        </code><code>total_delete_link_count = 0</code>

<code>        </code><code>success_delete_link_count = 0</code>

<code>        </code><code>last_heart_beat_time = 2012-01-05 18:45:50</code>

<code>        </code><code>last_source_update = 2012-01-05 01:20:28</code>

<code>        </code><code>last_sync_update = 1969-12-31 16:00:00</code>

<code>        </code><code>last_synced_timestamp= 1969-12-31 16:00:00</code>

<code>    </code><code>Host 2:</code>

<code>        </code><code>ip_addr = 192.168.234.140  ACTIVE</code>

<code>        </code><code>total storage = 18GB</code>

<code>        </code><code>free storage = 12GB</code>

<code>        </code><code>total_upload_count = 16</code>

<code>        </code><code>success_upload_count = 16</code>

<code>        </code><code>last_source_update = 2012-01-05 01:54:02</code>

 storage.conf整理漢化版

<a href="http://blog.51cto.com/dubing/760388#">expand source</a>

 tracker.conf整理漢化版

 Web服務

  FastDfs本身提供了組的概念,不同的組可以用不同的域名,如果是圖檔等類型資源,利用多個子域名在網站優化中是很有幫助的。不過如果你想在一個域名下實作多個伺服器的分布式方案,可以利用nignx的反向代理來做urlrewrite。

  舉個簡單的例子

<code>server {</code>

<code>         </code><code>listen       80;</code>

<code>         </code><code>server_name 192.168.234.140;</code>

<code>    </code><code>index index.html index.htm index.php;  </code>

<code>    </code><code>root  /app/test;</code>

<code>    </code><code>location /g1{</code>

<code>         </code><code>proxy_pass http://192.168.234.139;</code>

<code>       </code><code>}</code>

<code>}</code>

 分布式算法

  和memcached這些分布式系統不同。Fastdfs的分布式算法是在服務端實作,Fish也在不斷改良着算法。最新的是avl樹。不過貌似有整rbtree的趨勢。

 負載均衡,同步

  fastdfs還内置了組内的同步功能,不過我覺得對于我的項目可用性不大,因為我要實作的是狹義的分布式檔案系統隻要能保證海量的可擴充存儲方案就可以了,同步、負載均衡之類的就交給專業的運維們吧。

  同步功能沒有開關配置,在同步時間設定上作手腳就可以了。

  sync_start_time,sync_end_time

 實踐-用戶端  

  系統:windowsXP

  開發工具:VS2008

 首先我們來看下基本功能

找張mm圖檔  -_____-

 上傳成功 對比一下

  然後再看看分布式結果

192.168.234.139上

192.168.234.140上

web上浏覽

性能方面 和伺服器本身的帶寬 吞吐都有關系 不過作為檔案系統 對穩定性的要求更大 而fastdfs的很多執行個體都證明了這一點

看完上面 是不是覺得很容易入手 當然 fastdfs的深度應用還有很多 

對于.net下 fastdfs的應用 大家有什麼疑問可以聯系我

本篇先到此 希望對大家有幫助

本文轉自 熬夜的蟲子  51CTO部落格,原文連結:http://blog.51cto.com/dubing/760388

繼續閱讀