简体   繁体   English

卡夫卡使用者关闭后丢失消息状态

[英]Kafka consumer losing state of messages after shutdown

Thanks for taking time to answer the question. 感谢您抽出宝贵时间回答问题。 I am using kafka with a python consumer. 我在与Python使用者一起使用kafka。 Everything works great when the consumer is up and running and messages get pushed to kafka which are then read by the consumer. 消费者启动并运行时,一切正常,消息被推送到kafka,然后由消费者读取。

However, if the consumer goes down for whatever reason, when it comes back up, it only reads the NEW messages that are posted to kafka after the consumer is back up. 但是,如果使用者由于某种原因而停机,则在其恢复时,它只会读取在使用者备份后发布到kafka的新消息。 The messages between shutdown-poweron are lost, that is, the consumer does not read these messages after it comes back up. 关机-开机之间的消息会丢失,也就是说,使用方在恢复后不会阅读这些消息。

consumer = KafkaConsumer(..)

is what I use to create the consumer. 这就是我用来创建消费者的方式。

What client are you using? 您正在使用什么客户端? Maybe it is necessary to set the start offset for the consumer. 也许有必要为使用者设置起始偏移量。 Have a look at the seek() function and auto-commit setting. 看一下seek()函数和自动提交设置。 May my codes help, but maybe we use different consumer classes (mine: http://kafka-python.readthedocs.org/en/latest/apidoc/kafka.consumer.html ): 也许我的代码有帮助,但是也许我们使用不同的使用者类(我的: http : //kafka-python.readthedocs.org/en/latest/apidoc/kafka.consumer.html ):

def connect(self):
        '''Initialize Kafka Client and Consumer.'''
        try:
            print "Try to init KafkaClient:", self.Brokers
            self.__kafka_client = KafkaClient( self.Brokers )


            print "Try to init Kafka Consumer."
            self.__consumer = SimpleConsumer(
                    self.__kafka_client,
                    self.GroupID,
                    self.Topic,
                    auto_commit = True,
                    partitions=self.Partitions,
                    auto_commit_every_n = 100,
                    auto_commit_every_t=5000,
                    fetch_size_bytes=4096,
                    buffer_size=4096,
                    max_buffer_size=32768,
                    iter_timeout=None,
                    auto_offset_reset='largest' )


            print "Set the starting offset."
            self.__consumer.seek(0, self.OffsetMode)


self.__consumer.seek(0, 0) =>start reading from the beginning of the queue.
self.__consumer.seek(0, 1) =>start reading from current offset.
self.__consumer.seek(0, 2) =>skip all the pending messages and start reading only new messages (** maybeyour case**).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM