kafka SASL認證配置
1、找到kafka安裝根目錄,在config文件夾下創建kafka_server_jaas.conf,寫入
KafkaServer { org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret" user_admin="admin-secret" user_alice="alice-secret"; };
2、在config文件夾下創建kafka_client_jaas.conf,寫入
KafkaClient { org.apache.kafka.common.security.plain.PlainLoginModule required username="alice" password="alice-secret"; };
3、打開config文件夾下server.properties,添加
listeners=SASL_PLAINTEXT://localhost:9092 # 使用的認證協議 security.inter.broker.protocol=SASL_PLAINTEXT # SASL機制 sasl.enabled.mechanisms=PLAIN sasl.mechanism.inter.broker.protocol=PLAIN # 完成身份驗證的類 authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer # 如果沒有找到ACL(訪問控制列表)配置,則允許任何操作。 allow.everyone.if.no.acl.found=true
4、打開config文件夾下producer.properties、consumer.properties,分別添加
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
5、打開bin\windows目錄下的kafka-server-start.bat文件添加
set KAFKA_OPTS=-Djava.security.auth.login.config=file:%~dp0../../config/kafka_server_jaas.conf
6、 打開bin\windows目錄下的kafka-console-producer.bat和kafka-console-consumer.bat文件分別添加
set KAFKA_OPTS=-Djava.security.auth.login.config=file:%~dp0../../config/kafka_client_jaas.conf
7、通過以上步驟基本的配置已經完成, 現在開始逐個啟動
啟動zookeeper:.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties
啟動kafka:.\bin\windows\kafka-server-start.bat .\config\server.properties
創建topic:.\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
創建producer:.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test --producer.config .\config\producer.properties
創建consumer:.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning --consumer.config .\config\consumer.properties
在producer窗口下輸入信息進行測試,每輸入一行回車后消息馬上就會出現在consumer中,表明kafka SASL已經安裝測試成功
Confluent.Kafka使用
在producer和consumer config中添加
{ "security.protocol","SASL_PLAINTEXT" }, { "sasl.mechanism","PLAIN" }, { "sasl.username", "alice"}, { "sasl.password", "alice-secret" }, //{ "debug", "security,broker,protocol" } 開啟調試