前言

理解一下Kafka的读的自动提交功能。

找到了一篇专门介绍这个功能的文章,选择主要的内容进行一下翻译和做笔记。

正文

Understanding the ‘enable.auto.commit’ Kafka Consumer property

Kafka Consumers read messages from a Kafka topic, its not a hard concept to get your head around. But behind the scenes there’s a lot more going on than meets the eye.

Say we’re consuming messages from a Topic and our Consumer crashes. Once we realise that the world isn't ending, we recover from the crash and we start consuming again. We start receiving messages exactly where we left off from, its kinda neat.

假设我们正在从一个 Topic 中消费消息,这个时候我们的这个消费者(客户端)宕机了。我们意识到这不是世界的末日,我们可以从宕机中恢复,重新开始消费。我们可以从我们上一次离开的地方重新接收消息,这非常灵巧。

There’s two reasons as to why this happens. One is something referred to as the “Offset” and the other is a couple of default Consumer values.

发生这样的事情是因为两个原因。一个是一个叫 “Offset” 的东西,另外一个是一些 Consumer 的默认的值。

So whats an Offset?

The Offset is a piece of metadata, an integer value that continually increases for each message that is received in a partition. Each message will have a unique Offset value in a partition.

Offset 是一块元数据,一个整数,会针对每一个 partition 上接收到的消息而持续增长。每一个消息在一个 partition 上将会有唯一的一个Offset。

I use Keys in some of my projects, some of them I don’t ;)

So as you can see here, each message has a unique Offset, and that Offset represents the position of that message in that particular partition.

上面介绍了一下Kafka的offset是什么,offset是记录每条消息在partition里面的位置的。

When a Consumer reads the messages from the Partition it lets Kafka know the Offset of the last consumed message. This Offset is stored in a Topic named _consumer_offsets, in doing this a consumer can stop and restart without forgetting which messages it has consumed.

这里讲,offset会被存在一个叫做_consumer_offsets的主题中,这样来帮助消费者记录处理到哪里了。

When we create our Consumers, they have a set of default properties which we can override or we can just leave the default values in effect.

There are two properties that are driving this behaviour.

有两个属性需要关注。

enable.auto.commit
auto.commit.interval.ms

The first property enable.auto.commit has a default value of true and the second property auto.commit.interval.ms has a default value of 5000. These values are correct for Blizzards node-rdkafka client and the Java KafkaConsumer client but other libraries may differ.

enable.auto.commit 的默认值是 true;就是默认采用自动提交的机制。

auto.commit.interval.ms 的默认值是 5000,单位是毫秒。

So by default every 5 seconds a Consumer is going to commit its Offset to Kafka or every time data is fetched from the specified Topic it will commit the latest Offset.

这样,默认5秒钟,一个 Consumer 将会提交它的 Offset 给 Kafka,或者每一次数据从指定的 Topic 取回时,将会提交最后一次的 Offset。

Now in some scenarios this is the ideal behaviour but on other scenarios its not.

这样,在某些场景下,这是理想的表现,但是在其他场景下,并不是。

Say our Consumer is processing a message with an Offset of 100 and whilst processing it the Consumer fetches some more data, the Offset is commit and then the Consumer crashes. Upon coming back up it will start consuming messages from the most recent committed Offset, but how can we safely say that we haven’t lost messages and the Offset of the new message isn't later then the one of the message been processed?

这么说,我们的 Consumer 正在消费一个 Offset 是100的消息,同时这个 Consumer 取回了一些数据,这个 Offset 提交了,然后 Consumer 崩溃了。在我们回来的时候,我们会重新从最新提交的 Offset 去进行消息的消费,但是我们如何能安全地说,我们没有丢失消息,并且这个新消息的 Offset 不会比刚刚被处理的那个消息靠后呢?

What we can do is commit the Offset of messages manually after processing them. This give us full control over when we consider a message dealt with, processed and ready to let Kafka know that.

解决这个问题的方案就是我们手动地提交这个 Offset,在处理完这些消息之后。这给与了我们完全的控制,什么时候去处理一个消息,什么时候去让 Kafka 知道这个。

Firstly we have to change the value of the enable.auto.commit property.

enable.auto.commit: false

When we change this property the auto.commit.interval.ms value isnt taken into consideration.

So now we can commit our Offset manually after the processing has taken place and if the Consumer crashes whilst processing a message it will start consuming from that same Offset, no messages lost.

我们把这个参数设置为 false ,就会由我们自己手动地来处理这个事情。

Both the clients mentioned earlier in this article have methods exposed to commit the Offset.

For further reading on the clients check out the links below.

如果 enable.auto.commit 设置成 false,那么 auto.commit.interval.ms 也就不被再考虑了

JSDoc: Class: KafkaConsumer
KafkaConsumer class for reading messages from Kafka This is the main entry point for reading data from Kafka. You…blizzard.github.io

KafkaConsumer (kafka 0.10.2.1 API)
To avoid this, we will manually commit the offsets only after the corresponding records have been inserted into the…kafka.apache.org

If anyone wants any more information on Kafka or Consumers get in touch on Twitter.

Cheers,

Danny

https://twitter.com/danieljameskay

参考

https://medium.com/@danieljameskay/understanding-the-enable-auto-commit-kafka-consumer-property-12fa0ade7b65

https://kafka.apache.org/0102/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html 这里是官网介绍如何使用consumer

理解 Kafka 消费者属性的 enable.auto.commit相关推荐

  1. 容易被误会的 Kafka 消费者属性 enable.auto.commit

    前言 理解一下Kafka的读的自动提交功能. 找到了一篇专门介绍这个功能的文章,选择主要的内容进行一下翻译和做笔记. 自动提交参数auto.commit的设置 Understanding the 'e ...

  2. Kafka之enable.auto.commit使用解析

    通过字面意思我们不难理解这是kafka的自动提交功能. 配置消费者(配置ENABLE_AUTO_COMMIT_CONFIG为 true 配置自动提交) enable.auto.commit 的默认值是 ...

  3. springboot和kafka结合其中enable.auto.commit等于false失效

    事件描述 公司使用的是Spring Cloud工作的微服务框架.其中做了SpringBoot和kafka的结合.但是意外的是enable.auto.commit参数设置成了false,kafka的of ...

  4. kafka消费者如何读同一生产者消息_Kafka系列3:深入理解Kafka消费者

    上面两篇聊了Kafka概况和Kafka生产者,包含了Kafka的基本概念.设计原理.设计核心以及生产者的核心原理.本篇单独聊聊Kafka的消费者,包括如下内容:消费者和消费者组 如何创建消费者 如何消 ...

  5. 怎么理解 Kafka 消费者与消费组之间的关系?

    与生产者对应的是消费者,应用程序可以通过 KafkaConsumer 来订阅主题,并从订阅的主题中拉取消息.不过在使用 KafkaConsumer 消费消息之前需要先了解消费者和消费组的概念,否则无法 ...

  6. 怎么理解Kafka消费者与消费组之间的关系?

    与生产者对应的是消费者,应用程序可以通过 KafkaConsumer 来订阅主题,并从订阅的主题中拉取消息.不过在使用 KafkaConsumer 消费消息之前需要先了解消费者和消费组的概念,否则无法 ...

  7. Kafka快速入门(Kafka消费者)

    Kafka 消费者 1. Kafka 消费方式 2 Kafka 消费者工作流程 2.1 消费者总体工作流程 2.2 消费者组原理 Consumer Group(CG):消费者组,由多个consumer ...

  8. (转)Kafka 消费者 Java 实现

    转自: Kafka 消费者 Java 实现 - 简书应用程序使用 KafkaConsumer向 Kafka 订阅 Topic 接收消息,首先理解 Kafka 中消费者(consumer)和消费者组(c ...

  9. kafka基础篇(四)——kafka消费者客户端

    一.入门程序 先上代码,从代码入手,讲解kafka消费者客户端的细节. public class HelloKafkaConsumer {public static void main(String[ ...

最新文章

  1. 工程能力提升管理之道
  2. 每个设计师应该阅读的8本书
  3. 阿里云消息队列 RocketMQ、Kafka 荣获金融级产品稳定性测评 “先进级” 认证
  4. Unix基本操作指令备忘
  5. ThinkPHP模型连接数据库 查询 ajax
  6. Android系统的体系结构、开发语言及源码结构
  7. 空间复杂度 用什么符号表示_什么是大O符号解释:时空复杂性
  8. 【浏览器】Firefox插件AdblockPlus屏蔽广告
  9. CSS定位和浮动(吸顶、居中)
  10. mac 下安装java, jmeter, ant, jenkins,使用jmeter+ant+jenkins 接口测试集成工具,发送html报告到邮箱中
  11. CS之攻击菜单详解-后门生成与上线
  12. 幼儿园计算机信息技术培训总结,幼儿园教师信息技术培训总结
  13. 大众点评社区运营攻略
  14. java木马源码_用Java编写木马程序【附源代码下载】
  15. jmeter手动添加cookie及线程间cookie共享的2种方法
  16. 【​观察】“数字广东”背后的力量 腾讯云创新政务服务新模式
  17. 火焰图(FlameGraph)的使用
  18. html名人名言页面,网页制作:关于生命的名言警句 ― 名人名言  一品故事网,Www.07938.Come...
  19. GD32F30x系列系统及存储器架构
  20. python 移动一个文件或目录

热门文章

  1. googiehost免费空间申请
  2. c语言关键词中英翻译机编程,C语言关键字中英翻译机.doc
  3. E931.96人体感应控制IC自学笔记
  4. 网络安全信息与动态周报(8月30日-9月5日)
  5. leetcode Rotate Image
  6. 为什么戏说php,戏说PHP——1. 1切的开始
  7. Microsoft Exchange Server 2007: Tony Redmond's Guide to Successful Implementation
  8. JavaCV-学习笔记一
  9. Anaconda的安装
  10. 【八步拿捏】Aaqus有限元分析及减震复材建模计算/力学分析等多个SCI案例复现(附源码解析)...