今天就在向大家演示一个案例,Flume单源数据多出口案例(注意:此案例是sink组),与我写的另一篇文章:略有不同。话不多说,一切用图说话:

flume 传输多个文件 flume 多个sink_负载均衡

 单Source、Channel多Sink(负载均衡)单Source、Channel多Sink(负载均衡),此图来自官网哈

我们的需求:使用Flume-1监控文件变动,Flume-1将变动内容传递给Flume-2,Flume-2负责存储到HDFS。同时Flume-1将变动内容传递给Flume-3,Flume-3也负责存储到HDFS,这里Flume-1是从控制台输入数据。

flume 传输多个文件 flume 多个sink_hadoop_02

 好了,图上表达的很明白,source端接收来自控制台的数据输入,然后经过channle传输到sink端(注意和我上面提到的另一篇文章中的区别),这里用到的是负载均衡,sink发送数据不会立刻发送到flume-2和flume-3的source端口,这里是先发送到sink发送器,发送器判断哪一个flume负载小一点,并且会把数据发送给负载小的哪一个,所以此案例主要讲的就是负载均衡这一点,与前一篇文章的选择器有所不同。

第一步:在flume目录下面创建job工作目录,用来存放配置文件,创建flume-netcat-flume.conf,添加以下配置信息:

#在这里只有一个channel缓存,但是有两个sink,把他看成一个组
# Name the components on this agent
a1.sources = r1
a1.channels = c1
a1.sinkgroups = g1
a1.sinks = k1 k2

# Describe/configure the source
a1.sources.r1.type = netcat//发送工具
a1.sources.r1.bind = hadoop101//发送数据的端口
a1.sources.r1.port = 44444//发送数据的端口
#sink的类型是负载均衡
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.backoff = true
a1.sinkgroups.g1.processor.selector = round_robin
a1.sinkgroups.g1.processor.selector.maxTimeOut=10000

# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop101
a1.sinks.k1.port = 4141

a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hadoop101
a1.sinks.k2.port = 4142

# Describe the channel
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.sinkgroups.g1.sinks = k1 k2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c1

第二步:创建flume-flume-console1.conf,配置上级Flume输出的Source,输出是到本地控制台。

# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1

# Describe/configure the source
a2.sources.r1.type = avro
a2.sources.r1.bind = hadoop101
a2.sources.r1.port = 4141

# Describe the sink
a2.sinks.k1.type = logger

# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1

第三步:创建flume-flume-console2.conf,配置上级Flume输出的Source,输出是到本地控制台。

# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c2

# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop101
a3.sources.r1.port = 4142

# Describe the sink
a3.sinks.k1.type = logger

# Describe the channel
a3.channels.c2.type = memory
a3.channels.c2.capacity = 1000
a3.channels.c2.transactionCapacity = 100

# Bind the source and sink to the channel
a3.sources.r1.channels = c2
a3.sinks.k1.channel = c2

第四步:执行配置文件

bin/flume-ng agent -c conf/ -n a3 -f /opt/module/flume-1.7.0/job1/flume-flume-console1.conf -Dflume.root.logger=INFO,console
bin/flume-ng agent -c conf/ -n a3 -f /opt/module/flume-1.7.0/job1/flume-flume-console2.conf-Dflume.root.logger=INFO,console 
 bin/flume-ng agent --conf conf/ --name a1 --conf-file /opt/module/flume-1.7.0/job1/flume-netcat-flume.conf

第五步:我们可以在控制台输入内容,可以看到另外两个控制台输出的event包。

现在可以在hadoop101注浆机上面执行nc hadoop101 44444命令就可以向44444端口发送消息了,在flume1和flume2控制台可以看到输出。

Notes:使用netcat工具向端口发送数据需要先安装netcat工具:

sudo yum install -y nc 命令可以直接安装

sudo netstat -tunlp | grep 44444可以判断44444端口是否被占用。