作者: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服务器

kafka启动需要依赖zookeeper和Java8环境,所以需要先安装zookeeper和java8

注:本人实验使用的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 # 自动创建topic
delete.topic.enable=true # 使删除topic命令可以起作用
host.name=localhost

zookeeper.connect=localhost:2181

启动kafka server

> sudo zkServer.sh start
> sudo kafka-server-start.sh  /etc/kafka/server.properties # 可以加上-daemon选项在后台以守护进程运行

创建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
# 输入字符,以回车结束一条消息

从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设置时才有效

参考文章


本文转载:CSDN博客