我可以忽略 org.apache.kafka.common.errors.NotLeaderForPartitionExceptions 吗?

Can I ignore org.apache.kafka.common.errors.NotLeaderForPartitionExceptions?

我的 Apache Kafka 生产者 (0.9.0.1) 间歇性地抛出一个

org.apache.kafka.common.errors.NotLeaderForPartitionException

我执行 Kafka 发送的代码类似于此

final Future<RecordMetadata> futureRecordMetadata = KAFKA_PRODUCER.send(new ProducerRecord<String, String>(kafkaTopic, UUID.randomUUID().toString(), jsonMessage));

try {
    futureRecordMetadata.get();
} catch (final InterruptedException interruptedException) {
    interruptedException.printStackTrace();
    throw new RuntimeException("sendKafkaMessage(): Failed due to InterruptedException(): " + sourceTableName + " " + interruptedException.getMessage());
} catch (final ExecutionException executionException) {
    executionException.printStackTrace();
    throw new RuntimeException("sendKafkaMessage(): Failed due to ExecutionException(): " + sourceTableName + " " + executionException.getMessage());
}

我在 catch (final ExecutionException executionException) {} 块中捕获了 NotLeaderForPartitionException

可以忽略这个特殊的异常吗?

我的Kafka消息发送成功了吗?

如果您收到NotLeaderForPartitionException,则您的数据写入成功。

每个主题分区由一个或多个代理存储(有一个领导者;其余代理称为追随者),具体取决于您的复制因子。生产者需要向领导者 Broker 发送新消息(到追随者的数据复制发生在内部)。

您的生产者客户端没有连接到正确的 Broker,即连接到跟随者而不是领导者(或者连接到甚至不再是跟随者的经纪人),并且该经纪人拒绝您的发送请求。如果领导者发生变化但生产者仍然拥有关于哪个代理是分区领导者的过时缓存元数据,就会发生这种情况。