如何在Kafka中修改Topic的preferred replica

操作背景

假如topic test.example中partition 0的replicas为[0,4],则0为preferred replica,应该成为leader。
这时我们期望4为preferred replica,并变成leader。

执行步骤如下:

1.查看当前的topic详细信息:
lizhitao@users-MacBook-Pro-2:~$ ./bin/kafka-topics.sh –zookeeper 192.168.2.225:2183/config/mobile/mq –describe –topic test.example
Topic:test.example PartitionCount:12 ReplicationFactor:2 Configs:
Topic: test.example Partition: 0 Leader: 0 Replicas: 0,4 Isr: 0,4
Topic: test.example Partition: 1 Leader: 1 Replicas: 1,5 Isr: 1,5
Topic: test.example Partition: 2 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: test.example Partition: 3 Leader: 0 Replicas: 0,4 Isr: 0,4
Topic: test.example Partition: 4 Leader: 1 Replicas: 1,5 Isr: 1,5
Topic: test.example Partition: 5 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: test.example Partition: 6 Leader: 0 Replicas: 0,4 Isr: 0,4
Topic: test.example Partition: 7 Leader: 1 Replicas: 1,5 Isr: 1,5
Topic: test.example Partition: 8 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: test.example Partition: 9 Leader: 0 Replicas: 0,4 Isr: 0,4
Topic: test.example Partition: 10 Leader: 1 Replicas: 1,5 Isr: 1,5
Topic: test.example Partition: 11 Leader: 2 Replicas: 2,3 Isr: 2,3

2.修改replicas的顺序
lizhitao@users-MacBook-Pro-2:~$ cat partitions-to-move.json
{
“partitions”: [
{
“topic”: “test.example”,
“partition”: 0,
“replicas”: [
4,
0
]
}
],
“version”: 1
}

lizhitao@users-MacBook-Pro-2:~$ ./bin/kafka-reassign-partitions.sh –zookeeper 192.168.2.225:2183/config/mobile/mq –reassignment-json-file partitions-to-move.json –execute

3.更改leader
lizhitao@users-MacBook-Pro-2:~$ cat topicPartitionList.json
{
“partitions”:
[
{“topic”: “test.example”, “partition”: 0}
]
}

lizhitao@users-MacBook-Pro-2:~$ ./bin/kafka-preferred-replica-election.sh –zookeeper 192.168.2.225:2183/config/mobile/mq –path-to-json-file topicPartitionList.json

4.检查replicas leader切换情况
lizhitao@users-MacBook-Pro-2:~$ ./bin/kafka-topics.sh –zookeeper 192.168.2.225:2183/config/mobile/mq –describe –topic test.example
Topic:test.example PartitionCount:12 ReplicationFactor:2 Configs:
Topic: test.example Partition: 0 Leader: 4 Replicas: 4,0 Isr: 0,4
Topic: test.example Partition: 1 Leader: 1 Replicas: 1,5 Isr: 1,5
Topic: test.example Partition: 2 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: test.example Partition: 3 Leader: 0 Replicas: 0,4 Isr: 0,4
Topic: test.example Partition: 4 Leader: 1 Replicas: 1,5 Isr: 1,5
Topic: test.example Partition: 5 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: test.example Partition: 6 Leader: 0 Replicas: 0,4 Isr: 0,4
Topic: test.example Partition: 7 Leader: 1 Replicas: 1,5 Isr: 1,5
Topic: test.example Partition: 8 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: test.example Partition: 9 Leader: 0 Replicas: 0,4 Isr: 0,4
Topic: test.example Partition: 10 Leader: 1 Replicas: 1,5 Isr: 1,5
Topic: test.example Partition: 11 Leader: 2 Replicas: 2,3 Isr: 2,3

引用自尚安wiki

可以使用多线程的方式,在每个线程消费不同的topic。具体做法如下: 1. 创建多个消费者对象,每个消费者对象消费不同的topic。 2. 将每个消费者对象放入一个线程,并启动线程。 3. 在每个线程,使用消费者对象消费对应的topic。 示例代码: ```java public class KafkaConsumerDemo { private static final String TOPIC_1 = "topic1"; private static final String TOPIC_2 = "topic2"; public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); executor.submit(new ConsumerThread(TOPIC_1)); executor.submit(new ConsumerThread(TOPIC_2)); } private static class ConsumerThread implements Runnable { private final String topic; public ConsumerThread(String topic) { this.topic = topic; } @Override public void run() { Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "test-group"); props.put("enable.auto.commit", "true"); props.put("auto.commit.interval.ms", "1000"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList(topic)); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.printf("topic = %s, partition = %d, offset = %d, key = %s, value = %s\n", record.topic(), record.partition(), record.offset(), record.key(), record.value()); } } } } } ``` 上面的代码,创建了两个消费者线程,一个消费topic1,一个消费topic2。在每个线程,创建了一个KafkaConsumer对象,用于消费对应的topic。在循环,使用poll方法获取消息,并处理消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值