作者:crane-yuan 日期:2017-08-31
介紹
Apache Kafka was originated at LinkedIn and later became an open sourced Apache project in 2011, then First-class Apache project in 2012. Kafka is written in Scala and Java. Apache Kafka is publish-subscribe based fault tolerant messaging system. It is fast, scalable and distributed by design.
安裝kafka服務(wù)器
kafka啟動(dòng)需要依賴zookeeper和Java8環(huán)境,所以需要先安裝zookeeper和java8
注:本人實(shí)驗(yàn)使用的kafka版本為0.8.2.x
ArchLinux下安裝命令
> yaourt -S zookeeper
> yaourt -S java8
> yaourt -S kafka #可以自己修改PKGBUILD文件帝璧,安裝指定版本
Mac下安裝命令
> brew install zookeeper
> brew cask install java
> brew install kafka
配置zookeeper
# conf/zoo.cfg
tickTime=2000
dataDir=/var/zookeeper
clientPort=2181
配置kafka
/etc/kafka/server.properties
broker.id=0
auto.create.topics.enable=true # 自動(dòng)創(chuàng)建topic
delete.topic.enable=true # 使刪除topic命令可以起作用
host.name=localhost
zookeeper.connect=localhost:2181
啟動(dòng)kafka server
> sudo zkServer.sh start
> sudo kafka-server-start.sh /etc/kafka/server.properties # 可以加上-daemon選項(xiàng)在后臺(tái)以守護(hù)進(jìn)程運(yùn)行
創(chuàng)建topic
> kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test-topic
獲取topic列表
> kafka-topics.sh --list --zookeeper localhost:2181
寫消息到topic
> kafka-console-producer.sh --broker-list localhost:9092 --topic test-topic
# 輸入字符,以回車結(jié)束一條消息
從topic讀取消息
> kafka-console-consumer.sh --zookeeper localhost:2181 --topic test-topic --from-beginning
刪除topic
> kafka-topics.sh --zookeeper localhost:2181 --delete --topic test-topic # server.properties中delete.topic.enable=true設(shè)置時(shí)才有效