限流方式

方式 优点 缺点
client id 简单便捷 client id,一次只能有一个生产者实例,只能单并发
user 可以多 producer 同时进行,可与client id 进行组合,可以设置用户密码,增加一定的安全性,但用户名密码位置容易暴露 需要对kafka 开启安全认证,部署复杂行增加

基于 client id 限流

使用方法

# 对 test_lz4_10m_client 进行限流 生产消费速率为 10 M/S
./bin/kafka-configs.sh  --bootstrap-server xxxx:9092 --alter --add-config 'producer_byte_rate=10240,consumer_byte_rate=10240,request_percentage=200' --entity-type clients --entity-name test_lz4_10m_client

基于 user 限流

kafka 认证方式

  • SASL/GSSAPI (Kerberos) - starting at version 0.9.0.0
  • SASL/PLAIN - starting at version 0.10.0.0 (每次生效需要重启broker)
  • SASL/SCRAM-SHA-256 and SASL/SCRAM-SHA-512 - starting at version 0.10.2.0 (可动态增加用户)
  • SASL/OAUTHBEARER - starting at version 2.0

其中 SASL/GSSAPI (Kerberos) 这种可能是生产环境使用最合适的,但是笔者这边暂时还没有使用 Kerberos ,所以这里主要使用 SASL/PLAIN 和 SASL/SCRAM-SHA-256 这两种做一个探索。

使用 SASL_PLAINTEXT/PLAIN 进行用户认证实现限流

  1. broker 配置
#配置 ACL 入口类
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
#本例使用 SASL PLAINTEXT
listeners=SASL_PLAINTEXT://xxxxxx:9092
advertised.listeners=SASL_PLAINTEXT://xxxxxx:9092
security.inter.broker.protocol= SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN
#设置本例中 admin 为超级用户
super.users=User:admin
  1. 创建 kafka_server_jaas.conf
# 这里是创建了两个用户admin和test user_admin 表示用户 admin,后面的 admin-secret 表示是 admin 的密码
KafkaServer {org.apache.kafka.common.security.plain.PlainLoginModule requiredusername="admin"password="admin-secret"user_admin="admin-secret"user_test="123456";
};
  1. 启动脚本添加指定 kafka_server_jaas.conf

vim kafka-server-start.sh

# 添加 -Djava.security.auth.login.config=/data/kafka_2.13-2.7.1/config/kafka_server_jaas.conf
exec $base_dir/kafka-run-class.sh $EXTRA_ARGS -Djava.security.auth.login.config=/data/kafka_2.13-2.7.1/config/kafka_server_jaas.conf  kafka.Kafka "$@"
  1. 添加生产者消费者配置文件
# 生产者 producer_jaas.conf  消费者 consumer_jaas.conf 权限文件
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username = "test"
password="123456";
};# 修改 kafka-console-producer.sh
## -Djava.security.auth.login.config=/data/kafka_2.13-2.7.1/config/producer_jaas.conf
exec $(dirname $0)/kafka-run-class.sh -Djava.security.auth.login.config=/data/kafka_2.13-2.7.1/config/producer_jaas.conf kafka.tools.ConsoleProducer "$@"# 修改 kafka-console-consumer.sh
## -Djava.security.auth.login.config=/data/kafka_2.13-2.7.1/config/consumer_jaas.conf
exec $(dirname $0)/kafka-run-class.sh -Djava.security.auth.login.config=/data/kafka_2.13-2.7.1/config/consumer_jaas.conf  kafka.tools.ConsoleConsumer "$@"
  1. 使用
# 1. 进行授权对 用户
# 给用户 test 对 topic test_se 读的权限
./bin/kafka-acls.sh --authorizer kafka.security.auth.SimpleAclAuthorizer --authorizer-properties zookeeper.connect=xxxxxx:2181/kafka27 --add --allow-principal User:test --operation Read --topic test_se# 给用户 test 的 group test-group 赋予读的权限
./bin/kafka-acls.sh --authorizer kafka.security.auth.SimpleAclAuthorizer --authorizer-properties zookeeper.connect=xxxxxx:2181/kafka27 --add --allow-principal User:test --operation Read --group test-group# 给用户 test 赋予 test_se Write 的权限
./bin/kafka-acls.sh --authorizer kafka.security.auth.SimpleAclAuthorizer --authorizer-properties zookeeper.connect=xxxxxx:2181/kafka27 --add --allow-principal User:test --operation Write --topic test_se# 2. producer 使用
./bin/kafka-console-producer.sh --bootstrap-server xxxxxx:9092  --topic test_se  --producer-property security.protocol=SASL_PLAINTEXT  --producer-property sasl.mechanism=PLAIN# 3. consumer 使用
./bin/kafka-console-consumer.sh  --bootstrap-server xxxxxx:9092   --from-beginning --topic test_se --consumer.config ./config/console_consumer.conf

使用 SASL_PLAINTEXT/SCRAM 进行用户认证实现限流

  1. broker 配置
###################### SASL #########################
sasl.enabled.mechanisms=SCRAM-SHA-256
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-256
security.inter.broker.protocol=SASL_PLAINTEXT
listeners=SASL_PLAINTEXT://xxxxxx:9092
advertised.listeners=SASL_PLAINTEXT://xxxxxx:9092
####################### ACL ########################
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
super.users=User:admin
  1. 创建 kafka-broker-scram.jaas
KafkaServer {org.apache.kafka.common.security.scram.ScramLoginModule required
username="admin"
password="admin";
};
  1. 指定 kafka-broker-scram.jaas 位置

修改 vim kafka-server-start.sh

exec $base_dir/kafka-run-class.sh $EXTRA_ARGS -Djava.security.auth.login.config=/data/kafka_2.13-2.7.1/config/auth/kafka-broker-scram.jaas  kafka.Kafka "$@"
  1. 添加生产者消费者配置文件
# consumer-scram.conf 或 producer-scram.conf
security.protocol=SASL_PLAINTEXT
sasl.mechanism=SCRAM-SHA-256
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="test" password="123456";
  1. 授权
# 添加用户
./bin/kafka-configs.sh --zookeeper xxxxxx:2181/kafka27 --alter --add-config 'SCRAM-SHA-256=[password=123456]' --entity-type users --entity-name test# 读权限
./bin/kafka-acls.sh --authorizer-properties zookeeper.connect=xxxxxx:2181 --add --allow-principal User:"test" --consumer --topic 'test_topic' --group '*'# 写权限
./bin/kafka-acls.sh --authorizer-properties zookeeper.connect=xxxxxx:2181 --add --allow-principal User:"test" --producer --topic 'test_topic'# 生产者
./bin/kafka-console-producer.sh --broker-list xxxxxx:9092 --topic test_scram --producer.config config/auth/producer-scram.conf# 消费者
./bin/kafka-console-consumer.sh --bootstrap-server xxxxxx:9092 --topic test_scram --consumer.config config/auth/consumer-scram.conf

限流

# 对用户 test 限流 10M/S
./bin/kafka-configs.sh --zookeeper xxxxxx:2181/kafka27 --alter --add-config 'producer_byte_rate=10485760' --entity-type users --entity-name test# 对 client id 为 clientA 的限流 10M/S
./bin/kafka-configs.sh --zookeeper xxxxxx:2181/kafka27 --alter --add-config 'producer_byte_rate=10485760' --entity-type clients --entity-name clientA# /bin/kafka-configs.sh  --bootstrap-server xxxxxx:9092 --alter --add-config 'producer_byte_rate=10240,consumer_byte_rate=10240,request_percentage=200' --entity-type clients --entity-name test_lz4_10m_client

此处的限流应该是对单个 broker 限流为 10 M/S ,应为测试 topic 有 3 分区分别分布在三个 broker 所以总体限流大概在 30M/S 左右ß

附一张 kafka 压缩类型对比(无 CPU 对比)

Kafka 压缩、限流和 SASL_PLAIN 、 SASL_SCRAM-SHA-256简单认证相关推荐

  1. 【Flink】Flink 消费 kafka 实现 限流处理 RateLimiter

    文章目录 1.概述 2.案例 2.1 案例1 纪念一波,九师兄博客热门订阅专栏时常名列前茅,我飘了,哈哈哈哈,得意的笑 1.概述 首先看看 [java]高并发之限流 RateLimiter使用 这个去 ...

  2. 【kafka】kafka broker 限流 topic 限流 配额

    纪念一波,九师兄博客热门订阅专栏时常名列前茅,我飘了,哈哈哈哈,得意的笑 1. 概述 翻译:配额 4.9 配额 Kafka集群有能力对请求执行配额来控制客户端使用的代理资源.Kafka broker可 ...

  3. 阿里云二面:你对限流了解多少?

    今天来说说限流的相关内容,包括常见的限流算法.单机限流场景.分布式限流场景以及一些常见限流组件. 当然在介绍限流算法和具体场景之前我们先得明确什么是限流,为什么要限流?. 任何技术都要搞清它的来源,技 ...

  4. 你被限流了吗?| 图解+代码

    来源 | yes的练级攻略 图源 | CSDN付费下载自视觉中国 今天来说说限流的相关内容,包括常见的限流算法.单机限流场景.分布式限流场景以及一些常见限流组件. 当然在介绍限流算法和具体场景之前我们 ...

  5. 服务器三种常见的限流算法

    服务器三种常见的限流算法 1.计数器算法 2. 滑动窗口 3.令牌桶算法 4.漏桶算法 滑动窗口限流 漏桶限流 令牌桶限流 限流算法总结 单机限流和分布式限流 限流组件 在开发高并发系统时,有三把利器 ...

  6. 熔断降级与限流在开源SpringBoot/SpringCloud微服务框架的最佳实践

    目录导读 熔断降级与限流在开源SpringBoot/SpringCloud微服务框架的最佳实践 1. 开源代码整体架构设计 2. 微服务逻辑架构设计 3. 微服务熔断降级与限流规划 3.1 微服务熔断 ...

  7. 搞懂限流算法这一篇就够了

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 TL;DR(too long don't read) 限流算法:计 ...

  8. 搞懂限流算法这一篇就够了 No.154

    点击上方"方志朋",选择"设为星标" 做积极的人,而不是积极废人 TL;DR(too long don't read) 限流算法:计数器.滑动窗口.漏桶.令牌桶 ...

  9. 微服务架构 — 服务治理 — 服务限流、服务降级、服务熔断

    目录 文章目录 目录 服务限流 服务降级 服务熔断 服务限流 C ⇄ S 的异常问题:C 的请求太多,超出 S 的服务能力,导致 S 不可用.例如:DoS 攻击,企图耗尽被攻击对象的资源,让目标系统无 ...

最新文章

  1. 在 react 里使用 antd
  2. 陌陌股价过山车背后隐藏了什么?
  3. 2017年第八届蓝桥杯 - 省赛 - C/C++大学A组 - C. 魔方状态
  4. 520,一份给程序员的“硬核”脱单秘籍
  5. linux操作系统基本配置
  6. linux python复制安装,复制一个Python全部环境到另一个环境,python另一个,导出此环境下安装的包...
  7. 设计灵感|色彩叠加在海报设计中的妙用!
  8. geotools 读取shp属性过滤_Flink进阶之使用布隆过滤器实现UV统计
  9. C# 读取word2003 并且显示在界面上的方法
  10. “陆奇争夺战”:江湖传言,得陆奇者得AI天下。
  11. python解决sip与ptqt不兼容导致页面截图引擎无法运行问题
  12. 跳级全奖进哈佛,连马云都忌惮三分,赚18个亿后隐退美国,如今的他在干什么?...
  13. 省团团小程序被微信封禁
  14. 51单片机流水灯方法大全
  15. 联想l430主板图纸_L430开箱+拆机+换内存+换U+评测+拷机15小时,图多杀猫
  16. android获取手机号码的归属地以及运营商,本地查询
  17. 计算机常用的IP地址三类,常用的三类IP地址
  18. 通过UEFI禁用 BD PROCHOT
  19. 拼多多“出海”的三个考验?
  20. 30ea什么意思_ea阶段是什么?你未必全知道!

热门文章

  1. 护眼党必备良心app
  2. Column 'id' in where clause is ambiguous
  3. Windows批处理命令快速获取文件夹下特定类型的文件名(2022.5.15)
  4. HTML期末大作业`关于在线电影主题网站设计——腾龙电影(3页) HTML+CSS+JavaScript 学生DW网页设计作业成品
  5. 卡诺图最简化SOP/POS表达式
  6. CreateJS 入门小记
  7. 中国方案入选世界5G标准
  8. python语言程序设计基础考试题库_中国大学MOOC(慕课)_Python语言程序设计基础_测试题及答案...
  9. ITU-R 建议书下载网址
  10. ios快捷指令:一键登录/登出南京大学校园网