概述
随着kafka的越来越流行,把kafka作为消息(特别是日志)的传输消息队列的越来越多,同时其他各种开源工具都开始跟kafka对接。
在(一)中我们可以了解到,flume是常用语日志采集的工具,而kafka又常用于日志传输,因此对flume支持kafka的需求也很多,而官网也在提供flume的kafka sink,但貌似支持的配置不好,所有我们可以借助广大网页的力量,来实现kafka sink的功能。
使用
- 下载
地址:https://github.com/beyondj2ee/flumeng-kafka-plugin
这是github上网友提供的flume kafka的插件,我在生产环境中也使用到很多场景,还是比较稳定。 - 解压处理
下载后解压,解压目录为:flumeng-kafka-plugin-master,文件夹下flumeng-kafka-plugin目录结构如下:
- conf
- libs
- package
- src
- pom.xml
其中,libs和package包有我们需要的引入的jar包,conf下有示例的flume配置。
3. 配置
将libs和package下的所有jar包都放入flume目录的lib目录下;
配置flume-conf.properties
flume kafka sink配置
############################################
# producer config
###########################################
#agent section
producer.sources = s
producer.channels = c
producer.sinks = r
#source section
producer.sources.s.type = exec
producer.sources.s.command = tail -f /home/user/xxx/xxx.log
producer.sources.s.channels = c
# Each sink type must be defined
producer.sinks.r.type = org.apache.flume.plugins.KafkaSink
producer.sinks.r.metadata.broker.list=127.0.0.1:9092
producer.sinks.r.partition.key=0
producer.sinks.r.partitioner.class=org.apache.flume.plugins.SinglePartition
producer.sinks.r.serializer.class=kafka.serializer.StringEncoder
producer.sinks.r.request.required.acks=0
producer.sinks.r.max.message.size=1000000
producer.sinks.r.producer.type=sync
producer.sinks.r.custom.encoding=UTF-8
producer.sinks.r.custom.topic.name=test
#Specify the channel the sink should use
producer.sinks.r.channel = c
# Each channels type is defined.
producer.channels.c.type = memory
producer.channels.c.capacity = 1000
flume kafka source配置
############################################
# consumer config
###########################################
consumer.sources = s
consumer.channels = c
consumer.sinks = r
consumer.sources.s.type = seq
consumer.sources.s.channels = c
consumer.sinks.r.type = logger
consumer.sinks.r.channel = c
consumer.channels.c.type = memory
consumer.channels.c.capacity = 100
consumer.sources.s.type = org.apache.flume.plugins.KafkaSource
consumer.sources.s.zookeeper.connect=127.0.0.1:2181
consumer.sources.s.group.id=testGroup
consumer.sources.s.zookeeper.session.timeout.ms=400
consumer.sources.s.zookeeper.sync.time.ms=200
consumer.sources.s.auto.commit.interval.ms=1000
consumer.sources.s.custom.topic.name=test
consumer.sources.s.custom.thread.per.consumer=4
- 启动
kafka sink启动命令./bin/flume-ng agent -n producer –conf conf -f conf/flume-conf.properties