Flume-负载均衡和故障转移案例

r囧r小猫 2022-10-04 13:57 259阅读 0赞

负载均衡和故障转移

在这里插入图片描述

使用Flume监控一个端口,其sink组中的sink分别对接Flume2和Flume3,采用FailoverSinkProcessor,实现故障转移的功能。其中Flume2和Flume3的Sink直接采用Logger方便查看和测试。

在这里插入图片描述

FailoverSinkProcessor:故障转移处理器维护了一个发送Event失败的sink的列表,保证有一个sink是可用的来发送Event。

配置及说明如下:

故障转移机制的工作原理是将故障sink降级到一个池中,在池中为它们分配冷却期(超时时间),默认30s。 在到达冷却时间之后,它将恢复到实时池。在冷却期中的sink不被考虑使用,只有恢复到实时池中的才能通过优先级被考虑使用。

sink具有与之相关的优先级,数值越大,优先级越高。 如果在发送Event时Sink发生故障,会继续尝试下一个具有最高优先级的sink。 例如,有100、80、50三个优先级的sink,优先级100的sink发生了故障则使用优先级有80的sink。如果未指定优先级,则根据配置中的顺序来选取。

要使用故障转移选择器,不仅要设置sink组的选择器为failover,还有为每一个sink设置一个唯一的优先级数值。 可以使用 maxpenalty 属性设置故障转移时间的上限(毫秒)。

在这里插入图片描述

Flume1配置文件如下:

  1. # Name the components on this agent
  2. a1.sources = r1
  3. a1.sinks = k1 k2
  4. a1.channels = c1
  5. # Describe/configure the source
  6. a1.sources.r1.type = netcat
  7. a1.sources.r1.bind = localhost
  8. a1.sources.r1.port = 44444
  9. # Describe the sink
  10. a1.sinks.k1.type = avro
  11. a1.sinks.k1.hostname = hadoop113
  12. a1.sinks.k1.port = 4441
  13. a1.sinks.k2.type = avro
  14. a1.sinks.k2.hostname = hadoop113
  15. a1.sinks.k2.port = 4442
  16. # Sink Group
  17. # channel对多个sink,默认是负载均衡,且是轮询的
  18. a1.sinkgroups = g1
  19. a1.sinkgroups.g1.sinks = k1 k2
  20. a1.sinkgroups.g1.processor.type = failover
  21. a1.sinkgroups.g1.processor.priority.k1 = 5
  22. a1.sinkgroups.g1.processor.priority.k2 = 10
  23. a1.sinkgroups.g1.processor.maxpenalty = 10000
  24. # Use a channel which buffers events in memory
  25. a1.channels.c1.type = memory
  26. a1.channels.c1.capacity = 1000
  27. a1.channels.c1.transactionCapacity = 100
  28. # Bind the source and sink to the channel
  29. a1.sources.r1.channels = c1
  30. a1.sinks.k1.channel = c1
  31. a1.sinks.k2.channel = c1

Flume2配置文件如下:

  1. # Name the components on this agent
  2. a2.sources = r2
  3. a2.sinks = k2
  4. a2.channels = c2
  5. # Describe/configure the source
  6. a2.sources.r2.type = avro
  7. a2.sources.r2.bind = hadoop113
  8. a2.sources.r2.port = 4441
  9. # Describe the sink
  10. a2.sinks.k2.type = logger
  11. # Use a channel which buffers events in memory
  12. a2.channels.c2.type = memory
  13. a2.channels.c2.capacity = 1000
  14. a2.channels.c2.transactionCapacity = 100
  15. # Bind the source and sink to the channel
  16. a2.sources.r2.channels = c2
  17. a2.sinks.k2.channel = c2

Flume3配置文件如下:

  1. # Name the components on this agent
  2. a3.sources = r2
  3. a3.sinks = k2
  4. a3.channels = c2
  5. # Describe/configure the source
  6. a3.sources.r2.type = avro
  7. a3.sources.r2.bind = hadoop113
  8. a3.sources.r2.port = 4442
  9. # Sink
  10. a3.sinks.k2.type = logger
  11. # Use a channel which buffers events in memory
  12. a3.channels.c2.type = memory
  13. a3.channels.c2.capacity = 1000
  14. a3.channels.c2.transactionCapacity = 100
  15. # Bind the source and sink to the channel
  16. a3.sources.r2.channels = c2
  17. a3.sinks.k2.channel = c2

通过nc localhost 44444连接到44444端口,依次输入hello、word、hahahah,结果全部在Flume3中输出了。
在这里插入图片描述

此时我们关闭Flume3,依次输入test、failover,结果全在Flume中输出了。

在这里插入图片描述

说明故障转移成功了。

负载均衡的话只需要将Sink组的SinkProcessor设置成load_balance即可,其他的和故障转移一模一样

  1. # Sink Group
  2. # channel对多个sink,不设置的话默认是负载均衡,且是轮询的
  3. a1.sinkgroups = g1
  4. a1.sinkgroups.g1.sinks = k1 k2
  5. a1.sinkgroups.g1.processor.type = load_balance
  6. a1.sinkgroups.g1.processor.backoff = true
  7. ## 这里设置成random
  8. a1.sinkgroups.g1.processor.selector = random

运行结果如下:

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,259人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Flume ——故障转移

    Sink groups允许组织多个sink到一个实体上。 Sink processors能够提供在组内所有Sink之间实现负载均衡的能力,而且在失败的情况下能够进行故障转移从一