1、下载kafka
wget https://archive.apache.org/dist/kafka/2.8.1/kafka_2.12-2.8.1.tgz
2、解压到/usr/local/
mkdir -p /usr/local/kafka2.12
tar -zxf kafka_2.12-2.8.1.tgz -C /usr/local/kafka2.12 --strip-components=1 #--strip-components选项表示从目录级别上去除指定的前缀,以实现更加控制解压的效果
3、修改配置文件
cd /usr/local/kafka2.12/config/
vi server.properties
broker.id=0
log.dir=/data/kafka/logs #配置zookeeper管理kafka的路径
zookeeper.connect=localhost:2181
listeners=PLAINTEXT://:9092 #配置kafka的监听端口
advertised.listeners=PLAINTEXT://192.168.0.190:9092 #把kafka的地址端口注册给zookeeper,如果是远程访问要改成外网IP
4、创建kafka日志目录
mkdir -p /data/kafka/logs
5、配置kafka快捷路径
vi /etc/profile
export KAFKA_HOME=/usr/local/kafka2.12
export PATH=$KAFKA_HOME/bin:$PATH
source /etc/profile
6、启动、停止kafka
启动kafka前必须先启动zookeeper
cd /usr/local/kafka2.12/bin
kafka-server-start.sh -daemon ../config/server.properties #启动
kafka-server-stop.sh #停止
7、设置开机自动服务
vi /lib/systemd/system/kafka.service
[Unit]
Description=Apache Kafka server (broker)
After=network.target zookeeper.service
[Service]
Type=simple
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/jdk/1.8.0_391/bin"
User=root
Group=root
ExecStart=/usr/local/kafka2.12/bin/kafka-server-start.sh /usr/local/kafka2.12/config/server.properties
ExecStop=/usr/local/kafka2.12/bin/kafka-server-stop.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl enable kafka.service #加入开机启动
systemctl start kafka.service #启动
systemctl stop kafka.service #停止
systemctl status kafka.service #状态
8、开放防火墙
firewall-cmd --zone=public --add-port=9092/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
9、测试kafka配置开机启动是否成功
netstat -lntup
jps
10、创建一个topic
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic testTopic
11、查看创建了多少个topic
kafka-topics.sh --list --zookeeper localhost:2181
12、创建一个生产者进行发送消息
kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic
>whart^H
>what's your name?
>shtuup
>wangyulong
>kafka test is't
13、创建一个消费者来进行接收消息
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic testTopic --from-beginning
在此,单点式的kafka已经安装完毕,如果重启了之后,需要先删除kafka的logs日志记录,否则无法启动kafka。一般kafka我们会配合ES使用的。