引子:
scribe是facebook開源的日志收集系統,在facebook内部已經得到大量的應用。它能夠從各種日志源上收集日志,存儲到一個中央存儲系統(可以是nfs,分布式檔案系統等)上,以便于進行集中統計分析處理。它為日志的“分布式收集,統一處理”提供了一個可擴充的,高容錯的方案。當中央存儲系統的網絡或者機器出現故障時,scribe會将日志轉存到本地或者另一個位置,當中央存儲系統恢複後,scribe會将轉存的日志重新傳輸給中央存儲系統。其通常與hadoop結合使用,scribe用于向hdfs中push日志,而hadoop通過mapreduce作業進行定期處理。
scribe從各種資料源上收集資料,放到一個共享隊列上,然後push到後端的中央存儲系統上。當中央存儲系統出現故障時,scribe可以暫時把日志寫到本地檔案中,待中央存儲系統恢複性能後,scribe把本地日志續傳到中央存儲系統上。需要注意的是,各個資料源須通過thrift(由于采用了thrift,用戶端可以采用各種語言編寫向scribe傳輸資料(每條資料記錄包含一個category和一個message)。可以在scribe配置用于監聽端口的thrift線程數(預設為3)。在後端,scribe可以将不同category的資料存放到不同目錄中,以便于進行分别處理。後端的日志存儲方 式可以是各種各樣的store,包括file(檔案),buffer(雙層存儲,一個主儲存,一個副存儲),network(另一個scribe服務 器),bucket(包含多個store,通過hash的将資料存到不同store中),null(忽略資料),thriftfile(寫到一個 thrift tfiletransport檔案中)和multi(把資料同時存放到不同store中)。
本文通過3個執行個體程式,分别示範scribe後端存儲為file、network和buffer時的操作方法和流程,示範示例程式位于scribe/examples目錄,目錄結構如下所示:[hadoop@hadoop1 examples]$ ls
example1.conf example2client.conf hdfs_example.conf scribe_cat
example2central.conf hdfs_example2.conf readme scribe_ctrl
一、example1:file
#step_01:建立消息檔案存放目錄
mkdir /tmp/scribetest
#step_02:啟動scribe
src/scribed examples/example1.conf
#step_03:發送消息到scribe
echo "hello world" | ./scribe_cat test
#step_04: 驗證消息記錄
cat /tmp/scribetest/test/test_current
#step_05: 檢查scribe狀态
./scribe_ctrl status
#step_06: 檢視scribe計數
./scribe_ctrl counters
#step_07: 停止scribe運作
./scribe_ctrl stop
二、example2:network
#step_01:建立工作目錄
mkdir /tmp/scribetest2
#step_02:啟動中心scribe程式,服務端口1463,記錄方式為file
src/scribed examples/example2central.conf
#step_03:啟動中心client程式,服務端口1464,存儲模式為network,寫入消息到中心scribe
src/scribed examples/example2client.conf
#step_04:發送消息到client scribe
echo "test message" | ./scribe_cat -h localhost:1464 test2
echo "this message will be ignored" | ./scribe_cat -h localhost:1464 ignore_me
echo "123:this message will be bucketed" | ./scribe_cat -h localhost:1464 bucket_me
#step_05:驗證消息被中心scribe接收和記錄到檔案
cat /tmp/scribetest/test2/test2_current
#step_06:驗證消息分組,不同category的資料存放到不同目錄中
cat /tmp/scribetest/bucket*/bucket_me_current
#step_07:狀态檢查消息計數檢查,如果管理指令不加參數預設為1643
./scribe_ctrl status 1463
./scribe_ctrl status 1464
./scribe_ctrl counters 1463
./scribe_ctrl counters 1464
#step_08:關閉服務程序
./scribe_ctrl stop 1463
./scribe_ctrl stop 1464
三、example3:buffer
#step_01:啟動中心scribe,服務端口1463
#step_02:啟動用戶端scribe,服務端口1464
#step_03:發送消息到用戶端scribe
echo "test message 1" | ./scribe_cat -h localhost:1464 test3
#step_04:驗證消息是否接受,在中心scribe消息存儲目錄查找
cat /tmp/scribetest/test3/test3_current
#step_05:停止中心scribe服務,我們期待看到結果是緩存
#step_06:驗證中心scribe運作狀态
#step_07:發送消息到用戶端-此時消息期待結果是緩存
echo "test message 2" | ./scribe_cat -h localhost:1464 test3
#step_08:逾時用戶端scribe會有報警資訊
#step_09:重新開機中心scribe
#step_10:驗證scribe狀态
#step_10:驗證中心scribe是否接收到緩存的消息
#step_11:關閉服務程序
四、工作流程
通過以上執行個體,我們可以看到scribe核心的工作原理和處理流程,具體流程如下圖所示: