安装包准备
下载安装包到目标目录,用tar zxvf 命令解压
认证配置文件
进入kafka安装包根目录的config文件夹下,创建kafka_server_jaas.conf 和kafka_zoo_jaas.conf 两个文件
Server{
org.apache.zookeeper.server.auth.DigestLoginModule required
username="[YOUR NAME]"
password="[YOUR PWD]"
user_[YOUR NAME]="[YOUR PWD]";
};
本段防止有的人不替换[YOUR NAME] 和[YOUR PWD] ,啊?不会真的有人不换吧,换成自己想起的用户名和密码,例如. [YOUR NAME]=hanmeimei,[YOUR PWD]=123456
Server{
org.apache.kafka.common.security.scram.ScramLoginModule required
username="hanmeimei"
password="123456"
user_hanmeimei="123456";
};
Client{
org.apache.zookeeper.server.auth.DigestLoginModule required
username="[YOUR NAME]"
password="[YOUR PWD]";
};
KafkaServer{
org.apache.kafka.common.security.scram.ScramLoginModule required
username="[YOUR NAME]"
password="[YOUR PWD]"
user_[YOUR NAME]="[YOUR PWD]";
};
这里选用的是org.apache.kafka.common.security.scram.ScramLoginModule加密方式,对应的就是SHA-256,根据需要可以选择其它的加密方式
启动zookeeper
dataDir=[ZOOKEEPER DATA PATH]
clientPort=2185
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
requireClientAuthScheme=sasl
jaasLoginRenew=3600000
zookeeper.sasl.client=true
export KAFKA_OPTS=" -Djava.security.auth.login.config=[KAFKA安装路径]/config/kafka_zoo_jaas.conf"
exec $base_dir/kafka-run-class.sh $EXTRA_ARGS org.apache.zookeeper.server.quorum.QuorumPeerMain "$@"
./zookeeper-server-start.sh ../config/zookeeper.properties
nohup ./zookeeper-server-start.sh ../config/zookeeper.properties 1>/dev/null 2>&1 &
启动kafka集群
listeners=SASL_PLAINTEXT://0.0.0.0:9092
advertised.listeners=SASL_PLAINTEXT://[暴露的IP]:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.enabled.mechanisms=SCRAM-SHA-256
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-256
log.dirs=/data/kafka/logs
zookeeper.connect=127.0.0.1:2185
上面的advertised.listeners 配置很重要,例如我们在云服务器上搭建Kafka集群,如果不配置这个直接通过公网IP+端口是不能正常访问Kafka的,一定要配置这个属性才能正常访问。同理docker环境部署的Kafka也需要这个属性配置
- [ ]复制两个
config/server.properties 做集群配置(本教程是单机模拟)
cp config/server.properties config/server-cluster-1.properties
cp config/server.properties config/server-cluster-2.properties
broker.id=1
listeners=SASL_PLAINTEXT://0.0.0.0:9093
advertised.listeners=SASL_PLAINTEXT://[暴露的IP]:9093
log.dirs=/data/kafka/logs-1
broker.id=2
listeners=SASL_PLAINTEXT://0.0.0.0:9094
advertised.listeners=SASL_PLAINTEXT://[暴露的IP]:9094
log.dirs=/data/kafka/logs-2
export KAFKA_OPTS=" -Djava.security.auth.login.config=[KAFKA安装路径]/config/kafka_server_jaas.conf"
exec $base_dir/kafka-run-class.sh $EXTRA_ARGS kafka.Kafka "$@"
./kafka-configs.sh --zookeeper 127.0.0.1:2185 --alter --add-config 'SCRAM-SHA-256=[iterations=8192,password=123456],SCRAM-SHA-512=[password=123456]' --entity-type users --entity-name hanmeimei
nohup ./kafka-server-start.sh ../config/server.properties 1>/dev/null 2>&1 &
nohup ./kafka-server-start.sh ../config/server-cluster-1.properties 1>/dev/null 2>&1 &
nohup ./kafka-server-start.sh ../config/server-cluster-2.properties 1>/dev/null 2>&1 &
执行命令ps -ef|grep kafka ,发现有4个进程,分别是zookeeper,3个kafka服务说明启动成功
验证
Topic验证
./kafka-topics.sh --create --zookeeper 127.0.0.1:2185 --replication-factor 1 --partitions 1 --topic centerm-cluster-topic
看到打印Created topic centerm-cluster-topic. 说明配置正确
生产者验证
bootstrap.servers=[暴露IP]:9092,[暴露IP]:9093,[暴露IP]:9094
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="[YOUR NAME]" password="[YOUR PWD]";
security.protocol=SASL_PLAINTEXT
sasl.mechanism=SCRAM-SHA-256
./kafka-console-producer.sh --bootstrap-server [暴露IP]:9092,[暴露IP]:9093,[暴露IP]:9094 --topic centerm-cluster-topic --producer.config ../config/producer.properties
没有报错,并且出现输入控制台表示配置启动成功了,随便输入一些信息,接着验证消费者
消费者验证
bootstrap.servers=[暴露IP]:9092,[暴露IP]:9093,[暴露IP]:9094
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="[YOUR NAME]" password="[YOUR PWD]";
security.protocol=SASL_PLAINTEXT
sasl.mechanism=SCRAM-SHA-256
./kafka-console-consumer.sh --bootstrap-server [暴露IP]:9092,[暴露IP]:9093,[暴露IP]:9094 --topic centerm-cluster-topic --consumer.config ../config/consumer.properties --from-beginning
如果能收到刚刚发送的消息验证成功
|