Kafka 分布式消息系统的安装配置

官方文档 :http://kafka.apache.org/documentation.html#quickstart

安装配置

1
2
3
4
5
6
7
8
9
10
11
12
wget https://www.apache.org/dyn/closer.cgi?path=/kafka/0.8.2-beta/kafka_2.9.1-0.8.2-beta.tgz
解压后
cd config
vim server.properties

############################# Zookeeper #############################
# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=master:2181,slave1:2181,slave2:2181

启动服务

1
2
zookeeper-3.4.6/bin/zkServer.sh start &
bin/kafka-server-start.sh config/server.properties &

创建 查看 topic

1
bin/kafka-topics.sh --create --zookeeper master:2181 --replication-factor 1 --partitions 2 --topic test

其中 replication-factor 表示备份的数量,两个 partitions 就会在 logs 目录下产生 test-0 和 test-1 两个
另一台机器启动zookeeper客户端

1
2
3
4
./zkCli.sh -server localhost:2181 
Enter
ls /
[controller_epoch, storm, brokers, zookeeper, admin, consumers, config]

我们会发现这里也多了一个 brokers ,里面也有我们创建的 topics。

1
bin/kafka-topics.sh --list --zookeeper localhost:2181

写日志 查看日志

1
2
3
4
5
6
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test 
This is a message
This is another message
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
This is a message
This is another message