卡夫卡分区滞后增加

2024-04-25 20:57:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个应用程序使用Kafka 1.0作为队列。Kafka主题有80个分区和80个用户在运行。(Kafka python消费者)。

通过运行命令:

./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group mygroup  --describe 

我看到其中一个分区被卡在一个偏移量上,并且随着新记录的添加,滞后量不断增加。

上述命令的输出如下所示:

TOPIC                          PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG        CONSUMER-ID                                       HOST

118 mytopic                       37         1924            2782            858        kafka-python-1.3.4-3da99d4d-63e8-4e72-967e-xxxxxxxxxxx/localhost
119 mytopic                       38         2741            2742            1          kafka-python-1.3.4-40b44482-39fc-42d0-8f55-xxxxxxxxxxx/localhost
120 mytopic                       39         2713            2713            0          kafka-python-1.3.4-4121d080-1d7c-4d6b-ac58-xxxxxxxxxxx/localhost
121 mytopic                       40         2687            2688            1          kafka-python-1.3.4-43441f6e-fd35-448e-b791-xxxxxxxxxxx/localhost

这是什么原因?另外,使用reset offsets命令重置偏移量也是不可取的,因为此服务器可能无法定期手动监视。

在Linux m/c中,客户端作为并行进程在后台运行:

consumer = KafkaConsumer('mytopic', group_id='mygroup', bootstrap_servers='localhost:9092',
                     session_timeout_ms=120000, heartbeat_interval_ms=100000, max_poll_records=1,
                     auto_commit_interval_ms=100000, request_timeout_ms=350000, max_partition_fetch_bytes=3*1024*1024,
                     value_deserializer=lambda m: json.loads(m.decode('ascii')))

for message in consumer:
    msg = json.loads(message.value)
    process_message(msg)

Tags: kafka命令localhostmessageconsumergroupbootstrapoffset
1条回答
网友
1楼 · 发布于 2024-04-25 20:57:01

If consumer offset is not moving after some time, then consumer is likely to have stopped. If consumer offset is moving, but consumer lag (difference between the end of the log and the consumer offset) is increasing, the consumer is slower than the producer. If the consumer is slow, the typical solution is to increase the degree of parallelism in the consumer. This may require increasing the number of partitions of a topic.

在卡夫卡docs上阅读更多。

简单地说,你生产的比消耗的多。你需要提高消费率以减少滞后。你需要增加更多的消费者。如果你只是在测试,那么你的消费者是缓慢的。

相关问题 更多 >