一,下载kafka
1,官网地址
http://kafka.apache.org/downloads.html
2,下载
[root@localhost source]# wget http://mirror.bit.edu.cn/apache/kafka/2.4.0/kafka_2.13-2.4.0.tgz
说明:架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,解压:
[root@localhost source]# tar -zxvf kafka_2.13-2.4.0.tgz
[root@localhost source]# mv kafka_2.13-2.4.0 /usr/local/soft/
三,启动zookeeper
[root@localhost source]# systemctl status zookeeper
如果状态不是running则:
[root@localhost source]# systemctl start zookeeper
四,创建日志目录:
[root@localhost source]# cd /data/logs/ [root@localhost logs]# mkdir kafkalogs [root@localhost logs]# chmod 777 kafkalogs
五,配置kafka的日志目录
[root@localhost ~]# vi /usr/local/soft/kafka_2.13-2.4.0/config/server.properties
只修改log.dirs的值,设置为自定义的kafka日志目录
log.dirs=/data/logs/kafkalogs
六,配置环境变量
[root@localhost ~]# vi /etc/profile
说明:增加kafka的所在目录变量,如下两行
export KAFKA_HOME=/usr/local/soft/kafka_2.13-2.4.0 export PATH=$PATH:$KAFKA_HOME/bin
七,启动服务/停止服务
启动kafka
[root@localhost ~]# /usr/local/soft/kafka_2.13-2.4.0/bin/kafka-server-start.sh -daemon /usr/local/soft/kafka_2.13-2.4.0/config/server.properties
停止kafka
[root@localhost ~]# /usr/local/soft/kafka_2.13-2.4.0/bin/kafka-server-stop.sh
八,测试kafka服务启动后端口是否绑定:
telnet到默认的端口9092
[root@localhost ~]# telnet localhost 9092 Trying ::1... Connected to localhost. Escape character is '^]'.
九,用systemd管理kafka服务
1,创建service文件
[root@localhost ~]# vi /etc/systemd/system/kafka.service
文件内容为:
[Unit] Description=Apache Kafka server (broker) After=network.target zookeeper.service [Service] Type=forking User=root Group=root Environment=JAVA_HOME=/usr/local/soft/jdk-13.0.2 ExecStart=/usr/local/soft/kafka_2.13-2.4.0/bin/kafka-server-start.sh -daemon /usr/local/soft/kafka_2.13-2.4.0/config/server.properties ExecStop=/usr/local/soft/kafka_2.13-2.4.0/bin/kafka-server-stop.sh #Restart=on-failure [Install] WantedBy=multi-user.target
注意:Environment=JAVA_HOME=/usr/local/soft/jdk-13.0.2
这一行必须有,否则启动和关停时会报错
2,测试启动、停止kafka服务
启动
[root@localhost ~]# systemctl start kafka
停止
[root@localhost ~]# systemctl stop kafka
十,解决一个问题:
kafka-server-stop.sh不能正常关闭kafka
说明:此问题在fedora30上存在,centos8上面未发现:
修改一下脚本即可:
[root@localhost system]# vi /usr/local/soft/kafka_2.13-2.4.0/bin/kafka-server-stop.sh
把得到进程id的命令改成用jps获取
修改为:
#PIDS=$(ps ax | grep -i 'kafka.Kafka' | grep java | grep -v grep | awk '{print $1}') PIDS=$(jps -lm | grep -i 'kafka.Kafka' | awk '{print $1}')
说明:jps最好加上绝对路径
十一,查看本地已安装的kafka的版本:
进入到kafka安装目录下的libs目录下面,可以看到kafka_2.13-2.4.0.jar这个文件,
也可以用命令来获取:
[root@localhost libs]# find ./ -name *kafka_* | head -1 | grep -o 'kafka[^ ]*' kafka_2.13-2.4.0.jar
说明:2.13是Scala 的版本,2.4.0就是你kafka的版本
十二,查看本地centos的版本:
[root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)