1、简介java
官网:http://kafka.apache.org/apache
Apache Kafka是分布式发布-订阅消息系统。它最初由LinkedIn公司开发,以后成为Apache项目的一部分。Kafka是一种快速、可扩展的、设计内在就是分布式的,分区的和可复制的提交日志服务。bootstrap
Apache Kafka与传统消息系统相比,有如下不一样:服务器
它被设计为一个分布式系统,易于向外扩展;socket
它同时为发布和订阅提供高吞吐量;分布式
它支持多订阅者,当失败时能自动平衡消费者;this
它将消息持久化到磁盘,所以可用于批量消费,例如ETL,以及实时应用程序。.net
2、安装设计
下载地址:wget http://mirrors.shuosc.org/apache/kafka/1.0.2/kafka_2.11-1.0.2.tgz日志
解压:tar -zxvf kafka_2.11-1.0.2.tgz
cd kafka_2.11-1.0.2
3、启动服务器
一、启动ZooKeeper
Kafka使用ZooKeeper,因此您须要先启动一个ZooKeeper服务器,若是您尚未。您可使用随Kafka一块儿打包的便捷脚原本获取一个快速可是比较粗糙的单节点ZooKeeper实例。
启动命令:bin/zookeeper-server-start.sh config/zookeeper.properties
这个 zookeeper中主要就3个配置:
# the directory where the snapshot is stored.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=0
咱们须要记住zookeeper的端口 2181,在后面会用到。
二、Kafka基本配置
Kafka在config目录下提供了一个基本的配置文件。为了保证能够远程访问Kafka,咱们须要修改两处配置。
打开config/server.properties文件,在很靠前的位置有listeners和 advertised.listeners两处配置的注释,去掉这两个注释,而且根据当前服务器的IP修改以下:
# 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://: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.163.10:9092
当前服务器IP为192.168.163.10,你须要修改成外网或局域网能够访问到的服务器IP。
三、启动Kafka
接下来启动Kafka服务:
启动命令:bin/kafka-server-start.sh config/server.properties
四、建立 Topic
使用下面的命令建立 Topic。
命令:bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
五、启动一个消费者
在一个新的终端执行下面的命令。
命令:bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
六、启动生产者
命令:bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
启动后,能够输入内容,而后回车。
此时你应该能够在上一个消费者中看到有消息输出。
七、查看 topic 列表
命令:bin/kafka-topics.sh --list --zookeeper localhost:2181
八、查看描述 topics 信息
命令:bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
第一行给出了全部分区的摘要,每一个附加行给出了关于一个分区的信息。 因为咱们只有一个分区,因此只有一行。
“Leader”: 是负责给定分区的全部读取和写入的节点。 每一个节点将成为分区随机选择部分的领导者。
“Replicas”: 是复制此分区日志的节点列表,不管它们是不是领导者,或者即便他们当前处于活动状态。
“Isr”: 是一组“同步”副本。这是复制品列表的子集,当前活着并被引导到领导者。