Kafka 新生产者超时

Kafka new producer timeout

我正在使用新的 kafka 生产者客户端并将 timeout.ms 属性 设置为 50 毫秒。

这里是生产者中使用的完整配置:

props.put("acks", "1");
props.put("buffer.memory", "33554432");
props.put("retries", "1");
props.put("batch.size", "16384");
props.put("client.id", "foo");
props.put("linger.ms", "0");
props.put("timeout.ms", "50");

在某些高负载时刻请求平均响应时间为4秒,但我没有收到任何超时错误。

有谁知道这个timeout是怎么计算的,什么时候开始计算,什么时候结束?有没有办法配置超时从生产者的发送方法被调用的那一刻开始?

新的 timeout.ms 属性 与生产者的 ack 配置一起工作。例如考虑以下情况

ack = all
timeout.ms = 3000

在这种情况下 ack = all 意味着领导者在收到对全套同步副本 (ISR) 的确认之前不会做出响应,并且获得此确认的最长等待时间将是 3000 ms.如果它在给定时间内没有收到预期数量的确认,它将 return 出错。

另请注意,此 属性 不考虑网络延迟。

来自文档页面:

The configuration controls the maximum amount of time the server will wait for acknowledgments from followers to meet the acknowledgment requirements the producer has specified with the acks configuration. If the requested number of acknowledgments are not met when the timeout elapses an error will be returned. This timeout is measured on the server side and does not include the network latency of the request.

所以在你的情况下ack=1(我不是 100% 确定这一点,如果适用,可以接受任何更正)如果领导者无法回应(在没有将记录写入自己的日志之后)等待所有关注者的完全确认)在 50 毫秒内应该抛出错误。

在新的 Kafka 2.0 Producer API 中,您可以使用以下属性之一: https://kafka.apache.org/documentation/#producerconfigs

查看 https://kafka.apache.org/20/javadoc/index.html?org/apache/kafka/clients/producer/KafkaProducer.html

上的用法示例

超时现在由 max.block.ms 属性 定义。