1、修改config/server.properties,添加如下内容
listeners=SASL_PLAINTEXT://内网ip:9092
advertised.listeners=SASL_PLAINTEXT://外网ip:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN
2、添加相关安全认证配置文件
在config目录下添加kafka-zk-jaas.conf
zookeeper {org.apache.kafka.common.security.plain.PlainLoginModule requiredusername="admin"password="xxxxxx";
};
在config目录下添加kafka-server-jaas.conf
KafkaServer {org.apache.kafka.common.security.plain.PlainLoginModule requiredusername="admin"password="xxxxxx"user_admin="xxxxxx";
};
在config目录下添加kafka-client-jaas.conf
KafkaClient {org.apache.kafka.common.security.plain.PlainLoginModule requiredusername="admin"password="xxxxxx";
};
3、修改相关启动文件应用上面添加的配置文件去启动
修改zookeeper-server-start.sh,在exec前一行添加:
export KAFKA_OPTS="-Djava.security.auth.login.config=file:$base_dir/../config/kafka-zk-jaas.conf"
修改kafka-server-start.sh,在exec前一行添加:
export KAFKA_OPTS="-Djava.security.auth.login.config=file:$base_dir/../config/kafka-server-jaas.conf"
修改kafka-console-consumer.sh,在exec前一行添加:
export KAFKA_OPTS="-Djava.security.auth.login.config=file:$(dirname $0)/../config/kafka-client-jaas.conf"
修改kafka-console-producer.sh,在exec前一行添加:
export KAFKA_OPTS="-Djava.security.auth.login.config=file:$(dirname $0)/../config/kafka-client-jaas.conf"
4、修改springboot配置
spring.kafka.bootstrap-servers=外网ip:9092
spring.kafka.properties.security.protocol=SASL_PLAINTEXT
spring.kafka.properties.sasl.mechanism=PLAIN
spring.kafka.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="xxxxxx";
spring.kafka.consumer.properties.sasl.mechanism=PLAIN
spring.kafka.consumer.properties.security.protocol=SASL_PLAINTEXT
5、kafka启动停止命令
切换到kafka安装目录:cd /home/kafka_2.13-2.6.0/
zookeeper启动命令:./bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
kafka启动命令:./bin/kafka-server-start.sh -daemon config/server.properties
kafka停止命令:./bin/kafka-server-stop.sh
zookeeper停止命令:./bin/zookeeper-server-stop.sh
查看zookeeper是否启动成功:ps -ef | grep zookeeper
查看kafka是否启动成功:ps -ef | grep kafka