Kafka入门到实战-第五弹

Kafka入门到实战

Kafka常见操作

官网地址

声明: 由于操作系统, 版本更新等原因, 文章所列内容不一定100%复现, 还要以官方信息为准

bash 复制代码
https://kafka.apache.org/

Kafka概述

Apache Kafka 是一个开源的分布式事件流平台,提供高性能数据管道、流分析、 数据集成和任务关键型应用程序。

Kafka的基础操作

本节将在Kafka集群上执行。所有工具都可以在Kafka发行版的bin/目录下使用,如果在没有参数的情况下运行,每个工具都会打印所有可能的命令行选项的详细信息。

  • 添加和删除主题 创建一个名为 my_topic_name的主题, 分区为10, 副本数为2(上次搭建的集群只有两个broker), 保留时间是1天

    bash 复制代码
    bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic my_topic_name \
            --partitions 10 --replication-factor 2 --config retention.ms=86400000
  • 查看分区是否正常创建 ls /tmp/kraft-combined-logs/

  • 删除主题, 可以看到分区被逻辑删除啦,

    bash 复制代码
    bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic my_topic_name
  • 修改主题, 只需要把delete 改为alert即可

  • 优雅地关闭, 以下内容直接参考官网, 未在本机上测试

    • 关闭生产者
    • 关闭消费者
    • 添加controlled.shutdown.enable=true
    • bin/kafka-server-stop.sh
  • 设置具有优先的领导权, 用于一个服务实例宕机启动

    bash 复制代码
    auto.leader.rebalance.enable=true
  • 跨机架平衡复制副本, 实现Kafka故障转移和容灾备份

    bash 复制代码
     broker.rack=my-rack-id
  • 查看消费者的位置

    bash 复制代码
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group my-group
  • 管理消费者组

    bash 复制代码
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
  • 列出消费者组详细信息

    bash 复制代码
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group my-group
    • 还可以加一些参数
    • --members 提供使用者组中所有活动成员的列表
    • --members --verbose 还提供分配给每个成员的分区
    • --offsets 默认的description选项,提供与"--description"选项相同的输出
    • --state 提供有用的组级别信息
    • --delete 删除一个或多个消费者组
  • 扩展集群

  • 下线服务实例

  • 增加复制因子

  • 限制数据迁移期间的带宽使用

  • 配置额度

    • 自定义额度对于 (user=user1, client-id=clientA)

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity-type users --entity-name user1 --entity-type clients --entity-name clientA
    • 自定义额度对于 user=user1

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity-type users --entity-name user1
    • 自定义额度对于 client-id=clientA

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity-type clients --entity-name clientA
    • 为user=userA配置默认客户端id配额

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity-type users --entity-name user1 --entity-type clients --entity-default
    • 配置默认配额对于user

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity-type users --entity-default
    • 配置默认配额对于client-id

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity-type clients --entity-default
    • 查询分配的配额 (user, client-id)

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --describe --entity-type users --entity-name user1 --entity-type clients --entity-name clientA
    • 查询分配的配额 (user)

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --describe --entity-type users --entity-name user1
    • 查询分配的配额 (client-id)

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --describe --entity-type clients --entity-name clientA
    • 查询分配的配额 (所有user)

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --describe --entity-type users
    • 查询分配的配额 (所有user, client-id)

      bash 复制代码
      bin/kafka-configs.sh  --bootstrap-server localhost:9092 --describe --entity-type users --entity-type clients

更新计划

下一部分是Kafka实战的代码部分, 敬请期待...

相关推荐
喂完待续4 小时前
Apache Hudi:数据湖的实时革命
大数据·数据仓库·分布式·架构·apache·数据库架构
yh云想13 小时前
《从入门到精通:Kafka核心原理全解析》
分布式·kafka
武子康14 小时前
大数据-70 Kafka 日志清理:删除、压缩及混合模式最佳实践
大数据·后端·kafka
ModelWhale17 小时前
“大模型”技术专栏 | 浅谈基于 Kubernetes 的 LLM 分布式推理框架架构:概览
分布式·kubernetes·大模型
愿天堂没有C++18 小时前
C++——分布式
分布式
UPToZ18 小时前
【Docker】搭建一个高性能的分布式对象存储服务 - MinIO
分布式·docker·容器
前端世界1 天前
鸿蒙任务调度机制深度解析:优先级、时间片、多核与分布式的流畅秘密
分布式·华为·harmonyos
A尘埃1 天前
金融项目高可用分布式TCC-Transaction(开源框架)
分布式·金融·开源
夜影风1 天前
RabbitMQ核心架构与应用
分布式·架构·rabbitmq
花酒锄作田1 天前
Nginx反向代理Kafka集群
nginx·kafka