开发环境搭建

仍然使用hombrew安装

#$ brew cask install java
$ brew install kafka

安装完成后提示:

20190528155902504628190.png

然后启动ZookeeperKafka:

启动Zookeeper

zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties

启动kafka

kafka-server-start /usr/local/etc/kafka/server.properties

警告处理

WARNING:
During server start, you might be facing connection broken issue.

处理方法,修改server.properties

$ vim /usr/local/etc/kafka/server.properties

注释以下内容

listeners=PLAINTEXT://:9092

并更新为

############################# Socket Server Settings #############################
# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://localhost:9092

重启服务生效

测试

创建 Kafka Topic:

A topic is a category or feed name to which records are published. Topics in Kafka are always multi-subscriber; that is, a topic can have zero, one, or many consumers that subscribe to the data written to it.

主题是发布记录的类别或订阅源名称。 Kafka的主题总是多用户; 也就是说,一个主题可以有零个,一个或多个消费者订阅写入它的数据。

创建一个topic

创建一个名字为test的topic

kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

初始化 Producer console

初始化 Kafka producer console, 它将会监听localhost的9092端口,基于testtopic:

$ kafka-console-producer --broker-list localhost:9092 --topic test
>send first message
>send second message
>wow it is working

初始化 Consumer console

初始化 Kafka Consumer console, which will listen to bootstrap server localhost at port 9092 at topic test from beginning:

$ kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
send first message
send second message
wow it is working

参考

Apache Kafka Installation on Mac using Homebrew