Kafka - SASL認證

kafka SASL認證配置

一、找到kafka安裝根目錄,在config文件夾下建立kafka_server_jaas.conf,寫入java

KafkaServer {
    org.apache.kafka.common.security.plain.PlainLoginModule required
    username="admin"
    password="admin-secret"
    user_admin="admin-secret"
    user_alice="alice-secret";
};

二、在config文件夾下建立kafka_client_jaas.conf,寫入apache

KafkaClient {
    org.apache.kafka.common.security.plain.PlainLoginModule required
    username="alice"
    password="alice-secret";
};

三、打開config文件夾下server.properties,添加bootstrap

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

四、打開config文件夾下producer.properties、consumer.properties,分別添加windows

security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN

五、打開bin\windows目錄下的kafka-server-start.bat文件添加測試

set KAFKA_OPTS=-Djava.security.auth.login.config=file:%~dp0../../config/kafka_server_jaas.conf

六、 打開bin\windows目錄下的kafka-console-producer.bat和kafka-console-consumer.bat文件分別添加ui

set KAFKA_OPTS=-Djava.security.auth.login.config=file:%~dp0../../config/kafka_client_jaas.conf

七、經過以上步驟基本的配置已經完成, 如今開始逐個啓動spa

啓動zookeeper:.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.propertiesdebug

啓動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 testcode

建立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" }  開啓調試
相關文章
相關標籤/搜索