简体   繁体   English

Kafka 这个主题分区没有领导者,因为我们正在进行领导选举

[英]Kafka There is no leader for this topic-partition as we are in the middle of a leadership election

just starting learning Kafka.刚开始学习卡夫卡。 Im trying to setup a small kafka cluster including 2 brokers.我正在尝试设置一个包含 2 个代理的小型 kafka 集群。 I was successfull in sending messages to my topic when both brokers are up.当两个代理都启动时,我成功地向我的主题发送消息。 I want to test the behavior of my cluster when one of 2 brokers goes done.我想在 2 个代理之一完成时测试我的集群的行为。 I stopped my primary broker (Kafka1) using docker stop kafka1, and i tried then to send a message to my cluster to see if my producer is able to understand that he need to send to kafka2 as kafka1 is down.我使用 docker stop kafka1 停止了我的主要代理(Kafka1),然后我尝试向我的集群发送一条消息,以查看我的生产者是否能够理解他需要发送到 kafka2,因为 kafka1 已关闭。

However i constantly receiving the below errors:但是我不断收到以下错误:

{"level":"ERROR","timestamp":"2022-07-19T18:59:46.891Z","logger":"kafkajs","message":"[Connection] Response Metadata(key: 3, version: 6)","broker":"localhost:39092","clientId":"my-app","error":"There is no leader for this topic-partition as we are in the middle of a leadership election","correlationId":1,"size":144} {"level":"ERROR","timestamp":"2022-07-19T18:59:46.891Z","logger":"kafkajs","message":"[Connection] 响应元数据(key: 3, version : 6)","broker":"localhost:39092","clientId":"my-app","error":"这个主题分区没有领导,因为我们正在进行领导选举" ,"correlationId":1,"size":144}

below is my producer code:以下是我的生产者代码:

const kafka = new Kafka({
clientId: 'my-app',
brokers: ['localhost:29092', 'localhost:39092'],
})
const producer = kafka.producer({ createPartitioner: Partitioners.LegacyPartitioner })

await producer.connect()

await producer.send({
  topic: 'coverageEvolved',
  messages: [
    { value: JSON.stringify(bodyActiveMq), key: bodyActiveMq[0].roamPartner},
  ],
})

await producer.disconnect()

and below is my docker-compose-file:以下是我的 docker-compose-file:

version: '2'
services:
    zookeeper:
    image: confluentinc/cp-zookeeper:latest
    restart: unless-stopped
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 22181:2181
    volumes:
      - ./zookeeper/data:/var/lib/zookeeper/data
    kafka-1:
        image: confluentinc/cp-kafka:latest
        depends_on:
          - zookeeper
        ports:
            - 29092:29092
        restart: unless-stopped
        environment:
          KAFKA_BROKER_ID: 1
          KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
          KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka- 
    1:9092,PLAINTEXT_HOST://localhost:29092
          KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
          KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
          KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 2
        volumes:
          - ./kafka1/data:/var/lib/kafka/data
      kafka-2:
        image: confluentinc/cp-kafka:latest
        depends_on:
          - zookeeper
        ports:
          - 39092:39092
        restart: unless-stopped
        environment:
          KAFKA_BROKER_ID: 2
          KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
          KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-2:9092,PLAINTEXT_HOST://localhost:39092
          KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
          KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
          KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 2
        volumes:
          - ./kafka2/data:/var/lib/kafka/data

If you've not created your topic some other way, Kafka will default to create your coverageEvolved topic used in the code with only one replica, and only one partition.如果您没有以其他方式创建主题,Kafka 将默认创建代码中使用的coverageEvolved主题,其中只有一个副本和一个分区。

If you kill the broker hosting that one replica, there will be no in sync replica leader that can be produced to.如果您终止托管该副本的代理,则将没有可以生成的同步副本领导者。

You can use Kafkajs to create topics .您可以使用 Kafkajs 创建主题

Also worth mentioning, there's a transactions topic that only has one replica (you're missing an environment variable for it).另外值得一提的是,有一个只有一个副本的事务主题(您缺少它的环境变量)。 This is mainly only relevant for Java clients since transactional producers are enabled by default as of Kafka 3.0这主要仅与 Java 客户端相关,因为从 Kafka 3.0 开始默认启用事务生产者

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM