注意:操作前打开2181和9092端口
1.安装zookeeper
wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
tar -zxvf zookeeper-3.4.10.tar.gz
cd zookeeper-3.4.10/conf
2.把zoo_sample.cfg复制一份命名为zoo.cfg
3.编辑zoo.cfg
vim zoo.cfg
修改内容:
tickTime=2000
initLimit=10
syncLimit=5
#目录自行创建
dataDir=/tmp/zookeeper
#dataLogDir=/var/log/kafka/zk
clientPort=2181
4.安装kafka
wget http://mirrors.hust.edu.cn/apache/kafka/1.1.0/kafka_2.11-1.1.0.tgz
tar -zxvf kafka_2.11-1.1.0.tgz
5.进入目录,修改配置文件
cd kafka_2.11-1.1.0/config
vim server.properties
打开注释:
#listeners=PLAINTEXT://:9092
#advertised.listeners=PLAINTEXT://your.host.name:9092
更改zookeeper的配置,把local host改为自己的zookeeper地址,若zookeeper在本地可以不用修改
6.修改配置文件
vim zookeeper.properties
添加:host.name=192.168.2.48
advertised.host.name=192.168.2.48
advertised.port=9092
7.启动kafka
进入bin目录执行命令:
启动zookeeper:./zookeeper-server-start.sh /opt/kafka_2.11-1.1.0/config/zookeeper.properties
启动kafka:./kafka-server-start.sh /opt/kafka_2.11-1.1.0/config/server.properties
关闭zookeeper:./zookeeper-server-stop.sh /opt/kafka_2.11-1.1.0/config/zookeeper.properties
关闭kafka:./kafka-server-stop.sh /opt/kafka_2.11-1.1.0/config/server.properties
8.测试
创建一个名为test的topic:./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看所有的topic:./kafka-topics.sh --list --zookeeper localhost:2181
发送消息:./kafka-console-producer.sh --broker-list localhost:9092 --topic test
输入 hello kafka
接收消息:./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning