当前kafka2的最新版本是kafka_2.13-2.8.2,使用kafka3将不支持jdk8,本文详细介绍kafka_2.13-2.8.2在ubuntu18.04的安装及使用。
kafka官网:kafka官网地址 https://kafka.apache.org/
jdk8安装方式请参考:Ubuntu18安装jdk8 https://blog.csdn.net/haixiangyun/article/details/132584713
kafka下载地址:kafka下载链接 https://kafka.apache.org/downloads
我的kafka压缩包放到/usr/local/下,解压kafka压缩文件并进入到kafka目录
cd /usr/local/
tar -zxf kafka_2.13-2.8.2.tgz
cd kafka_2.13-2.8.2
配置listeners
sudo vim config/server.properties
在kafka目录中启动
sudo bin/zookeeper-server-start.sh config/zookeeper.properties
如果是自己下载jdk配置环境变量会报错找不到java:
/usr/local/kafka_2.13-2.8.2/bin/kafka-run-class.sh: 第 330 行:exec:java:未找到
需要修改kafka中JAVA_HOME配置,
sudo vim bin/kafka-run-class.sh
找到JAVA_HOME的配置并修改保存
再重新执行启动zookeeper即可成功。如果是通过apt安装jdk8不会存在以上问题。
zookeeper服务保持开启,重新开一个终端执行
sudo bin/kafka-server-start.sh config/server.properties
保持zookeeper和kafka的服务开启,新开一个终端
sudo bin/kafka-topics.sh --create --topic topic-test --bootstrap-server localhost:9092
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
bin/kafka-console-consumer.sh --topic topic-test --from-beginning --bootstrap-server localhost:9092
保持zookeeper和kafka的服务开启,监听topic消息的终端开着
bin/kafka-console-consumer.sh --topic topic-test --from-beginning --bootstrap-server localhost:9092
在此终端输入消息,按回车发送,在监听端就会输出消息。说明kafka安装成功。
cd /lib/systemd/system/
sudo vim zookeeper.service
输入如下内容:(kafka安装目录换成自己的)
[Unit]
Description=Apache Kafka Zookeeper server
Documentation=http://zookeeper.apache.org
Requires=network.target remote-fs.target
After=network.target remote-fs.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/kafka_2.13-2.8.2/bin/zookeeper-server-start.sh /usr/local/kafka_2.13-2.8.2/config/zookeeper.properties
ExecStop=/usr/local/kafka_2.13-2.8.2/bin/zookeeper-server-stop.sh
Restart=on-abnormal
[Install]
WantedBy=multi-user.target
sudo vim kafka.service
输入如下内容:(jdk和kafka安装目录换成自己的)
[Unit]
Description=Apache Kafka server (broker)
Documentation=http://kafka.apache.org/documentation.html
Requires=network.target remote-fs.target
After=network.target remote-fs.target zookeeper.service
[Service]
Type=simple
User=root
Group=root
Environment="JAVA_HOME=/usr/local/jdk1.8.0_381"
ExecStart=/usr/local/kafka_2.13-2.8.2/bin/kafka-server-start.sh /usr/local/kafka_2.13-2.8.2/config/server.properties
ExecStop=/usr/local/kafka_2.13-2.8.2/bin/kafka-server-stop.sh
Restart=on-abnormal
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
启动zookeeper
sudo systemctl start zookeeper
启动kafka
sudo systemctl start kafka
查看zooekeeper状态
sudo systemctl status zookeeper
查看kafka状态
sudo systemctl status kafka
停止kafka服务
sudo systemctl stop kafka
停止zookeeper服务
sudo systemctl stop zookeeper
sudo systemctl enable zookeeper
sudo systemctl enable kafka
重启ubuntu18进行测试
kafka安装还是比较简单,需要注意的是对jdk版本的依赖,以及启动zookeeper时对jdk的配置