云服务器(Centos7)设置kafka认证密码
以kafak3.2.0版本为例,先解压到某个目录,修改配置文件完成。
安装kafka教程传送门:https://blog.csdn.net/wssc555/article/details/127857626
测试正常启动后开始设置认证密码。
1.修改kafka配置文件 server.properties
listeners=SASL_PLAINTEXT://内网ip:9092
# Listener name, hostname and port the broker will advertise to clients.
# If not set, it uses the value for "listeners".
advertised.listeners=SASL_PLAINTEXT://外网ip:9092
# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
#使用的认证协议
security.inter.broker.protocol=SASL_PLAINTEXT
#SASL机制
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
#完成身份验证的类
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
#如果没有找到ACL(访问控制列表)配置,则允许任何操作。
allow.everyone.if.no.acl.found=false
#需要开启设置超级管理员,设置admin用户为超级管理员
super.users=User:admin
2.添加SASL配置文件
在kafka安装目录下创建一个 kafka_server_jaas.conf 文件
vi /config/kafka_server_jaas.conf:
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin@123"
user_admin="admin@123";
};
说明:该配置通过org.apache.org.apache.kafka.common.security.plain.PlainLoginModule由指定采用PLAIN机制,定义了用户。
usemame和password指定该代理与集群其他代理初始化连接的用户名和密码
"user_"为前缀后接用户名方式创建连接代理的用户名和密码,例如,user_producer=“producerpwd” 是指用户名为producer,密码为producerpwd
username为admin的用户,和user为admin的用户,密码要保持一致,否则会认证失败
上述配置中,创建了三个用户,分别为admin、producer和consumer(创建多少个用户,可根据业务需要配置,用户名和密码可自定义设置)
3.修改启动脚本
vi /bin/kafka-server-start.sh ,加入一行:
export KAFKA_OPTS="-Djava.security.auth.login.config=/usr/kafka_2.12-3.2.0/config/kafka_server_jaas.conf"
如图:
4.启动kafka服务
# 进入kafka_2.12-3.2.0目录下
[root@VM-20-8-centos kafka_2.12-3.2.0]#
# 先启动zookeeper
bin/zookeeper-server-start.sh -daemon ./config/zookeeper.properties
# 启动kafka
bin/kafka-server-start.sh -daemon ./config/server.properties
5.测试连接
5.1使用kafkatool测试
在JAAS Config中配置用户名和密码
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin@123";
点击Connect,若能连接成功,左侧红灯变为绿色。
5.2使用Springboot连接
引入kafka依赖
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.2.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
在application.yml中配置
spring:
kafka:
# 消费者
consumer:
group-id: 消费组id
auto-offset-reset: earliest
bootstrap-servers: 云服务器外网ip
# 生产者
producer:
bootstrap-servers: 云服务器外网ip
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
properties:
security:
protocol: SASL_PLAINTEXT
sasl:
mechanism: PLAIN
jaas:
config: 'org.apache.kafka.common.security.scram.ScramLoginModule required username="admin" password="admin@123";'
启动项目
连接成功。