目前kafka集群有3个节点,server.properties 配置
offsets.topic.replication.factor=1 transaction.state.log.replication.factor=1 transaction.state.log.min.isr=1
目前设置为1个副本,如果一台服务器挂了,那么就会造成数据丢失。
因此需要将副本改为3.
查看topic描述
./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic testmy
使用API查看topic描述, 参考: Kafka客户端操作 topic描述
name:testmy, desc:(name=testmy, internal=false, partitions= (partition=0, leader=192.168.6.34:9092 (id: 2 rack: null), replicas=192.168.6.34:9092 (id: 2 rack: null), isr=192.168.6.34:9092 (id: 2 rack: null)), (partition=1, leader=192.168.6.35:9092 (id: 3 rack: null), replicas=192.168.6.35:9092 (id: 3 rack: null), isr=192.168.6.35:9092 (id: 3 rack: null)), (partition=2, leader=192.168.6.33:9092 (id: 1 rack: null), replicas=192.168.6.33:9092 (id: 1 rack: null), isr=192.168.6.33:9092 (id: 1 rack: null)))
配置为3分片1副本。
创建test.json
{ "version": 1, "partitions": [ { "topic": "testmy", "partition": 0, "replicas": [ 1, 2, 3 ] }, { "topic": "testmy", "partition": 1, "replicas": [ 1, 2, 3 ] }, { "topic": "testmy", "partition": 2, "replicas": [ 1, 2, 3 ] } ] }
执行脚本
./kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file ../config/test.json --execute
返回结果如下图所示,
参数解释:
--reassignment-json-file 带有分区的JSON文件
--execute 按规定启动重新分配通过
再次查看topic描述
./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic testmy
可以发现,副本已经改为3了。
使用API查看topic描述, Kafka客户端操作 topic描述
name:testmy, desc:(name=testmy, internal=false, partitions= (partition=0, leader=192.168.6.34:9092 (id: 2 rack: null), replicas=192.168.6.33:9092 (id: 1 rack: null), 192.168.6.34:9092 (id: 2 rack: null), 192.168.6.35:9092 (id: 3 rack: null), isr=192.168.6.34:9092 (id: 2 rack: null), 192.168.6.33:9092 (id: 1 rack: null), 192.168.6.35:9092 (id: 3 rack: null)), (partition=1, leader=192.168.6.35:9092 (id: 3 rack: null), replicas=192.168.6.33:9092 (id: 1 rack: null), 192.168.6.34:9092 (id: 2 rack: null), 192.168.6.35:9092 (id: 3 rack: null), isr=192.168.6.35:9092 (id: 3 rack: null), 192.168.6.33:9092 (id: 1 rack: null), 192.168.6.34:9092 (id: 2 rack: null)), (partition=2, leader=192.168.6.33:9092 (id: 1 rack: null), replicas=192.168.6.33:9092 (id: 1 rack: null), 192.168.6.34:9092 (id: 2 rack: null), 192.168.6.35:9092 (id: 3 rack: null), isr=192.168.6.33:9092 (id: 1 rack: null), 192.168.6.34:9092 (id: 2 rack: null), 192.168.6.35:9092 (id: 3 rack: null)))
默认配置
offsets.topic.replication.factor=3 transaction.state.log.replication.factor=3 transaction.state.log.min.isr=3 num.partitions=1 default.replication.factor=3
offsets.topic.replication.factor 用于配置offset记录的topic的partition的副本个数。
transaction.state.log.replication.factor 事务主题的复制因子
transaction.state.log.min.isr 覆盖事务主题的min.insync.replicas配置
num.partitions 新建Topic时默认的分区数
default.replication.factor 自动创建topic时的默认副本的个数。 该参数是真正决定topic的副本数量的。
创建topic topicmy2, 6个分区一个副本
增加一个副本