Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据。本文介绍 Kafka 的安装部署与使用方法。
介绍
Apache Kafka是分布式发布-订阅消息系统。它最初由LinkedIn公司开发,之后成为Apache项目的一部分。Kafka是一种快速、可扩展的、设计内在就是分布式的,分区的和可复制的提交日志服务。
Apache Kafka与传统消息系统相比,有以下不同:
- 它被设计为一个分布式系统,易于向外扩展;
- 它同时为发布和订阅提供高吞吐量;
- 它支持多订阅者,当失败时能自动平衡消费者;
- 它将消息持久化到磁盘,因此可用于批量消费,例如ETL,以及实时应用程序。
安装
依赖zk,安装部署见Zookeeper 安装使用
kafka 单节点
下载地址:https://kafka.apache.org/downloads 下载 kafka_2.11-1.0.0.tgz 安装包。
解压:
tar -zxvf kafka_2.11-1.0.0.tgz
cd /usr/local/kafka_2.11-1.0.0/
修改 kafka-server 的配置文件
vim /usr/local/kafka/config/server.properties
修改其中的:
broker.id=1
log.dir=/data/kafka/logs-1
使用 kafka-server-start.sh 启动 kafka 服务:
bin/kafka-server-start.sh config/server.properties
kafka 集群
Kafka 支持两种模式的集群搭建:
- 在单机上运行多个 broker 实例来实现集群
- 在多台机器上搭建集群
下面介绍下如何实现单机多 broker 实例集群,其实很简单,只需要如下配置即可。
利用单节点部署多个 broker。 不同的 broker 设置不同的 id,监听端口及日志目录。 例如:
cp config/server.properties config/server-2.properties
cp config/server.properties config/server-3.properties
vim config/server-2.properties
和 config/server-3.properties
,修改 :
broker.id=2
listeners = PLAINTEXT://your.host.name:9093
log.dir=/data/kafka/logs-2
和
broker.id=3
listeners = PLAINTEXT://your.host.name:9094
log.dir=/data/kafka/logs-3
启动Kafka服务:
bin/kafka-server-start.sh config/server-2.properties &
bin/kafka-server-start.sh config/server-3.properties &
至此,单机多broker实例的集群配置完毕。
分别在多个节点按上述方式安装 Kafka,配置启动多个 Zookeeper 实例。
假设三台机器 IP 地址是 : 10.0.0.1, 10.0.0.2, 10.0.0.3
分别配置多个机器上的 Kafka 服务,设置不同的 broker id
,zookeeper.connect
设置如下:
vim config/server.properties
里面的 zookeeper.connect
修改为:
broker.id=1/2/3
zookeeper.connect=10.0.0.1:2181,10.0.0.2:2181,10.0.0.3:2181
使用
创建 topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test_topic
查看 topic 列表
bin/kafka-topics.sh --list --zookeeper localhost:2181
查看 topic 详情
bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --topic test_topic --describe
列出了 test_topic
的 parition
数量、replica因子
以及每个 partition
的 leader
、replica
信息
查看 consumer 列表
bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server 127.0.0.1:9292 --list
bin/kafka-consumer-groups.sh --zookeeper 127.0.0.1:2181 --list
查看 consumer 详情
bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server 127.0.0.1:9292 --group test --describe
bin/kafka-consumer-groups.sh --zookeeper 127.0.0.1:2181 --group console-consumer-1 --describe
其中依次展示group名称、消费的topic名称、partition id、consumer group最后一次提交的offset、最后提交的生产消息offset、消费offset与生产offset之间的差值、当前消费topic-partition的group成员id(不一定包含hostname)
检测
数据上报
bin/kafka-topics.sh --list --zookeeper $zkaddr
监控 topic 数据:
bin/kafka-console-consumer.sh --zookeeper 127.0.0.1:2181/common_kafka --topic $topic
127.0.0.1:2181/common_kafka
来自 kafka/config/server.properties
可视化
F&Q
启动失败问题
错误日志:
ARN Found a corrupted index file due to requirement failed: Corrupt index found, index file (/data/...-0/00000000000000108309.index) has non-zero size but the last offset is 108309 which is no larger than the base offset 108309.}. deleting /data/...-0/00000000000000108309.timeindex, /data/...-0/00000000000000108309.index and rebuilding index
解决方法:收到删除对应的index,后重启服务。