背景:因為公司收集終端盒子數據的kafka服務偶爾會倒,所以考慮使用kafka的分布式,增加broker節點,來提高系統的可用性。當然,zookeeper服務節點也是可以增加的,但不在本文范圍內。
具體步驟如下:
1. 新加kafka服務,並啟動
如果是同一服務器,則可以拷貝新建server.properties的方式啟動,具體參考官網
如果是不同服務器,則需要下載解壓同一版本的kafka,並修改zookeeper的設置,然后啟動
這里我用的是第二種方案,下面是server.properties中修改的三個配置
broker.id=1 listeners=PLAINTEXT://192.168.0.43:9093 zookeeper.connect=192.168.0.20:2181
啟動命令
nohup /opt/kafka_2.12-2.4.0/bin/kafka-server-start.sh /opt/kafka_2.12-2.4.0/config/server.properties &
相應的停止命令
/opt/kafka_2.12-2.4.0/bin/kafka-server-stop.sh /opt/kafka_2.12-2.4.0/config/server.properties &
2. 重新給Topic分配副本
1)新建json文件increase-replication-factor.json,並輸入如下內容
{"version":1,
"partitions":[{"topic":"activeRecords","partition":0,"replicas":[0,1]}]
}
2)執行分配命令
./bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --execute

3. 查看執行結果
./bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --verify
可以看到,因為log文件過多,導致復制操作還在執行
等待一段時間之后,再次查看執行結果

最后查看該Topic狀態(RelicationFactor和Replicas的變化),增加成功!
./bin/kafka-topics.sh --zookeeper localhost:2181 --topic activeRecords --describe

如果嘗試把節點0的kafka停掉,會發現Leader發生了變化
另外,中間發生了如下的錯誤,原因是兩邊的kafka版本不同,一個是2.10,一個是2.12,在將低版本升級后,問題解決。
Connection to 0 was disconnected before the response was read
參考:
kafka官網:http://kafka.apache.org/quickstart
kafka中文學習網站:https://www.w3cschool.cn/apache_kafka/
https://blog.csdn.net/lkforce/article/details/77864472
https://blog.csdn.net/lzufeng/article/details/81743521
