那首先我們來分析一下seaweedfs是什么?seaweedfs是一個非常優秀的由 golang 開發的分布式存儲開源項目。它是用來存儲文件的系統,並且與使用的語言無關,使得文件儲存在雲端變得非常方便。seaweedfs也是一個非常優秀的開源項目,Seaweedfs的設計原理是基於 Facebook 的一篇圖片存儲系統的論文Facebook-Haystack。一聽就感覺作者很NB,有興趣的可以去看一看Seaweedfs的源碼,本文就講解seaweedfs的搭建和簡單應用。
seaweedfs的特點:
1 可以成存儲上億的文件(根據你硬盤大小變化)
2 速度剛剛的
一、seaweedfs的搭建
seaweedfs可以編譯安裝(需要VPN,網絡環境不好,最好不用該方法安裝),也可以下載釋放版本鏡像安裝。
1、安裝GO環境
(1)、下載go語言包,go語言包地址
wget 地址
比如:
wget https://golangtc.com/static/go/1.9.2/go1.9.2.linux-amd64.tar.gz
(2)、解壓到指定目錄,並添加環境變量
sudo tar -C /usr/local -xzf go1.9.2.linux-amd64.tar.gz
添加環境變量
#工作目錄 export GOPATH=/opt/go #解壓目錄 export GOROOT=/usr/local/go export GOARCH=386 export GOOS=linux export GOBIN=$GOROOT/bin export GOTOOLS=$GOROOT/pkg/tool/ export PATH=$PATH:$GOBIN:$GOTOOLS
(3)、安裝git、mercurial
sudo apt-get install git suod apt-get install mercurial
(4)、安裝seaweedfs
如果要編譯安裝運行下面命令,沒有VPN,可以忽略下面命令:
go get github.com/chrislusf/seaweedfs/go/weed
下載安裝:seaweedfs地址
wget https://github.com/chrislusf/seaweedfs/releases/download/0.76/linux_amd64.tar.gz sudo tar -zxvf linux_amd64.tar.gz
(5)、配置運行seaweedfs
進入解壓目錄,以守護進程啟動seaweedfs的主服務及集群
nohup ./weed master -mdir=/data/fileData -port=9333 -defaultReplication="001" -ip="ip地址" >>/data/fileData/server_sfs.log & nohup ./weed volume -dir=/data/t_v1 -max=5 -mserver="ip地址:9333" -port=9080 -ip="ip地址" >>/data/t_v1_sfs.log & nohup ./weed volume -dir=/data/t_v2 -max=5 -mserver="ip地址:9333" -port=9081 -ip="ip地址" >>/data/t_v2_sfs.log & nohup ./weed volume -dir=/data/t_v3 -max=5 -mserver="ip地址:9333" -port=9082 -ip="ip地址" >>/data/t_v3_sfs.log &
將上面的ip地址換為具體的ip即可,默認可設為localhost。volume多少可以根據自己的情況添加。mdir、dir是指定文件存儲路徑。
一個 MasterServer 對應三個 VolumeServer ,設置復制模式為 “001” , 也就是在相同 Rack 下復制副本為一份,也就是總共有兩份。如下:
更多的其他配置可以查看文檔
二、seaweedfs的文件應用
訪問服務器ip地址:9333,可以看到如下界面
(1)獲取fid,使用GET或POST訪問路徑http://localhost:9333/dir/assign,獲取fid和上傳文件地址,seaweedfs會返回json的結果。
如下:
curl -X POST http://localhost:9333/dir/assign
得到結果:
{"fid":"1,08e684f060","url":"1ocalhost:9080","publicUrl":"localhost:9080","count":1}
fid就是上傳的標志。
使用url或者publicUrl加上fid,就是文件上傳的地址。如下:
http://localhost:9080/1,08e684f060
接着就可以發起一個PUT或POST請求到上面的地址,把文件上傳上去。
curl -X PUT -F file=@/home/back.png http://localhost:9080/1,08e684f060
結果如下:
{
"name": "back.png", "size": 64300 }
(2)如果要更新一個文件,直接發起POST或者PUT請求到url+fid的地址即可。fid就是保存圖片使用的fid。
(3)刪除圖片發起一個DELETE請求即可。
curl -X DELETE http://localhost:8080/1,08e684f060
(4)查看文件,需要fid。就是剛剛上傳圖片得到的fid。可以將其保存在數據庫當中,查找文件時再調用。
當有多個Volume集群時,可以通過參數指定查看某個卷。
curl http://localhost:9333/dir/lookup?volumeId=2
得到具體的地址:
{"volumeId":"2","locations":[{"url":"localhost:9081","publicUrl":"localhost:9081"}]}
然后訪問具體的路由:
http://localhost:9081/fid
比如:http://localhost:9081/1,08e684f060
可以帶后綴,也可以不帶。
http://localhost:9081/1,08e684f060.jpg
還可以帶一些限定參數:
http://localhost:9081/1,08e684f060.jpg
http://localhost:9081/1,08e684f060.jpg?height=200&width=200
http://localhost:9081/1,08e684f060.jpg?height=200&width=200&mode=fit
http://localhost:9081/1,08e684f060.jpg?height=200&width=200&mode=fill
其實剛剛上傳的是在9080的volume1,9081是volume2。但是通過fid加上具體的volume端口,依舊可以訪問到圖片的地址。
三、seaweedfs客戶端使用
seaweedfs的使用與語言無關,都可以方便進行封裝。github上也有一些開放的client。比如這個java的客戶端。作者把線程分配、HTTP請求、文件操作進行了封裝,更加方便。但是目前作者沒怎么更新了,還有些問題。
我把客戶端的java代碼進行了重新覆寫,上傳到github了,seaweedfs-java-client歡迎大家start。
這里還有一些其他語言的客戶端,大家也可以去學習更改一下:
java-client:https://github.com/Shuyun123/seaweedfs-java-client
php-client:https://github.com/micjohnson/weed-php
node-client:https://github.com/cruzrr/node-weedfs
python-client:https://github.com/darkdarkfruit/python-weed
scala-client:https://github.com/chiradip/WeedFsScalaClient
四、擴展
除了seaweedfs,還有一些其他的文件存儲,比如:bfs(支撐Bilibili的小文件存儲系統),github地址 bfs。感興趣的讀者可以去學習一下。
還有比如阿里的https://github.com/alibaba/tfs,百度的https://github.com/baidu/bfs,fastdfs:https://github.com/happyfish100/fastdfs等。
作者:Anumbrella
鏈接: https://blog.csdn.net/Anumbrella/article/details/78585937