消息隊列kafka基礎知識(一)

一、kafka 簡介

kafka是一種高吞吐量的分佈式發佈訂閱消息系統,它可以處理網站中的所有動作流數據。

這種動作(網頁瀏覽,搜索和其他用戶的行動)是互聯網上所有用戶行為的記錄,這也是kafka經常被使用的場景之一。

這些行為數據通常由於吞吐量的要求,通過處理日誌和日誌聚合來解決。

1.1 kafka名詞解釋

  • producer:生產者。
  • consumer:消費者。
  • topic:消息以topic為類別記錄,Kafka將消息種子(Feed)分門別類,每一類的消息稱之為一個主題(Topic)。
  • broker:以集群的方式運行,可以由一個或多個服務組成,每個服務叫做一個broker;消費者可以訂閱一個或多個主題(topic),並從Broker拉數據,從而消費這些已發佈的消息。

每個消息(也叫作record記錄,也被稱為消息)是由一個key,一個value和時間戳構成。

1.2 kafka有四個核心API介紹

  • 應用程序使用producer API發佈消息到1個或多個topic中。
  • 應用程序使用consumer API來訂閱一個或多個topic,並處理產生的消息。
  • 應用程序使用streams API充當一個流處理器,從1個或多個topic消費輸入流,併產生一個輸出流到1個或多個topic,有效地將輸入流轉換到輸出流。
  • connector API允許構建或運行可重複使用的生產者或消費者,將topic鏈接到現有的應用程序或數據系統。

二、應用場景

  • 分佈式消息傳遞
  • 網站活躍數據跟蹤
  • 日誌聚合
  • 流式數據處理
  • 數據存儲
  • 事件源
  • 大數據相關場景
  • ……

三、數據管道

消息隊列kafka基礎知識(一)

四、相關術語

4.1 Topics

Kafka maintains feeds of messages in categories called topics.

消息都歸屬於一個類別成為topic,在物理上不同Topic的消息分開存儲,邏輯上一個Topic的消息對使用者透明

消息隊列kafka基礎知識(一)

4.2 Partitions

Topics are broken up into ordered commit logs called partitions

每個Topics劃分為一個或者多個Partition,並且Partition中的每條消息都被標記了一個sequential id ,也就是offset,並且存儲的數據是可配置存儲時間的

消息隊列kafka基礎知識(一)

4.3 Message Ordering

消息只保證在同一個Partition中有序,所以,如果要保證從Topic中拿到的數據有序,則需要做到:

  • Group messages in a partition by key(producer)
  • Configure exactly one consumer instance per partition within a consumer group

kafka能保證的是:

  • Message sent by a producer to a particular topic partition will be appended in the order they are sent
  • A consumer instance sees messages in the order they are stored in the log
  • For a topic with replication factor N, kafka can tolerate up to N-1 server failures without “losing” any messages committed to the log

4.4 Log

Partition對應邏輯上的Log

4.5 Replication 副本

  • Topics can (and should) be replicated(複製)
  • The unit of replication is the partition
  • Each partition in a topic has 1 leader and 0 or more replicas
  • A replica is deemed to be “in-sync” if (如果副本被認為是“同步”的話)

(1) The replica(副本) can communicate with Zookeeper(一種分佈式協調中間件)

(2) The replica is not “too far” behind the leader(configurable)

  • The group of in-sync replicas for a partition is called the ISR(In-Sync-Replicas)
  • The Replication factor cannot be lowered

kafka不是完全同步,也不是完全異步,是一種ISR機制:

1. leader會維護一個與其基本保持同步的Replica列表,該列表稱為ISR(in-sync Replica),每個Partition都會有一個ISR,而且是由leader動態維護

2. 如果一個flower比一個leader落後太多,或者超過一定時間未發起數據複製請求,則leader將其重ISR中移除

3. 當ISR中所有Replica都向Leader發送ACK時,leader才commit

4.6 kafka durability 可靠性

Durability can be configured with the producer configuration request.required.acks

  • 0 : The producer never waits for an ack
  • 1 : The producer gets an ack after the leader replica has received the data
  • -1 : The producer gets an ack after all ISRs receive the data

Minimum available ISR can also be configured such that an error is returned if enough replicas are not available to replicate data

所以,kafka可以選擇不同的durability來換取不同的吞吐量

消息隊列kafka基礎知識(一)

通常,kafka可以通過增加更多的Broker來提升吞吐量,Broker的詳細介紹見下節4.7

一個推薦的配置:

消息隊列kafka基礎知識(一)

4.7 Broker

Kafka is run as a cluster comparised of one or more servers each of which is called broker

消息隊列kafka基礎知識(一)

4.8 Producer

Processes that publish messages to a kafka topic are called producers

生產者將消息發佈到一個kafka主題

  • 生產者發佈一個它們選擇的主題
  • kafka可以是分佈式的,通常是通過”Round-Robin”算法策略,也可以根據message中的key來進行語義分割”semantic partitioning”來分佈式載入,Brokers 通過分區來均衡載入
  • kafka支持異步發送,異步發送消息是less durable的,但是是高吞吐的
消息隊列kafka基礎知識(一)


4.9 Consumer

Processes that subscribe(訂閱) to tpics and process the feed of published messages are called consumers

消費者訂閱一個主題,並且處理已發佈的消息

  • Multiple Consumer can read from the same topic
  • Each Consumer is responsible for managing it’s own offset
  • Message stay on kafka… they are not removed after they consumed
消息隊列kafka基礎知識(一)


Consumer可以從任一地方開始消費,然後又回到最大偏移量處,Consumers又可以被劃分為Consumer Group

4.10 Consumer Group

Consumer Group是顯式分佈式,多個Consumer構成組結構,Message只能傳輸給某個Group中的某一個Consumer

  • 常用的Consumer Group模式:
  • All consumer instances in one group
  • Acts like a traditional queue with load balancing
  • All consumer instances in different groups
  • All messages are broadcast to all consumer instances
  • “Logical Subscriber” - Many consumer instances in a group
  • Consumers are added for scalability and fault tolerance,Each consumer instance reads from one or more partitions for a topic ,There cannot be more consumer instances than partitions(添加消費者是為了實現可伸縮性和容錯性。每個消費者實例從一個或多個分區讀取主題,消費者實例不能多於分區數)
消息隊列kafka基礎知識(一)

Consumer Groups 提供了topics和partitions的隔離

消息隊列kafka基礎知識(一)

並且當某個Consumer掛掉後能夠重新平衡

  • Consumer Group的應用場景
  • 點對點
  • 將所有消費者放到一個Consumer Group
  • 廣播
  • 將每個消費者單獨放到一個Consumer Group
  • 水平擴展
  • 向Consumer Group中添加消費者並進行Rebalance
  • 故障轉移
  • 當某個Consumer發生故障時,Consumer Group重新分配分區

相關文章


分享到:


相關文章: