我的 Kafka 消费者轮询速度比我预期的要快。有没有我可以更改的配置,让它在 fetch.max.wait.ms
中一直等待?
我将 fetch.max.wait.ms 设置为某个秒数 (5)。我将 fetch.min.bytes
设置为一些较大的字节数 (99,988,800)。
我阅读了文档(但可能遗漏了一些内容):
https://kafka.apache.org/documentation/
fetch.min.bytes
The minimum amount of data the server should return for a fetch request. If insufficient data is available the request will wait for that much data to accumulate before answering the request. The default setting of 1 byte means that fetch requests are answered as soon as a single byte of data is available or the fetch request times out waiting for data to arrive. Setting this to something greater than 1 will cause the server to wait for larger amounts of data to accumulate which can improve server throughput a bit at the cost of some additional latency.
fetch.max.wait.ms
The maximum amount of time the server will block before answering the fetch request if there isn't sufficient data to immediately satisfy the requirement given by fetch.min.bytes.
fetch.max.wait.ms=5000,
fetch.min.bytes=99988800
根据我的配置选项和数据集,我希望对 poll
的调用在返回任何记录之前始终阻塞 5 秒。
相反,对 poll
的调用有时会在不到一秒的时间内解决,并且总是有一些少量的记录。
这是示例运行的输出:
// send 100 records
// doesn't matter how
// timestamp -> records received
// (date, hour and minute are not shown, just the relevant seconds.millis)
32.475 -> 10
33.392 -> 12
34.116 -> 16
37.477 -> 16
38.395 -> 18
39.118 -> 17
42.479 -> 7
43.397 -> 4
没有延迟真的接近 5 秒。
最佳答案
使用属性 fetch.max.wait.ms=5000
你说:“即使没有足够的数据可供选择,也不要等待超过 5 秒”。在执行轮询之前,您没有指定最小秒数。您可以通过启动 2 个 kafka 消费者来测试此行为,并在其中一个中设置 fetch.max.wait.ms=20000
并在另一个中保留默认值。您会看到,在使用默认设置的消费者中,您几乎会立即收到消息,而在使用 fetch.max.wait.ms=20000
的消费者中,您将不得不稍等片刻。我通过设置 fetch.max.wait.ms=20000
在我的机器上进行了尝试,有时接收记录需要 15 秒,有时需要 10 秒等等,但不会超过 20 秒。
关于java - 为什么我的 Kafka 消费者投票这么快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56909524/