# 设置下主机名
[root@localhost ~]# hostnamectl set-hostname kafka
[root@localhost ~]# tar -zxvf kafka_2.11-1.1.1.tgz
[root@localhost ~]# cd kafka_2.11-1.1.1
启动Zookeeper
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
修改kafka配置,为了远端能够访问到kafka
打开config/server.properties文件,在很靠前的位置有listeners和 advertised.listeners两处配置的注释,去掉这两个注释,并且根据当前服务器的IP修改如下:
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://192.168.4.139:9092
# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
advertised.listeners=PLAINTEXT://192.168.4.139:9092
启动Kafka 服务
bin/kafka-server-start.sh -daemon config/server.properties
创建topic
使用kafka-topics.sh 创建单分区单副本的topic test:
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看topic
bin/kafka-topics.sh --list --zookeeper localhost:2181
产生消息
使用kafka-console-producer.sh 发送消息:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
随便敲点内容,然后ctrl+c
消费消息
使用kafka-console-consumer.sh 接收消息并在终端打印:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning