上一節介紹了Flume如何將數據收集到hdfs文件系統上。本節將分享多個agent鏈接配合使用。node
原理圖:apache
操做步驟:centos
一、將centos-aaron-h1的flume複製一份到centos-aaron-h2 sudo scp -r /home/hadoop/apps/apache-flume-1.6.0-bin hadoop@192.168.29.145:/home/hadoop/apps/ 二、進入centos-aaron-h1的Flume配置目錄 cd ~/apps/apache-flume-1.6.0-bin/conf 三、新建配置文件 vi tail-avro-avro-logger.conf 四、在上面的配置文件中添加一下內容 #從tail命令獲取數據發送到avro端口 #另外一個節點可配置一個avro源來中繼數據,發送外部存儲 ################## # Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = exec a1.sources.r1.command = tail -F /home/hadoop/log/test.log a1.sources.r1.channels = c1 # Describe the sink #綁定的不是本機, 是另一臺機器的服務地址, sink端的avro是一個發送端, avro的客戶端, 往hadoop01這個機器上發 a1.sinks = k1 a1.sinks.k1.type = avro a1.sinks.k1.channel = c1 a1.sinks.k1.hostname = master a1.sinks.k1.port = 4141 a1.sinks.k1.batch-size = 2 # Use a channel which buffers events in memory a1.channels.c1.type = memory a1.channels.c1.capacity = 1000 a1.channels.c1.transactionCapacity = 100 # Bind the source and sink to the channel a1.sources.r1.channels = c1 a1.sinks.k1.channel = c1 五、保存上面配置 shift+Z+Z 六、建立Flume監聽的文件所在的文件夾 mkdir /home/hadoop/log 七、創Flume監聽的文件,並寫循環寫入數據 while true do echo 111111 >> /home/hadoop/log/test.log sleep 0.5 done 八、新打開個ssh客戶端執行下列命令查看日誌文件變化【使用大寫的-F是追蹤文件名進行輸出,而小寫-f是inode進行追蹤】 tail -F test.log 九、進入centos-aaron-h2的Flume配置目錄 cd ~/apps/apache-flume-1.6.0-bin/conf 十、新建配置文件 vi avro-hdfs.conf 十一、在上面的配置文件中添加一下內容 #從avro端口接收數據,下沉到logger # Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source #source中的avro組件是接收者服務, 綁定本機 a1.sources.r1.type = avro a1.sources.r1.channels = c1 a1.sources.r1.bind = 0.0.0.0 a1.sources.r1.port = 4141 # Describe the sink a1.sinks.k1.type = logger # Use a channel which buffers events in memory a1.channels.c1.type = memory a1.channels.c1.capacity = 1000 a1.channels.c1.transactionCapacity = 100 # Bind the source and sink to the channel a1.sources.r1.channels = c1 a1.sinks.k1.channel = c1 十二、在centos-aaron-h2啓動flume avro服務 bin/flume-ng agent -c conf -f conf/avro-hdfs.conf -n a1 -Dflume.root.logger=INFO,console 13.在centos-aaron-h1啓動flume avro客戶端 bin/flume-ng agent -c conf -f conf/tail-avro-avro-logger.conf -n a1
效果圖:app
注意點: Flume若是失敗了,必需要重啓agent進程,它會自動記錄上次採集的位置,繼續採集。你們能夠經過寫一個監聽腳原本實現重啓。最後你們能夠參考下《基於Flume的美團日誌收集系統》。ssh