简体   繁体   English

在 python 中保持 Kafka 消费者存活的最佳实践是什么?

[英]What is the best practice for keeping Kafka consumer alive in python?

Something is puzzling for me when it comes to keeping consumers alive.在保持消费者活力方面,有些事情让我感到困惑。 Let's say I have a topic to which data is constantly being written.假设我有一个不断写入数据的主题。 But, in an hour in a day, there are no new messages.但是,在一天中的一个小时内,没有新消息。 If I had set a timeout for my consumers, when there are no new messages, the consumer will get closed.如果我为消费者设置了超时,当没有新消息时,消费者将关闭。

Now, new messages arrive.现在,新消息来了。 But, there are not consumers alive to consume them.但是,没有活着的消费者来消费它们。

How should I handle such scenarios?我应该如何处理这种情况? My consumers may consume all messages and get closed.我的消费者可能会消费所有消息并关闭。 What is the best way to keep them alive?让他们活着的最好方法是什么? Is there any way to invoke them automatically upon the arrival of new messages?有没有办法在新消息到达时自动调用它们? What are the best practices for such scenarios?此类场景的最佳实践是什么?

Why not just为什么不只是

import time
from confluent_kafka import Consumer


consumer = Consumer({
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'my-consumer-1',
    'auto.offset.reset': 'earliest'
})
consumer.subscribe(['topicName'])

while True:
    try: 
        message = consumer.poll(10.0)

        if not message:
            time.sleep(120) # Sleep for 2 minutes

        if message.error():
            print(f"Consumer error: {message.error()}")
            continue

        print(f"Received message: {msg.value().decode('utf-8')}")
    except:
        # Handle any exception here
        ...
    finally:
        consumer.close()
        print("Goodbye")

I cannot comment on the requirement of "setting a timeout for consumers" , but in most of the cases consumers are supposed to run "forever" and should also be added to consumer groups in a way that they are highly available.我无法评论“为消费者设置超时”的要求,但在大多数情况下,消费者应该“永远”运行,并且还应该以高可用的方式添加到消费者组中。

Use a generator function使用发电机 function

def consumableMessages(self):
    self.kafka.subscribe(self.topic)
    try:
        for message in self.kafka:
            yield message.value.decode("utf-8")
    except KeyboardInterrupt:
        self.kafka.close()

And then we can wait for messages:然后我们可以等待消息:

for message in kafka.consumableMessages():
    print(message)

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

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