关于kafka生产消息的问题,求教

在网上看到有几种发送消息的方法

1, 其中,在 php-rdkafka-doc 文档中有写到使用:

for ($i = 0; $i < 10; $i++) {

$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message $i");
$producer->poll(0);

}

for ($flushRetries = 0; $flushRetries < 10; $flushRetries++) {

$result = $producer->flush(10000);
if (RD_KAFKA_RESP_ERR_NO_ERROR === $result) {
    break;
}

}

if (RD_KAFKA_RESP_ERR_NO_ERROR !== $result) {

throw new \RuntimeException('Was unable to flush, messages might be lost!');

}

由 poll -> flush

然后看到网上有朋友说 flush会造成严重堵塞问题;但是说能保证数据完整性

2, 在其他朋友笔记记录

for($i = 0; $i < 100; $i++) {

$producer->send([
    [
        'topic' => 'test1',
        'value' => 'test1....message.',
        'key' => '',
    ],
]);

}

现在有个问题就是:这二种写法之间,如何选择? 哪个更可靠

阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进