linux安装kafka
Zookeeper(单机)安装
- 下载
wget -P /opt/ https://mirrors.huaweicloud.com/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
- 解压
tar -zxvf zookeeper-3.4.10.tar.gz
- 重命名
mv zookeeper-3.4.10 zookeeper
- 修改zoo.cfg
复制文件
cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg
- 修改zoo.cfg
vim /opt/zookeeper/conf/zoo.cfg
- 输入以下内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zookeeper/data
dataLogDir=/opt/zookeeper/datalog
clientPort=2181
server.1=localhost:2888:3888
创建myid文件
创建文件夹
mkdir -p /opt/zookeeper/data
创建myid文件,并将其内容改为1
echo 1 >/opt/zookeeper/data/myid
配置环境变量
vim /etc/profile
加入下面内容
export ZOOKEEPER_HOME=/opt/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
然后
source /etc/profile
Kafka(单机安装)
下载
wget -P /opt/ https://mirrors.huaweicloud.com/apache/kafka/2.1.1/kafka_2.11-2.1.1.tgz
解压
tar -zxvf kafka_2.11-2.1.1.tgz
重命名
mv kafka_2.11-2.1.1 kafka
修改server.properties
vim /opt/kafka/config/server.properties
输入以下内容
broker.id=0
port=9092
host.name=localhost
log.dirs=/opt/kafka/log
zookeeper.connect=localhost:2181
offsets.topic.replication.factor=1
配置环境变量
vim /etc/profile
加入下面内容
export KAFKA_HOME=/opt/kafka
export PATH=$PATH:$KAFKA_HOME/bin
然后
source /etc/profile
启动Kafka
先启动Zookeeper
zkServer.sh start
查看状态
zkServer.sh status
启动成功
再启动Kafka
/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
启动脚本(在Zoopeeker和Kafka都关闭下使用启动脚本)
创建文件
vim /opt/kafka/bin/start-kafka.sh
添加下面内容
#!/bin/bash
#启动zookeeper
/opt/zoopeeker/bin/zkServer.sh start
sleep 3
#默默等3秒后执行
#启动kafka
/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties
给文件执行权限
chmod 777 /opt/kafka/bin/start-kafka.sh
启动
start-kafka.sh
启动成功如下图
Kafka指令
创建名为first分区为2,副本数为1,的topic
kafka-topics.sh --create -zookeeper localhost:2181 --topic first --partitions 2 --replication-factor 1
查看所有的topic
kafka-topics.sh --list -zookeeper localhost:2181
删除名为first的topic
kafka-topics.sh --delete -zookeeper localhost:2181 --topic first
查看名为first的topic
kafka-topics.sh --describe -zookeeper localhost:2181 --topic first
发送消息
kafka-console-producer.sh --broker-list localhost:9092 --topic first
消费消息
kafka-console-consumer.sh --topic first --bootstrap-server localhost:9092 --from-beginning