简体   繁体   中英

How can I change the leader between consumers?

If I have a topic and multiple consumers with the same groupId, only one consumer should receive the messages (the leader). I want to chose which consumer would be the leader and receives messages in java but I don't know how to do it.

This is the code for one of my consumer. Is there some properties to chose the leader ?

@KafkaListener(topics = {"topic_kafka","topic_kafkaaa"}, groupId = "group_id")
    public void consume(String message) throws IOException {
        log.info("Consumed message in {} : {}", TOPIC, message);
    }

I think Kafka consumer groups might work differently than you expect. There isn't a leader for a Kafka consumer group. A consumer group is subscribed to topics, These topics have a total number of partitions, X. Kafka then treats all of those consumers in the group, as if they were one logical entity (assuming if a message is delivered to any of them then its delivered to the entity) and divides the X partitions across the available number of consumers in the group. If you have more consumers than the number of partitions, the extra sit idle. Its a dynamic distribution system.

You are probably thinking about leader because you are considering the broker model, where a topic's replicas are spread across multiple brokers, and one of those brokers is the leader for the topic. This isn't how consumers work.

IF you want to ensure a particular consumer is the only one to get messages, then you need to give it it's own consumer group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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