首页 文章

Kafka:该服务器不是该主题分区的领导者

提问于
浏览
1

可能重复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 或 生产环境 者配置中进行哪些更正,以便我可以在此主题中成功发布消息?

1 回答

  • 0

    如果是 "console client working, but Java program not working" 的情况,那么'changing retry limit'的解决方案可能有所帮助 .

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

    (示例:主题已删除并再次使用相同名称创建,但具有不同的分区计数) .

    删除zookeeper和Kafka的旧日志文件并再次创建主题或创建具有不同名称的主题将解决该问题 .

相关问题