Kafka中Replicas复制备份机制
kafka将每个partition数据复制到多个server上,任何一个partition有一个leader和多个follower(可以没有),备份的个数可以通过broker配置文件来设定(replication-factor的参数配置指定).leader处理所有的read-write请求,follower需要和leader保持同步.Follower和consumer一样,消费消息并保存在本地日志中,leader负责跟踪所有的follower状态,如果follower"落后"太多或者失效,leader将会把它从replicas同步列表中删除.当所有的follower都将一条消息保存成功,此消息才被认为是"committed",那么此时consumer才能消费它.即使只有一个replicas实例存活,仍然可以保证消息的正常发送和接收,只要zookeeper集群存活即可.
Kafka中的选举
当leader失效时,需在followers中选取出新的leader,可能此时follower落后于leader,因此需要选择一个"up-to-date"的follower.选择follower时需要兼顾一个问题,就是新leader server上所已经承载的partition leader的个数,如果一个server上有过多的partition leader,意味着此server将承受着更多的IO压力.在选举新leader,需要考虑到"负载均衡".
常用命令
1、创建topics
./kafka-topics.sh --create --zookeeper chenx02:2181 --replication-factor 1 --partitions 1 --topic test
2、查看队列列表
./kafka-topics.sh --list --zookeeper chenx02:2181
3、查看队列明细
./kafka-topics.sh --describe --zookeeper chenx02:2181 --topic test
结果:
Topic:test    PartitionCount:1    ReplicationFactor:1    Configs:      
    Topic: test    Partition: 0    Leader: 2    Replicas: 2    Isr: 2
4、修改(test)队列参数
./kafka-topics.sh --zookeeper chenx02:2181 --partition 3 --topic test --alter
结果:
Topic:test    PartitionCount:3    ReplicationFactor:1    Configs:      
    Topic: test    Partition: 0    Leader: 2    Replicas: 2    Isr: 2       
    Topic: test    Partition: 1    Leader: 1    Replicas: 1    Isr: 1       
    Topic: test    Partition: 2    Leader: 2    Replicas: 2    Isr: 2
5、创建多副本的队列
./kafka-topics.sh --create --zookeeper chenx02:2181 --replication-factor 3 --partitions 4 --topic test_kafka
./kafka-topics.sh --describe --zookeeper chenx02:2181 --topic test_kafka      
Topic:test_kafka    PartitionCount:4    ReplicationFactor:3    Configs:       
    Topic: test_kafka    Partition: 0    Leader: 1    Replicas: 1,3,2    Isr: 1,3,2       
    Topic: test_kafka    Partition: 1    Leader: 2    Replicas: 2,1,3    Isr: 2,1,3       
    Topic: test_kafka    Partition: 2    Leader: 3    Replicas: 3,2,1    Isr: 3,2,1       
    Topic: test_kafka    Partition: 3    Leader: 1    Replicas: 1,2,3    Isr: 1,2,3
说明:
partiton: partion id      
leader:当前负责读写的lead broker id       
replicas:当前partition的所有replication broker  list       
isr:relicas的子集,只包含出于活动状态的broker
6、删除kafka的队列[注意需要重启kafka集群]      
kafka-run-class.sh kafka.admin.DeleteTopicCommand --topic test_kafka --zookeeper chenx02:2181
7、查看不可用的分区      
kafka-topics.sh --describe --unavailable-partitions --zookeeper chenx02:2181 --topic  test_kafka
8、发送消息      
./kafka-console-producer.sh --broker-list chenx02:9092 --topic test 
9、消费消息      
./kafka-console-consumer.sh --zookeeper chenx02:2181 --topic test --from-beginning
原文:http://www.cnblogs.com/jianyuan/p/4342945.html