最近要準(zhǔn)備做一個(gè)系統(tǒng)監(jiān)控小項(xiàng)目决侈,簡(jiǎn)要分析了需求之后決定用當(dāng)下比較火的flume-kafka-spark-springboot-echarts來做日志統(tǒng)計(jì)螺垢、分析、展示赖歌。于是先學(xué)習(xí)一下flume枉圃。
flume簡(jiǎn)介
簡(jiǎn)而言之flume就是一個(gè)收集器,根據(jù)官網(wǎng)(https://flume.apache.org/FlumeUserGuide.html)的介紹:“Apache Flume is a distributed, reliable, and available system for efficiently collecting, aggregating and moving large amounts of log data from many different sources to a centralized data store.”可知flume是一個(gè)可擴(kuò)展庐冯、可靠孽亲、可用性高、高效率的日志采集展父、聚集返劲、移動(dòng)的系統(tǒng)。
flume的結(jié)構(gòu)就是agent栖茉,agent中包含了source,channel,sink三個(gè)部分分別針對(duì)數(shù)據(jù)的流入篮绿、聚合/臨時(shí)存儲(chǔ)、輸出轉(zhuǎn)移衡载。
可在官網(wǎng)下查看關(guān)于flume的使用簡(jiǎn)介搔耕,對(duì)應(yīng)做兩個(gè)簡(jiǎn)單的小實(shí)驗(yàn)
首先下載flume對(duì)應(yīng)的tar包,解壓后再~./bash_profile中加上$FLUME_HOME及對(duì)應(yīng)的PATH配置
使用Flume的關(guān)鍵就是寫配置文件
- 配置Source
- 配置Channel
- 配置Sink
- 把三個(gè)組件連接起來
先在官網(wǎng)的指南上拿下所給的“Starting an agent”:
$ bin/flume-ng agent -n $agent_name -c conf -f conf/flume-conf.properties.template
再拿下所給的“A simple example”:
# example.conf: A single-node Flume configuration
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
# 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
在此解釋一下:
a1: agent名稱
r1: source的名稱
k1: sink的名稱
c1: channel的名稱
而flume-ng命令的參數(shù)可以直接在終端輸入flume-ng就有解釋了:
先做第一個(gè)小實(shí)驗(yàn):監(jiān)控TCP端口數(shù)據(jù)
在官網(wǎng)指南的導(dǎo)航欄中找到“flume source”下面的“NetCat TCP Source”根據(jù)下面的說明和之前的simple example把配置修改為:
a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
a1.sinks.k1.type = logger
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
在$FLUME_HOME/conf 下創(chuàng)建文件example.conf
修改之前的flume-ng命令:
flume-ng agent -n a1 -c $FLUME_HOME/conf -f $FLUME_HOME/conf/example.conf -Dflume.root.logger=info,console
PS:-Dflume.root.logger=info,console是將信息打印到終端,為了方便查看
此時(shí)終端輸入剛才命令后:
這時(shí)候已經(jīng)在監(jiān)控了弃榨!我們可以再打開一個(gè)終端菩收,使用telnet在端口44444輸入一些數(shù)據(jù)檢驗(yàn)一下:
這時(shí)候再看看之前的終端
每一條數(shù)據(jù)在flume中都變成了一個(gè)event鲸睛,包括了headers和body娜饵。
第二個(gè)小實(shí)驗(yàn):監(jiān)控文件
由于我需要的業(yè)務(wù)場(chǎng)景是監(jiān)控日志文件,第一個(gè)實(shí)驗(yàn)不能滿足我的需求官辈,于是做第二個(gè)實(shí)驗(yàn)箱舞。
先隨便找個(gè)目錄創(chuàng)建了一個(gè)mylog.log,用于監(jiān)控
這次是找到指南里的“Exec Source”拳亿,對(duì)應(yīng)修改配置文件example.conf:
a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /Users/cheng/Documents/mylog.log
a1.sinks.k1.type = logger
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
PS:這里使用了命令監(jiān)控:tail -F /Users/cheng/Documents/mylog.log在實(shí)時(shí)觀察我的文件是否被改動(dòng)
再執(zhí)行剛才的flume-ng命令晴股,這時(shí)候我們?cè)趍ylog.log中寫一點(diǎn)數(shù)據(jù)試試:
這時(shí)候觀察監(jiān)控的終端:
以上算是flume的一些入門吧~
吐槽一下為毛給的simple example的端口是44444肺魁,不吉利电湘!