步骤
搭建kafka单机环境时,使用版本是kafka_2.11-1.0.0.tgz,步骤是按官网的介绍如下:
1. 下载1.0.0release 并解压。
1) tar -xzf kafka_2.11-1.0.0.tgz
2) cd kafka_2.11-1.0.0
2. 启动服务
Kafka因为依赖与ZooKeeper,所以需要首先启动ZooKeeper。如果没有安装ZooKeeper,则可以使用内置的单节点ZooKeeper实例
bin/zookeeper-server-start.shconfig/zookeeper.properties
启动服务:
bin/kafka-server-start.shconfig/server.properties
3. 建立topic
创建一个名为“test”的单分区、单replica的主题:
bin/kafka-topics.sh --create--zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看topic:
bin/kafka-topics.sh --list --zookeeper localhost:2181
4. 发送消息
bin/kafka-console-producer.sh--broker-list localhost:9092 --topic test
This is a message
This is anothermessage
5. 接收消息
bin/kafka-console-consumer.sh --bootstrap-serverlocalhost:9092 --topic test --from-beginning
This is a message
This is anothermessage
问题
按上述步骤执行到5,接收消息时,会出现以下的错误信息:
[2017-11-27 13:02:40,618] WARN [Controllerid=0, targetBrokerId=0] Connection to node 0 could not be established. Brokermay not be available. (org.apache.kafka.clients.NetworkClient)
解决:
Vim config/server.properties
发现zookeeper.connect= localhost:2181的设置是localhost,则修改server.properties,添加:
listeners=PLAINTEXT://localhost:9092
重新启动zookeeper、kafka、consumer、producer即可解决问题。