kafka入门
安装与配置
- 安装
- java环境
- zookeeper
- 下载kafka下载地址
- 解压缩tar -zxvf kafka_2.13-2.6.0.tgz
- 启动./bin/kafka-server-start.sh -daemon config/server.properties后台启动指定配置文件
- 名词解释
- broker 处理节点 多个broker组成集群 生产环境物理上可以理解为一台机器
- topic 主题
- producer 消费生产者
- consumer 消费者
- consumerGroup 消费者组
- partition 一个topic可以分为多个partition
一个broker下可以有多个topic , 一个topic也可以在多个broker 一个topic分为多个partition
- 配置文件
broker.id
listeners 端口
zookeeper.connect=localhost:2181 zk地址
log.dir 日志输出地址
num.partitions topic创建时分区个数
命令行使用
- 创建主题主题名称,备份因子1分区数量1
./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
- 删除主题
./kafka-topics.sh --delete --zookeeper localhost:2181 --topic test
- 发送消息
./kafka-console-producer.sh --broker-list localhost:9092 --topic test
- 消费消息
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test
默认消费最新的,如需从头消费添加命令--from-beginning
- 消费形式
- 单播:消费时指定组
--consumer-property group.id=testGroup
不指定每个是不同的组,同一个组下只有一个客户端能消费到 - 多播: 不同的组每个组都能消费到
- 查看所有的组
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
- 查看组消费情况
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group testGroup
- 重置offset
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group you_consumer_group_name --topic you_topic_name --execute --reset-offsets --to-offset 80
- 查看所有的组
- 单播:消费时指定组
- 分区:
- 每个topic可以有多个分区,每个分区都有一个commit log,一个分区中的message的offset是唯一的,不同分区的message的offset可能相同,每个消费者基于自己在commit log中的offset进行工作,消费的offset由消费者自己维护
- 查看topic的情况
./kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
- PartitionCount:分区数量
- ReplicationFactor:备份数量
- Partition:分区
- Leader:领导者id
- Replicas:当前分区在那个broker上有备份,包括挂掉的
- isr: 当前分区在那个broker上有备份,存活且同步备份了的节点
- 增加分区
./kafka-topics.sh -alter --partitions 3 --zookeeper localhost:2181 --topic test
- 为什么要增加分区:
- 部分系统会对commit log文件做大小限制
- 分布式存储
- 提高并行度
- 集群
- 修改配置文件,主要是
broker.id
,listeners
,log.dirs
- 启动,
./kafka-server-start.sh -daemon ../config/server-2.properties
- 查看是否注册成功可以去zookeeper下查看
/brokers/ids
- 创建主题:
./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 5 --topic test2
备份因子为3 5个分区 - 查看主题信息
./kafka-topics.sh --describe --zookeeper localhost:2181 --topic test2
0,3分区的leader是broker0,3台主机都有备份,leader负责对当前主机的读写,followers只是被动复制,不提供读写Topic: test2 PartitionCount: 5 ReplicationFactor: 3 Configs: Topic: test2 Partition: 0 Leader: 0 Replicas: 0,1,2 Isr: 0,1,2 Topic: test2 Partition: 1 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0 Topic: test2 Partition: 2 Leader: 2 Replicas: 2,0,1 Isr: 2,0,1 Topic: test2 Partition: 3 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1 Topic: test2 Partition: 4 Leader: 1 Replicas: 1,0,2 Isr: 1,0,2
- 集群收发消息
/kafka-console-producer.sh --broker-list localhost:9092,localhost:9093,localhost:9094 --topic test2``./kafka-console-consumer.sh --bootstrap-server localhost:9092,localhost:9093,localhost:9094 --topic test2 --from-beginning
- 杀死broker.id=0的进程重新查看topic信息,原本的分区0和分区3现在的leader已经转为1和2
Topic: test2 PartitionCount: 5 ReplicationFactor: 3 Configs: Topic: test2 Partition: 0 Leader: 1 Replicas: 0,1,2 Isr: 1,2 Topic: test2 Partition: 1 Leader: 1 Replicas: 1,2,0 Isr: 1,2 Topic: test2 Partition: 2 Leader: 2 Replicas: 2,0,1 Isr: 2,1 Topic: test2 Partition: 3 Leader: 2 Replicas: 0,2,1 Isr: 2,1 Topic: test2 Partition: 4 Leader: 1 Replicas: 1,0,2 Isr: 1,2
- 同一组的消费者客户端的数量不能比一个topic的分区数量多,否则多的消费者消费不到任何消息
- 修改配置文件,主要是
作者: JaminYe
版权声明:本文原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。