集群搭建
kafka集群规划
清单 | |
---|---|
kafka 版本 | kafka_2.11-0.11.0.3.tgz |
kafka安装机器 | hadoop101、hadoop102、hadoop103 |
zookeeper | hadoop101、hadoop102、hadoop103 |
安装步骤
解压
tar -zxvf kafka_2.11-0.11.0.3.tgz -C /opt/module/
修改配置文件
config/server.properties
#broker 的全局唯一编号,不能重复
broker.id=0
#删除 topic 功能使能
delete.topic.enable=true
#处理网络请求的线程数量
num.network.threads=3
#用来处理磁盘 IO 的线程数量
num.io.threads=8
#发送套接字的缓冲区大小
socket.send.buffer.bytes=102400
#接收套接字的缓冲区大小
socket.receive.buffer.bytes=102400
#请求套接字的缓冲区大小
socket.request.max.bytes=104857600
#kafka 运行日志存放的路径
log.dirs=/opt/module/kafka_2.11-0.11.0.3/data
#topic 在当前 broker 上的分区个数
num.partitions=1
#用来恢复和清理 data 下数据的线程数量
num.recovery.threads.per.data.dir=1
#segment 文件保留的最长时间,超时将被删除
log.retention.hours=168
#配置连接 Zookeeper 集群地址
zookeeper.connect=hadoop101:2181,hadoop102:2181,hadoop103:2181
vi /etc/profile
#Kafka
export KAFKA_HOME=/opt/module/kafka_2.11-0.11.0.3
export PATH=$PATH:$KAFKA_HOME/bin
分发机器,修改broker.id
启动/停止集群
#启动
bin/kafka-server-start -daemon config/server.properties
#停止
bin/kafka-server-stop.sh stop
群启/群停脚本
vi kafka-server-all.sh
#!/bin/bash
export KAFKA_CLUSTER="hadoop101 hadoop102 hadoop103"
startAll()
{
for i in $KAFKA_CLUSTER
do
echo "========== $i =========="
ssh $i 'source /etc/profile && $KAFKA_HOME/bin/kafka-server-start.sh -daemon $KAFKA_HOME/config/server.properties'
done
}
stopAll()
{
for i in $KAFKA_CLUSTER
do
echo "========== $i =========="
ssh $i 'source /etc/profile && $KAFKA_HOME/bin/kafka-server-stop.sh stop'
done
}
if [[ $# != 1 ]];then
echo "请输入执行参数!"
echo "$0 start|stop"
exit 1
fi
case $1 in
"start")
startAll;
;;
"stop")
stopAll;
;;
*)
echo "参数无效!!!"
;;
esac
群启/停
./kafka-server-all.sh start
./kafka-server-all.sh stop
Kafka 命令行操作
- 创建topic
注意:如果当前broker只有三个,如果我们想创建4个副本,是无法创建成功的。bin/kafka-topics.sh --zookeeper hadoop101:2181 \ --create --replication-factor 3 --partitions 3 \ --topic first
Error while executing topic command : replication factor: 4 larger than available brokers: 3 [2021-08-21 13:28:14,616] ERROR org.apache.kafka.common.errors.InvalidReplicationFactorException: replication factor: 4 larger than available brokers: 3 (kafka.admin.TopicCommand$)
- 查看topic
bin/kafka-topics.sh --zookeeper hadoop101:2181 --list
- 删除topic
需要将配置文件bin/kafka-topics.sh --zookeeper hadoop101:2181 --delete --topic first ###以下是输出内容 Topic first is marked for deletion. Note: This will have no impact if delete.topic.enable is not set to true.
KAFKA/config/server.properties
的属性delete.topic.enbale=true
,才能真正的删除topic。 - 发送消息
bin/kafka-console-producer.sh --broker-list hadoop101:9092 --topic first
- 消费消息
bin/kafka-console-consumer.sh --bootstrap-server hadoop101:9092 --topic first
- 查询单个topic信息
bin/kafka-topics.sh --zookeeper hadoop101:2181 --describe --topic first
- 修改分区大小(只能增加,不能减少)
试图减少分区,将报错bin/kafka-topics.sh --alter --zookeeper hadoop101:2181 --topic first --partitions 3
[hadoop@hadoop101 kafka_2.11-0.11.0.3]$ bin/kafka-topics.sh --alter --zookeeper hadoop101:2181 --topic first --partitions 2 WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected Error while executing topic command : The number of partitions for a topic can only be increased [2021-08-21 13:49:40,406] ERROR kafka.admin.AdminOperationException: The number of partitions for a topic can only be increased at kafka.admin.AdminUtils$.addPartitions(AdminUtils.scala:292) at kafka.admin.TopicCommand$$anonfun$alterTopic$1.apply(TopicCommand.scala:147) at kafka.admin.TopicCommand$$anonfun$alterTopic$1.apply(TopicCommand.scala:124) at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59) at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48) at kafka.admin.TopicCommand$.alterTopic(TopicCommand.scala:124) at kafka.admin.TopicCommand$.main(TopicCommand.scala:64) at kafka.admin.TopicCommand.main(TopicCommand.scala) (kafka.admin.TopicCommand$)
基本架构
Kafka 是一个分布式的基于发布/订阅模式的消息队列(Message Queue),主要应用于
大数据实时处理领域。
- 生产者
- 消费者(消费者组CG)
- kafka集群
- Broker
- Topic
- Partition
- Replication
- Leader
- Follower
- Zookeeper
kafka 存储架构
Kafka 中消息是以 topic 进行分类的,生产者生产消息,消费者消费消息,都是面向 topic
的。
topic 是逻辑上的概念,而 partition 是物理上的概念,每个 partition 对应于一个 log 文
件,该 log 文件中存储的就是 producer 生产的数据。Producer 生产的数据会被不断追加到该
log 文件末端,且每条数据都有自己的 offset。消费者组中的每个消费者,都会实时记录自己
消费到了哪个 offset,以便出错恢复时,从上次的位置继续消费。
- topic下有分区,相同分区间组成副本,副本内分为leader和follower
- 一个分区内有多个segment
- 一个segment下分一个.log文件和一个.index索引文件,其中文件命名都是以当前文件的第一条消息的offset进行命名。“.index”文件存储大量的索引信息,“.log”文件存储大量的数据,索引文件中的元
数据指向对应数据文件中 message 的物理偏移地址。
kafka是如何快速的定位到一条消息?
分区下有segment,每个segment下.分为index索引文件和.log数据文件,首先当一条读取消息,比如读取offset=xxx请求,broker可以通过segment的文件名,快速定位到当前offset在哪个segment内,此时可以通过二分查找法进行定位,然后由于.index中的数据是固定大小的元数据信息,可以根据当前segment的起始offset快速计算出指定offset的文件位置,直接读取对应offset的元数据,得到数据在.log文件中的偏移量,从而快速读取消息。