简体   繁体   English

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

[英]Kafka: This server is not the leader for that topic-partition

Possible duplicate of Kafka - This server is not the leader for that topic-partition but there is no accepted answer nor a clear solution. 可能是Kafka的副本-该服务器不是该主题分区的领导者,但没有公认的答案,也没有明确的解决方案。

I have a simple java program to produce the message to Kafka: 我有一个简单的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));

I am getting the following exception: 我收到以下异常:

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.

When I tried with kafka-console-producer , I am getting the following error: 当我尝试使用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.

When I describe my topic, I have the following information: 当描述主题时,我将获得以下信息:

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

I tried creating a new topic and producing messages as mentioned in Quick start guide then the above steps work well. 我尝试按照快速入门指南中的说明创建一个新主题并生成消息,然后上述步骤可以正常工作。

What correction should I make in My_Topic or producer configuration so that I can publish messages successfully in this topic? 我应该在My_Topic或生产者配置中进行哪些更正,以便可以在此主题中成功发布消息?

Had it been the case of "console client working, but Java program not working" , then the solution of 'changing retry limit' may have helped. 如果“控制台客户端正常运行,但Java程序无法正常运行” ,则“更改重试限制”的解决方案可能会有所帮助。

As both the Java program and the built-in command-line producer are not able to connect to Kafka, I suspect that the problem could be due to stale configuration. 由于Java程序和内置命令行生产器都无法连接到Kafka,我怀疑问题可能是由于配置过时。

(Example: topic deleted and created again with the same name, but with different partition count). (例如:主题已删除,并使用相同的名称重新创建,但分区数不同)。

Deleting the old log files of zookeeper and Kafka and creating the topic again, or creating a topic with a different name will resolve the issue. 删除zookeeper和Kafka的旧日志文件并再次创建主题,或使用其他名称创建主题将解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行Kafka性能流量时出现错误“此服务器不是该主题分区的领导者” - Error “This server is not the leader for that topic-partition” while running Kafka performance traffic org.apache.kafka.common.errors.UnknownTopicOrPartitionException:此服务器未托管此主题分区 - org.apache.kafka.common.errors.UnknownTopicOrPartitionException: This server does not host this topic-partition Kafka主题与分区主题 - Kafka Topic vs Partition topic kafka 获取主题的分区数 - kafka get partition count for a topic Apache Kafka - 关于主题/分区的KafkaStream - Apache Kafka - KafkaStream on topic/partition GlobalKTable - StreamsException:遇到与任何全局状态存储无关的主题分区 - GlobalKTable - StreamsException: Encountered a topic-partition not associated with any global state store Kafka 生产者无法发送带有 NOT_LEADER_FOR_PARTITION 异常的消息 - Kafka producer fails to send messages with NOT_LEADER_FOR_PARTITION exception Apache Kafka主题元数据提取包含错误:{LEADER_NOT_AVAILABLE} - Apache Kafka Topic metadata fetch included errors: {LEADER_NOT_AVAILABLE} 如何在Kafka中验证分区或主题的大小? - How to verify the size of partition or Topic in Kafka? 如何将数据发送到特定的 Kafka 主题分区? - How to send data to particular Kafka topic partition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM