不死鸟.亚历山大.狼崽子

不死鸟.亚历山大.狼崽子

1 案例说明

企业中应用程序部署后会将日志写入到文件中,可以使用Flume从各个日志文件将日志收集到日志中心以便于查找和分析。

2 使用Exec Soucre

Exec Source通过指定命令监控文件的变化,加粗属性为必须设置的。

添加配置文件exec-log.conf

# 定义agent名称为a1
# 设置3个组件的名称
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# 配置source类型为exec,命令为 tail -F app.log
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F conf/app.log

# 配置sink类型为Logger
a1.sinks.k1.type = logger

# 配置channel类型为内存,内存队列最大容量为1000,一个事务中从source接收的Events数量或者发送给sink的Events数量最大为100
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 将source和sink绑定到channel上
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动Flume

bin/flume-ng agent -n a1 -c ./ -f conf/exec-log.conf -Dflume.root.logger=INFO,console

可以查看agent控制台接收到了最新的日志

Apache Flume(4):日志文件监控-LMLPHP

但是以上方法有重复消费的问题,每次启动都会全量读取log文件里的全部数据,下面我们解决重复消费问题。

3 解决重复消费问题

4 新增dir-log.conf

# 定义agent名称为a1
# 设置3个组件的名称
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# 配置source类型为TAILDIR
a1.sources.r1.type = TAILDIR
a1.sources.r1.positionFile = /home/flume/position.json
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /home/flume/conf/app.log
a1.sources.r1.filegroups.f2 = /home/flume/conf/logs/.*log

# 配置sink类型为Logger
a1.sinks.k1.type = logger

# 配置channel类型为内存,内存队列最大容量为1000,一个事务中从source接收的Events数量或者发送给sink的Events数量最大为100
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 将source和sink绑定到channel上
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动Flume

bin/flume-ng agent -n a1 -c ./ -f conf/dir-log.conf -Dflume.root.logger=INFO,console
12-16 05:25