Kafka Producer处理逻辑
Kafka Producer产生数据发送给Kafka Server,具体的分发逻辑及负载均衡逻辑,全部由producer维护。
Kafka结构图
Kafka Producer默认调用逻辑
默认Partition逻辑
1、没有key时的分发逻辑
每隔 topic.metadata.refresh.interval.ms 的时间,随机选择一个partition。这个时间窗口内的所有记录发送到这个partition。
发送数据出错后也会重新选择一个partition
2、根据key分发
对key求hash,然后对partition数量求模
Utils.abs(key.hashCode) % numPartitions |
如何获取Partition的leader信息(元数据)
决定好发送到哪个Partition后,需要明确该Partition的leader是哪台broker才能决定发送到哪里。
具体实现位置
kafka.client.ClientUtils#fetchTopicMetadata |
实现方案
1、从broker获取Partition的元数据。由于Kafka所有broker存有所有的元数据,所以任何一个broker都可以返回所有的元数据
2、broker选取策略:将broker列表随机排序,从首个broker开始访问,如果出错,访问下一个
3、出错处理:出错后向下一个broker请求元数据
注意
- Producer是从broker获取元数据的,并不关心zookeeper。
- broker发生变化后,producer获取元数据的功能不能动态变化。
- 获取元数据时使用的broker列表由producer的配置中的 metadata.broker.list 决定。该列表中的机器只要有一台正常服务,producer就能获取元数据。
- 获取元数据后,producer可以写数据到非 metadata.broker.list 列表中的broker
错误处理
producer的send函数默认没有返回值。出错处理有EventHandler实现。
DefaultEventHandler的错误处理如下:
- 获取出错的数据
- 等待一个间隔时间,由配置 retry.backoff.ms 决定这段时间长短
- 重新获取元数据
- 重新发送数据
出错重试次数由配置 message.send.max.retries 决定
所有重试全部失败时,DefaultEventHandler会抛出异常。代码如下
if(outstandingProduceRequests.size >0) { producerStats.failedSendRate.mark() val correlationIdEnd = correlationId.get() error("Failed to send requests for topics %s with correlation ids in [%d,%d]" .format(outstandingProduceRequests.map(_.topic).toSet.mkString(","), correlationIdStart, correlationIdEnd-1)) thrownewFailedToSendMessageException("Failed to send messages after "+ config.messageSendMaxRetries +" tries.", null) } |
相关推荐
Apache Kafka实战.pdf..
Spring for Apache Kafka API。 Spring for Apache Kafka 开发文档。
Apache Kafka源码剖析 PDF较大,分5份上传!一起解压即可。
This book is here to help you get familiar with Apache Kafka and to solve your challenges related to the consumption of millions of messages in publisher-subscriber architectures. It is aimed at ...
Apache Kafka Apache Kafka Apache Kafka Apache Kafka
Apache Kafka is a popular distributed streaming platform that acts as a messaging queue or an enterprise messaging system. It lets you publish and subscribe to a stream of records and process them in ...
Building Data Streaming Applications with Apache Kafka 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Learning Apache Kafka Second Edition provides you with step-by-step, practical examples that help you take advantage of the real power of Kafka and handle hundreds of megabytes of messages per second ...
Streaming Architecture New Designs Using Apache Kafka and MapR Streams
Apache Kafka 官方文档中文版,Apache Kafka 官方文档中文版
apache kafka技术内幕 和 apacke kafka源码分析2本PDF 电子书 网盘下载
Kafka是一个对于像Hadoop的一样的日志数据和离线分析系统,但又要求实时处理的限制,这是一个可行的解决方案。 1.Kafka集群包含一个或多个服务器,这种服务器被称为broker 2.Partition是物理上的概念,每个Topic...
。Apache Kafka Cookbook(PACKT,2015)
with no previous knowledge of Apache Kafka. As the book progresses, the difficulty level increases. The second half of this cookbook is about configuration; this is advanced material for those who ...
Apache Kafka(带书签)
Apache Kafka实战epu.rar
Kafka 是由 LinkedIn 开发的一个分布式的消息系统, 使用 Scala 编写, 它以可水平扩展和高吞吐率而被广泛使用。 目前越来越多的开源分布式处理系统如Cloudera、 Apache Storm、Spark 都支持与 Kafka 集成。
Apache Kafka-大数据实时处理平台介绍.docx
Apache Kafka源码剖析 PDF较大,分5份上传!一起解压即可。