Kafka的基本介绍以及扩展

基本操作

新增Topic

指定两个分区,两个副本,replication不能大于集群中的broker数

[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --create --zookeeper hadoop01:2181 --partitions 2 --replication-factor 2 --topic hello
Created topic hello.

查询Topic

[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --list --zookeeper hadoop01:2181
hello

# 查看详细信息
[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --describe --zookeeper hadoop01:2181
Topic: hello	PartitionCount: 2	ReplicationFactor: 2	Configs: 
	Topic: hello	Partition: 0	Leader: 1	Replicas: 1,2	Isr: 1,2
	Topic: hello	Partition: 1	Leader: 2	Replicas: 2,0	Isr: 2,0

image-20240312094710653

修改Topic

修改partition的数量,只能增加

[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --alter --zookeeper hadoop01:2181 --partitions 5 --topic hello
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!


[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --describe --zookeeper hadoop01:2181
Topic: hello	PartitionCount: 5	ReplicationFactor: 2	Configs: 
	Topic: hello	Partition: 0	Leader: 1	Replicas: 1,2	Isr: 1,2
	Topic: hello	Partition: 1	Leader: 2	Replicas: 2,0	Isr: 2,0
	Topic: hello	Partition: 2	Leader: 0	Replicas: 0,2	Isr: 0,2
	Topic: hello	Partition: 3	Leader: 1	Replicas: 1,2	Isr: 1,2
	Topic: hello	Partition: 4	Leader: 2	Replicas: 2,0	Isr: 2,0

删除Topic

删除topic,删除操作是不可逆的,从1.0开始默认开启删除功能,之前的版本只会标记为删除状态,需要设置delete.topic.enable为true才可以真正删除

[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --delete --zookeeper hadoop01:2181 --topic helloTopic hello is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

生产者和消费者

创建生产者

bin/kafka-console-prodecer.sh

创建消费者

bin/kafka-console-consumer.sh
[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --create --zookeeper hadoop01:2181 --partitions 5 --replication-factor 2 --topic hello
Created topic hello.

# producer
[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-console-producer.sh --broker-list hadoop01:9092 --topic hello

# consumer 这个只消费最新的消息
[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-console-consumer.sh  --bootstrap-server hadoop01:9092 --topic hello

# 消费之前的消息
[root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-console-consumer.sh  --bootstrap-server hadoop01:9092 --topic hello --from-beginning

Broker扩展

配置文件server.properties

# The number of messages to accept before forcing a flush of data to disk
# 根据条数选择刷新磁盘的时机
log.flush.interval.messages=10000

# 根据消息的间隔时间刷新
# The maximum amount of time a message can sit in a log before we force a flush
log.flush.interval.ms=1000



# The minimum age of a log file to be eligible for deletion due to age  日志保存时间
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies  每隔5分钟检查文件是否满足删除的条件
log.retention.check.interval.ms=300000


Producer扩展

  • Partitioner:根据用户设置的算法(比如根据消息的key来设计算法到底分发到哪个分区里面)来计算发送到哪个分区-Partition,默认是随机
  • 数据通信方式:同步发送和异步发送,同步是指生产者发送数据后,要等待接收方发回响应后再发送下一个数据的通讯方式;异步指发送生产者发送消息后不等接收方响应就立即发送下一条数据的方式,通信方式通过acks的配置来控制。
    • acks:默认为1.表示需要Leader节点回复收到消息
    • acks:all,表示需要所有的Leader节点以及所有的副本节点回复收到消息(acks=-1)
    • acks:0,不需要回复

Topic、Partition、Message扩展

  • 每个Partition在存储层面是Append Log文件,新消息都会被直接追加到log文件的尾部,每条消息在log文件中的位置称为offset(偏移量)
  • 越多的Partition可以容纳更多的Consumer,有效提升并发消费的能力
  • 业务类型增加了可以增加Topic,数据量大需要增加Partition
  • Message:offset,类型是long表示此消息在一个Partition中的起始位置,可以认为offset是Partition中的messageId,自增;MessageSize,类似为int32,表示此消息的字节大小;data,类型为bytes,表示message的具体内容

image-20240312125048098

存储策略

  • 在kafka中每个topic包含1到多个partition,每个partition存储一部分Message,每条Message包含三个属性,其中有一个是Offset
  • Offset相当于这个partition中的message的唯一ID,可以通过分段+索引的方式去找到这个message;分段就是segment文件,每个partition由多个segment文件组成;索引就是index,每个index里面都会记录每个segment文件中的第一条数据的偏移量,然后根据这个偏移量就可以去segment文件中找到对应的消息
# The maximum size of a log segment file. When this size is reached a new log segment will be created.
# 这个配置就表示每个segment文件的大小,超过这个大小就会再创建一个新的文件
log.segment.bytes=1073741824

kafka消息的存储流程:producer生产的消息会被发送到Topic的多个Partition上面,Topic收到消息之后会往partition的最后一个segment文件中添加这条消息,文件达到一定大小后会创建新的文件

image-20240312130302894

image-20240312125947693

容错机制

  • 一个Broker宕机后对集群的影响不大

    # 模拟节点宕机
    [root@hadoop01 config]# jps
    41728 NameNode
    53523 Kafka
    42246 ResourceManager
    59789 Jps
    41998 SecondaryNameNode
    52655 QuorumPeerMain
    [root@hadoop01 config]# kill 53523
    [root@hadoop01 config]# jps
    41728 NameNode
    59809 Jps
    42246 ResourceManager
    41998 SecondaryNameNode
    52655 QuorumPeerMain
    
    # 连接到kafka
    [root@hadoop01 zookeeper3.8.4]# bin/zkCli.sh
    [zk: localhost:2181(CONNECTED) 0] ls /
    [admin, brokers, cluster, config, consumers, controller, controller_epoch, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zookeeper]
    [zk: localhost:2181(CONNECTED) 1] ls /brokers 
    [ids, seqid, topics]
    [zk: localhost:2181(CONNECTED) 2] ls /brokers/ids 
    [1, 2]
    [zk: localhost:2181(CONNECTED) 4] get /brokers/ids/1
    {"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://hadoop02:9092"],"jmx_port":-1,"host":"hadoop02","timestamp":"1710206078306","port":9092,"version":4}
    [zk: localhost:2181(CONNECTED) 5] 
    
    
    
    # zookeeper会重新选举leader
    [root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --describe --zookeeper hadoop01:2181 --topic hello
    Topic: hello	PartitionCount: 5	ReplicationFactor: 2	Configs: 
    	Topic: hello	Partition: 0	Leader: 2	Replicas: 0,2	Isr: 2
    	Topic: hello	Partition: 1	Leader: 1	Replicas: 1,0	Isr: 1
    	Topic: hello	Partition: 2	Leader: 2	Replicas: 2,1	Isr: 2,1
    	Topic: hello	Partition: 3	Leader: 1	Replicas: 0,1	Isr: 1
    	Topic: hello	Partition: 4	Leader: 1	Replicas: 1,2	Isr: 1,2
    You have new mail in /var/spool/mail/root
    
    
    • 当kafka集群中新增一个Broker节点,zookeeper会自动识别并在适当的时机选择此节点提供Leader服务
    # 重新启动
    [root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-server-start.sh -daemon config/server.properties 
    You have new mail in /var/spool/mail/root
    [root@hadoop01 kafka_2.12-2.4.0]# jps
    41728 NameNode
    60640 Kafka
    60707 Jps
    42246 ResourceManager
    41998 SecondaryNameNode
    52655 QuorumPeerMain
    
    # 进入zookeeper观察
    [zk: localhost:2181(CONNECTED) 2] ls /brokers/ids
    [0, 1, 2]
    [zk: localhost:2181(CONNECTED) 3] get /brokers/ids/0
    {"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://hadoop01:9092"],"jmx_port":-1,"host":"hadoop01","timestamp":"1710221534732","port":9092,"version":4}
    
    # 查询kafka topic信息
    [root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --describe --zookeeper hadoop01:2181 --topic hello
    Topic: hello	PartitionCount: 5	ReplicationFactor: 2	Configs: 
    	Topic: hello	Partition: 0	Leader: 2	Replicas: 0,2	Isr: 2,0
    	Topic: hello	Partition: 1	Leader: 1	Replicas: 1,0	Isr: 1,0
    	Topic: hello	Partition: 2	Leader: 2	Replicas: 2,1	Isr: 2,1
    	Topic: hello	Partition: 3	Leader: 1	Replicas: 0,1	Isr: 1,0
    	Topic: hello	Partition: 4	Leader: 1	Replicas: 1,2	Isr: 1,2
    You have new mail in /var/spool/mail/root
    
    
  • 新启动的几点不会是任何分区的leader,所以要重新均匀分配,其实不分配也可以,在kafka中有对应的配置

     [root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-leader-election.sh --bootstrap-server hadoop01:9092 --election-type preferred --all-topic-partitions
    You have new mail in /var/spool/mail/root
    [root@hadoop01 kafka_2.12-2.4.0]# bin/kafka-topics.sh --describe --zookeeper hadoop01:2181 --topic hello
    Topic: hello	PartitionCount: 5	ReplicationFactor: 2	Configs: 
    	Topic: hello	Partition: 0	Leader: 0	Replicas: 0,2	Isr: 2,0
    	Topic: hello	Partition: 1	Leader: 1	Replicas: 1,0	Isr: 1,0
    	Topic: hello	Partition: 2	Leader: 2	Replicas: 2,1	Isr: 2,1
    	Topic: hello	Partition: 3	Leader: 0	Replicas: 0,1	Isr: 1,0
    	Topic: hello	Partition: 4	Leader: 1	Replicas: 1,2	Isr: 1,2
    
    

在kafka中的Broker是无状态的,本身是不保存任何信息的,Broker的所有信息都放在zookeeper里面了,所以,Broker进程挂掉或者启动,对集群的影响不大!

  • 34
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
apache kafka 在数据处理中特删是日志和消息的处理上会有徆多出色的表现.首先弼然推荐的是 kafka 的宏 网 http://kafka.apache.org/。在宏网最值得参考的文章就是 kafka design: http://kafka.apache.org/design.html,要特删重规返篇文章,里面有好多理念都特删好,推荐多读几遍。 在 OSC 的翻译频道有 kafka design 全中文的翻译,翻得挺好的,推荐一下: http://www.oschina.net/translate/kafka-design。kafka 的 wiki 是徆丌错的学习文档: https://cwiki.apache.org/confluence/display/KAFKA/Index 接下来就是一系列文章,文章都是循序渐迕的方式带你了览 kafka: 关亍 kafka基本知识,分布式的基础:《分布式消息系统 Kafka 刜步》 kafka分布式搭建,quick start:《kafka 分布式环境搭建》 关亍 kafka 的实现细节,返主要就是讲 design 的部分:《细节上》、《细节下》 关亍 kafka 开収环境,scala 环境的搭建:《开収环境搭建》 数据生产者,producer 的用法:《producer 的用法》、《producer 使用注意》 数据消费者,consumer 的用法:《consumer 的用法》 迓有些零碎的,关亍通信段的源码览读:《net 包源码览读》、《broker 配置》 扩展的阅读迓有下面返些: 关亍 kafka 和 jafka 的相关博客,特删好,有徆多问题也都找他览决的,大神一般的存在: http://rockybean.github.com/@rockybean kafka 的 java 化版本 jafka:https://github.com/adyliu/jafka 淘宝的 metaQ:https://github.com/killme2008/Metamorphosis 最近在写的 inforQ,刚开始写,也纯粹是为了读下源码,丌定期更新: https://github.com/ielts0909/inforq

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉霍拉卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值