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实战的代码部分, 敬请期待...

相关推荐
iPadiPhone2 小时前
分布式架构的“润滑剂”:RabbitMQ 核心原理与大厂面试避坑指南
分布式·后端·面试·架构·rabbitmq
wanhengidc6 小时前
云手机与模拟器的关系
大数据·运维·服务器·分布式·智能手机
iPadiPhone8 小时前
万亿级流量的基石:Kafka 核心原理、大厂面试题解析与实战
分布式·后端·面试·kafka
Fang fan8 小时前
Netty入门
java·开发语言·redis·分布式·python·哈希算法
低调的JVM10 小时前
Golang下kafka可观测数据采集组件Otelsarama详解
golang·kafka·可观测·opentelemetry
黑棠会长1 天前
ABP框架09.数据安全与合规:审计日志与实体变更追踪
分布式·安全·架构·c#·abp
珠海西格1 天前
四可装置如何监测组件衰减与逆变器效率?
大数据·运维·服务器·分布式·能源
仗剑_走天涯1 天前
Hadoop 安装
大数据·hadoop·分布式
czlczl200209251 天前
Zookeeper原理
分布式·zookeeper·云原生
weixin199701080161 天前
《深入浅出:图解淘宝分布式数据库TDDL(及开源替代方案)》
数据库·分布式·开源