Kafka 、Kafka Tools针对于SASL修改

最近公司项目,需要支持Kafka SASL,经过2天的摸索,终于搞定,特此记录。
该文从以下5方面,进行介绍

  1. 环境搭建
  2. 代码修改
  3. 监控工具修改
  4. 命令脚本参数对比
  5. 常见报错

一、环境搭建

1、Kafka Server修改

在config目录下创建kafka_server_jaas.conf文件,配置如下

KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=“admin”
password=“admin”
user_admin=“admin”
user_fm=“fm_security”;
};

其中,Username和password属性,用于Kafka Broker间,进行连接所使用的账户和密码。
user_{userName}=”{password}”,用于定义client连接到broker所需账号和密码。在上述配置中,定义了两个账户,admin和fm。

Server.properties修改

#Listeners
listeners=SASL_PLAINTEXT://10.45.50.65:9098
#使用的认证协议
security.inter.broker.protocol=SASL_PLAINTEXT
#SASL机制
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
#身份验证处理类
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
super.users=User:admin

2、Kafka Client修改

在config目录下创建kafka_client_jaas.conf配置文件,配置如下

KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=“fm”
password=“fm_security”;
};

userName和password属性,需要与kafka_server_jaas.conf配置文件中,配置的账户和密码对应,否则,会报认证失败的异常。

3、生产者脚本修改

在Consumer.properties配置文件中,增加如下两个配置项

security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required
username=“admin”
password=“admin”;

Kafka-console-consumer.sh修改
在最开始处,增加如下配置。其中config对应于Kafka Client中创建的配置文件路径。

export KAFKA_OPTS=" -Djava.security.auth.login.config=/home/install/signal_other/kafka_2.11-1.0.1/config/kafka_client_jaas.conf"

4、消费者脚本修改

同生产者。

5、Kafka-consumer-group脚本修改

在config目录下创建consumer_group.conf配置文件,增加如下配置

security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required
username=“fm”
password=“fm_security”;

其中userName和password属性的含义,同生产者和消费者脚本中userName和password属性。

二、代码修改

1、生产者修改

在生产者配置属性中,增加如下三项:
producerConfig.put(“security.protocol”, “SASL_PLAINTEXT”);
producerConfig.put(“sasl.mechanism”, “PLAIN”);
producerConfig.put(“sasl.jaas.config”, “org.apache.kafka.common.security.plain.PlainLoginModule required username=“fm” password=“fm_security”;”);
其中针对于username和password中出现的明文,可通过配置文件中配置密文,进行解密后,在进行字符串填充。

2、消费者修改

同生产者。

三、监控工具Kafka Tool修改

1、在Kafka Tool安装目录下,创建一个快捷方式。

2、在启动命令的时候,增加一个启动参数,完整参数如下

“E:\Program Files\kafkatool2\kafkatool.exe” -J-Djava.security.auth.login.config=“E:/Program Files/kafkatool2/kafka_client_jaas.conf”

在这里插入图片描述

3、创建kafka_client_jaas.conf配置文件,配置内容如下

KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=“fm”
password=“fm_security”;
};

4、添加kafka连接

(1)将type切换成SASL plaintext选项

在这里插入图片描述

(2)填入ZK(或broker地址)
(3)将SASL Mechanism输入框填入PLAIN

在这里插入图片描述

四、脚本对比(普通和SASL)

1、生产者

普通

./kafka-console-producer.sh --broker-list 10.45.50.65:9098 --topic QLJ

SASL

./kafka-console-producer.sh --broker-list 10.45.50.65:9098 --topic QLJ --producer.config …/config/producer.properties

2、消费者

普通

./kafka-console-consumer.sh --bootstrap-server 10.45.50.65:9098 --topic QLJ

SASL

./kafka-console-consumer.sh --bootstrap-server 10.45.50.65:9098 --topic QLJ --consumer.config …/config/consumer.properties

3、Kafka-consumer-group.sh

普通

./kafka-consumer-groups.sh --bootstrap-server 10.45.50.65:9098

SASL

./kafka-consumer-groups.sh --bootstrap-server 10.45.50.65:9098 --command-config …/config/consumer_group.properties --list

五、常见报错

1、Executing consumer group command failed due to Request METADATA failed on brokers List(10.45.50.65:9098 (id: -1 rack: null))
在这里插入图片描述
解决方式:执行命令时,提供–consumer.config …/config/consumer.properties参数。
2、 Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
在这里插入图片描述
解决方式:执行命令时,使用consumer_group.properties配置文件。因为consumer.properties配置文件中提供group.id属性,导致查询消费者详情的时候,使用的是配置文件提供的消费者群组,而不是参数传入的消费者群组名称。

3、org.apache.kafka.common.errors.SaslAuthenticationException: Authentication failed: Invalid username or password
在这里插入图片描述
解决方式:检查consumer.properties中配置的username和password和kafka_client_jaas.properties配置文件,提供的用户名和密码是否一致。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我要做个有钱人2020

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

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

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

打赏作者

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

抵扣说明:

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

余额充值