本文描述 Linux 上搭建单节点kafka,以Centos7.2为例。
1. 准备kafka代码
wget http://mirror.bit.edu.cn/apache/kafka/0.10.2.0/kafka_2.11-0.10.2.0.tgz
解压压缩包:
tar -zxvf kafka_2.11-0.10.2.0.tgz
cd kafka_2.11-0.10.2.0
其中bin目录下存放了操作kafka和zk的所需脚本。
2. 启动zookeeper服务
Kafka需要借助ZooKeeper服务,所以需要先启动ZooKeeper服务,启动方法如下:
[root@node1 kafka_2.11-0.10.2.0]# bin/zookeeper-server-start.sh config/zookeeper.properties
[2017-10-24 19:24:56,725] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...
3. 启动kafka服务
[root@node1 kafka_2.11-0.10.2.0]# bin/kafka-server-start.sh config/server.properties
[2017-10-24 19:26:54,639] INFO KafkaConfig values:
advertised.host.name = null
advertised.listeners = null
advertised.port = null
...
4. 创建一个topic
[root@node1 kafka_2.11-0.10.2.0]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic topic1
Created topic "topic1".
5. 查看topic列表
可以查看到上面新建的topic是否已经在topic列表中:
[root@node1 kafka_2.11-0.10.2.0]# bin/kafka-topics.sh --list --zookeeper localhost:2181
__consumer_offsets
topic1
除了手动创建topic,还可以配置代理服务器(broker),当一个不存在的topic被发布的时候它能自动创建相应的topic。
6. 启动生产者
Kafka带有一个命令行客户端,获取从文件或来自标准输入的输入,并作为消息发送到Kafka集群。默认情况下,每一行将被作为单独的消息发送。运行生产者脚本,然后输入一些信息到控制台发送到服务器。
[root@node1 kafka_2.11-0.10.2.0]# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic topic1
this is message
this is another message
7. 启动消费者
[root@node1 kafka_2.11-0.10.2.0]# bin/kafka-console-consumer.sh --bootsttrp-server localhost:9092 --topic topic1 --from-beginning
bootsttrp-server is not a recognized option
[root@node1 kafka_2.11-0.10.2.0]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic1 --from-beginning
this is message
this is another message
已经收到生产者发送的消息。
下一篇:kafka多节点集群