kafka入门

3 篇文章 0 订阅
安装与配置
  1. 安装
    1. java环境
    2. zookeeper
    3. 下载kafka下载地址
    4. 解压缩tar -zxvf kafka_2.13-2.6.0.tgz
    5. 启动./bin/kafka-server-start.sh -daemon config/server.properties后台启动指定配置文件
  2. 名词解释
    1. broker 处理节点 多个broker组成集群 生产环境物理上可以理解为一台机器
    2. topic 主题
    3. producer 消费生产者
    4. consumer 消费者
    5. consumerGroup 消费者组
    6. partition 一个topic可以分为多个partition
      一个broker下可以有多个topic , 一个topic也可以在多个broker 一个topic分为多个partition
  3. 配置文件
    broker.id
    listeners 端口
    zookeeper.connect=localhost:2181 zk地址
    log.dir 日志输出地址
    num.partitions topic创建时分区个数
命令行使用
  1. 创建主题主题名称,备份因子1分区数量1./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
  2. 删除主题./kafka-topics.sh --delete --zookeeper localhost:2181 --topic test
  3. 发送消息./kafka-console-producer.sh --broker-list localhost:9092 --topic test
  4. 消费消息./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test默认消费最新的,如需从头消费添加命令--from-beginning
  5. 消费形式
    1. 单播:消费时指定组--consumer-property group.id=testGroup不指定每个是不同的组,同一个组下只有一个客户端能消费到
    2. 多播: 不同的组每个组都能消费到
      1. 查看所有的组./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
      2. 查看组消费情况./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group testGroup
      3. 重置offsetbin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group you_consumer_group_name --topic you_topic_name --execute --reset-offsets --to-offset 80
  6. 分区:
    1. 每个topic可以有多个分区,每个分区都有一个commit log,一个分区中的message的offset是唯一的,不同分区的message的offset可能相同,每个消费者基于自己在commit log中的offset进行工作,消费的offset由消费者自己维护
    2. 查看topic的情况 ./kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
      1. PartitionCount:分区数量
      2. ReplicationFactor:备份数量
      3. Partition:分区
      4. Leader:领导者id
      5. Replicas:当前分区在那个broker上有备份,包括挂掉的
      6. isr: 当前分区在那个broker上有备份,存活且同步备份了的节点
    3. 增加分区./kafka-topics.sh -alter --partitions 3 --zookeeper localhost:2181 --topic test
    4. 为什么要增加分区:
      1. 部分系统会对commit log文件做大小限制
      2. 分布式存储
      3. 提高并行度
  7. 集群
    1. 修改配置文件,主要是broker.id,listeners,log.dirs
    2. 启动,./kafka-server-start.sh -daemon ../config/server-2.properties
    3. 查看是否注册成功可以去zookeeper下查看/brokers/ids
    4. 创建主题:./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 5 --topic test2备份因子为3 5个分区
    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
      
    6. 集群收发消息/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
    7. 杀死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
      
    8. 同一组的消费者客户端的数量不能比一个topic的分区数量多,否则多的消费者消费不到任何消息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jamin_Ye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值