1. 下载Kafka


从​​Kafka官网​​下载最新的Kafka,目前我下载的版本为kafka_2.10-0.8.1.1.tgz

2、解压Kafka

tar -zxvf kafka_2.10-0.8.1.1.tgz

3. 修改Zookeeper服务器配置,并启动

cd kafka_2.10-0.8.1.1
vi config/zookeeper.properties

#修改ZooKeeper的数据目录
dataDir=/usr/local/db/zookeeper

#配置host.name和advertised.host.name为IP地址,防止通过Java客户端连接时解析为localhost
host.name=192.168.209.121

advertised.host.name=192.168.209.121

#启动Zookeeper服务器
./zookeeper-server-start.sh /usr/local/kafka_2.10-0.8.1.1/config/zookeeper.properties

后台启动Zookeeper

./zookeeper-server-start.sh  -daemon /usr/local/kafka_2.10-0.8.1.1/config/zookeeper.properties

4.修改Kafka配置,并启动Kafka服务器

vi config/server.properties 

log.dirs=/usr/local/kafka/kafka-logs

#启动Kafka服务器
./kafka-server-start.sh /usr/local/kafka_2.10-0.8.1.1/config/server.properties

后台启动Kafka

./kafka-server-start.sh  -daemon /usr/local/kafka_2.10-0.8.1.1/config/server.properties

5.创建并查看Topic

cd /usr/local/kafka_2.10-0.8.1.1/bin/
./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Created topic "test".

#查看刚才的topic
./kafka-topics.sh --list --zookeeper localhost:2181
test

6. 使用生产者发送消息,每行是一条独立的消息

./kafka-console-producer.sh --broker-list localhost:9092 --topic test
This is a message
This is My mesage

7. 使用消费者接收消息

./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
This is a message
This is My me