【问题标题】:Kafka: This server is not the leader for that topic-partitionKafka:此服务器不是该主题分区的领导者
【发布时间】:2023-04-05 09:48:01
【问题描述】:

Kafka - This server is not the leader for that topic-partition 可能重复,但没有公认的答案,也没有明确的解决方案。

我有一个简单的 java 程序来向 Kafka 生成消息:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("retries", 1);
props.put("batch.size", 16384);
props.put("linger.ms", 100);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "com.company.project.KafkaValueSerializer");
MyMessage message = new MyMessage();
Producer<String, MyMessage> producer = new KafkaProducer<>(props);
Future<RecordMetadata> future =
    producer.send(new ProducerRecord<String, MyMessage>("My_Topic", "", message));

我收到以下异常:

Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.NotLeaderForPartitionException: This server is not the leader for that topic-partition.
    at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.valueOrError(FutureRecordMetadata.java:94)
    at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:64)
    at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:29)
    at 
Caused by: org.apache.kafka.common.errors.NotLeaderForPartitionException: This server is not the leader for that topic-partition.

当我尝试使用 kafka-console-producer 时,我收到以下错误:

D:\kafka_2.11-0.10.2.0\bin\windows>kafka-console-producer.bat --broker-list localhost:9092  --topic My_Topic
hello
[2018-10-25 17:05:27,225] WARN Got error produce response with correlation id 3 on topic-partition My_Topic-0, retrying (2 attempts left). Error: NOT_LEADER_FOR_PARTITION (org.apache.kafka.clients.producer.internals.Sender)
[2018-10-25 17:05:27,335] WARN Got error produce response with correlation id 5 on topic-partition My_Topic-0, retrying (1 attempts left). Error: NOT_LEADER_FOR_PARTITION (org.apache.kafka.clients.producer.internals.Sender)
[2018-10-25 17:05:27,444] WARN Got error produce response with correlation id 7 on topic-partition My_Topic-0, retrying (0 attempts left). Error: NOT_LEADER_FOR_PARTITION (org.apache.kafka.clients.producer.internals.Sender)
[2018-10-25 17:05:27,544] ERROR Error when sending message to topic My_Topic with key: null, value: 5 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
org.apache.kafka.common.errors.NotLeaderForPartitionException: This server is not the leader for that topic-partition.

当我描述我的主题时,我有以下信息:

Topic:My_Topic        PartitionCount:1        ReplicationFactor:1     Configs:
        Topic: My_Topic       Partition: 0    Leader: 0 Replicas: 0     Isr: 0

我尝试创建一个新主题并生成Quick start guide 中提到的消息,然后上述步骤运行良好。

我应该在My_Topic或生产者配置中进行哪些更正,以便我可以在该主题中成功发布消息?

【问题讨论】:

  • 您可以尝试增加重试次数并告诉我结果吗?例如`props.put("retries", 10);`
  • @GiorgosMyrianthous:没有任何改变。它仍然具有相同的异常和堆栈跟踪。

标签: java apache-kafka kafka-producer-api


【解决方案1】:

如果是“控制台客户端工作,但 Java 程序不工作”,那么“更改重试限制”的解决方案可能会有所帮助。

由于 Java 程序和内置的命令行生产者都无法连接到 Kafka,我怀疑问题可能是由于配置陈旧。

(例如:主题被删除并重新创建,名称相同,但分区数不同)。

删除zookeeper和Kafka的旧日志文件重新创建主题,或者创建一个不同名称的主题都可以解决问题。

【讨论】:

    猜你喜欢
    • 2018-05-25
    • 2020-03-30
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多