代码之家  ›  专栏  ›  技术社区  ›  Mohammad Faisal Reza

卡夫卡:这个服务器不是那个主题分区的领导者

  •  2
  • Mohammad Faisal Reza  · 技术社区  · 6 年前

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

    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 回复  |  直到 6 年前
        1
  •  0
  •   Gopinath    6 年前

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

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

    删除zookeeper和Kafka的旧日志文件并重新创建主题,或者使用不同的名称创建主题将解决此问题。