01-kafka单机部署

前言:
本方法适用于开发测试环境。
集群方式搭建 见 《kafka集群搭建》

  • 环境说明:
    我的测试环境是阿里云ECS.
    IP:172.xxx.xxx.149
    绑定弹性IP:47.xxx.xxx.140

1.下载

url
http://kafka.apache.org/

2. 拷贝到服务器

  • 下载完毕后,拷贝到/data 目录下,并解压缩。结果如下:
[root@kafka-01 data]# ll
drwxr-xr-x 7 root root     4096 32 15:28 kafka_2.13-2.7.0
-rw-r--r-- 1 root root 68583422 32 15:07 kafka_2.13-2.7.0.tgz
drwx------ 2 root root    16384 32 10:54 lost+found
  • 为方便使用,在/opt下创建一个软连接
[root@kafka-01 data]# ln -s /data/kafka_2.13-2.7.0 /opt/kafka
[root@kafka-01 data]# cd /opt/
[root@kafka-01 opt]# ll
总用量 4
lrwxrwxrwx  1 root root   22 32 15:33 kafka -> /data/kafka_2.13-2.7.0

3. 启动zookeeper

测试环境使用kafka自带zookeeper即可

# cd /opt/kafka/bin
# nohup ./zookeeper-server-start.sh ../config/zookeeper.properties > /tmp/zookeeper-run.log 2>&1 &

4. 启动kafka

  • 修改配置文件
    修改/opt/kafka/config/server.properties 文件
############################# Socket Server Settings #############################
listeners=SASL_PLAINTEXT://172.xxx.xxx.149:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
advertised.listeners=SASL_PLAINTEXT://47.xxx.xxx.140:9092

listeners写ECS的IP地址
advertised.listeners 写绑定的弹性IP(因为测试的是阿里云环境,因此需要多配置一个外网,如果不用外网只去掉这一行配置即可)

  • 设置server密码
    创建 /opt/kafka/config/kafka_server_jaas.conf 文件
KafkaServer {
    org.apache.kafka.common.security.plain.PlainLoginModule required
    username="kafka"
    password="xxxxxxx"
    user_kafka="xxxxxxx"
    user_enduser="xxxxxxx";
};

修改启动脚本

在 /opt/kafka/bin/kafka-server-start.sh脚本中,kafka-run-class.sh启动前,添加如下内容:

if [ "x$KAFKA_OPTS" = "x"  ]; then
    export KAFKA_OPTS="-Djava.security.auth.login.config=/opt/kafka/config/kafka_server_jaas.conf"
fi
  • 为 client设置密码
    创建 /opt/kafka/config/kafka_client_jaas.conf文件,写入登陆用户名密码
KafkaClient {
        org.apache.kafka.common.security.plain.PlainLoginModule required
        username="enduser"
        password="xxxxxx";
};
  • 修改consumer 脚本
    修改 /opt/kafka/bin/kafka-console-consumer.sh 脚本,在kafka-run-class.sh启动前,添加如下内容
if [ "x$KAFKA_OPTS" = "x"  ]; then
    export KAFKA_OPTS="-Djava.security.auth.login.config=/opt/kafka/config/kafka_client_jaas.conf"
fi
  • 修改producer 脚本
    修改 /opt/kafka/bin/kafka-console-producer.sh 脚本,在kafka-run-class.sh启动前,添加如下内容
if [ "x$KAFKA_OPTS" = "x"  ]; then
    export KAFKA_OPTS="-Djava.security.auth.login.config=/opt/kafka/config/kafka_client_jaas.conf"
fi
  • 启动
nohup /opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties 1>/dev/null 2>&1 &

5. 测试

  • 创建topic
./kafka-topics.sh  --zookeeper localhost:2181 --create --topic Test  --partitions 1 --replication-factor 1
  • 生产者
    打开一个终端,执行如下命令(ip写内网外网回环都可以),进入console后随便输入一串数字
[root@kafka-01 bin]# ./kafka-console-producer.sh   --broker-list 47.xxx.xxx.140:9092 --topic Test --producer-property security.protocol=SASL_PLAINTEXT --producer-property sasl.mechanism=PLAIN
>111
>hongjian
  • 消费者
    打开生产者的同时,打开一个消费者。可以看到接收到生产者的消息了。
[root@kafka-01 bin]# ./kafka-console-consumer.sh --bootstrap-server 47.xxx.xxx.140:9092 --topic Test --consumer-property security.protocol=SASL_PLAINTEXT --consumer-property sasl.mechanism=PLAIN --from-beginning
111
hongjian

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值