歡迎光臨
每天分享高質量文章

Kafka 是什麼?

點選上方“芋道原始碼”,選擇“置頂公眾號”

技術文章第一時間送達!

原始碼精品專欄

 

來源:https://www.jianshu.com/p/8d7f30f87f95


  • 定義

  • 架構

  • topic

  • durable

  • consumer

  • consumer group


定義

一千個人眼裡有一千個哈姆雷特。如果說誰最有資格定義kafka是什麼,那麼肯定是官方檔案:

Apache Kafka® is a distributed streaming platform.

官方還對流平臺進行了定義–流平臺有三大關鍵能力(A streaming platform has three key capabilities):

  • Publish and subscribe to streams of records, similar to a message queue or enterprise messaging system.

  • Store streams of records in a fault-tolerant durable way.

  • Process streams of records as they occur.

第一個特性是類MQ的釋出訂閱特性,第二個特性就是具備容錯的儲存能力,第三個特性是處理資料。所以kafka可以替代ActiveMQ這類訊息中介軟體。另外我們看一下官方對kafka的定位,如下圖所示:

kafka定位

kafka幾個重要的概念:

  • Kafka is run as a cluster on one or more servers that can span multiple datacenters.

  • The Kafka cluster stores streams of records in categories called topics.

  • Each record consists of a key, a value, and a timestamp.

架構

kafka架構如下圖所示,訊息中介軟體的本質就是:生產-儲存-消費。由下圖可知,在kafka的架構設計裡,無論是生產者,還是消費者,還是訊息儲存,都可以水平擴容從而提高整個叢集的處理能力,生來就是分散式系統。另外,圖中沒有展示出來的kafka另一個很重要的特性,那就是副本,在建立topic的時候指定分割槽數量的同時,還可以指定副本的數量(副本最大數量不允許超過broker的數量,否則會報錯:Replication factor: 2 larger than available brokers: 1)。各個副本之間只有一個leader,其他是follow,只有leader副本提供讀寫服務,follow副本只是冷備,當leader掛掉會從follow中選舉一個leader,從而達到高可用。

kafka architecture

圖片來源於https://en.wikipedia.org/wiki/File:Overview_of_Apache_Kafka.svg

topic

下圖是topic的解剖圖,kafka只有topic的概念,沒有類似ActiveMQ中的Queue(一對一)的概念(ActiveMQ既有Topic又有Queue)。一個topic可以有若干個分割槽,且分割槽可以動態修改,但是隻允許增加不允許減少。每個分割槽中的訊息是有序的。各個分割槽之間的訊息是無序的。新訊息採用追加的方式寫入,這種順序寫入方式,從而使kafka的吞吐能力非常強大(一些驗證表名順序寫入磁碟的速度超過隨機寫入記憶體)。

kafka topic
  • topic定義
    官方定義:A topic is a category or feed name to which records are published. Topics in Kafka are always multi-subscriber; that is, a topic can have zero, one, or many consumers that subscribe to the data written to it.
    例如訂單支付成功後,傳送名為TOPIC_PAYMENT_ORDER_SUCCESS,積分系統可以接收這個topic,給使用者送積分。會員系統可以接收這個topic,增加會員成長值。支付寶裡的螞蟻莊園還有支付成功後送飼料等。

  • 磁碟&記憶體速度對比
    由下圖可知,順序寫入磁碟的速度(Sequential, disk)為53.2M,而隨機寫入記憶體的速度(Random, memory)為36.7M。

    磁碟&記憶體速度對比

圖片來源於網路:http://searene.me/2017/07/09/Why-is-Kafka-so-fast/

durable

kafka對訊息日誌的儲存策略為:The Kafka cluster durably persists all published records—whether or not they have been consumed—using a configurable retention period. For example, if the retention policy is set to two days, then for the two days after a record is published, it is available for consumption, after which it will be discarded to free up space. Kafka’s performance is effectively constant with respect to data size so storing data for a long time is not a problem.
即無論如何,kafka會持久化儲存所有訊息,無論它們是否已經被消費。而kafka訊息日誌保留策略透過配置決定(以log.retention開頭的一些配置,例如log.retention.mslog.retention.minuteslog.retention.hourslog.retention.bytes),例如配置有效期兩天,那麼兩天內這些訊息日誌都能透過offset訪問。到期後,kafka會刪除這些訊息日誌檔案釋放磁碟空間。

consumer

kafka消費topic中某個分割槽示意圖如下,至於kafka如何在各個topic的各個分割槽中選擇某個分割槽,後面的文章會提到。由下圖可知,消費者透過offset定位並讀取訊息,且各個消費者持有的offset是自己的消費進度。

kafka consumer

consumer group

  • each record published to a topic is delivered to one consumer instance within each subscribing consumer group. Consumer instances can be in separate processes or on separate machines.

  • If all the consumer instances have the same consumer group, then the records will effectively be load balanced over the consumer instances.

  • If all the consumer instances have different consumer groups, then each record will be broadcast to all the consumer processes.

即對於訂閱了某個topic的consumer group下的所有consumer,任意一條訊息只會被其中一個consumer消費。如果有多個consumer group,各個consumer group之間互不幹擾。consumer group示意圖如下所示,某個topic訊息有4個分割槽:P0, P1, P2, P3。Consumer Group A中有兩個consumer:C1和C2。Consumer Group B中有4個consumer:C3,C4,C5和C6。如果現在生產者發送了一條訊息,那麼這條訊息只會被Consumer Group A中的C1和C2之中某個消費者消費到,以及被Consumer Group B中的C3,C4,C5和C6之中某個消費者消費到。

consumer group



如果你對 Dubbo / Netty 等等原始碼與原理感興趣,歡迎加入我的知識星球一起交流。長按下方二維碼噢

目前在知識星球更新了《Dubbo 原始碼解析》目錄如下:

01. 除錯環境搭建
02. 專案結構一覽
03. 配置 Configuration
04. 核心流程一覽

05. 拓展機制 SPI

06. 執行緒池

07. 服務暴露 Export

08. 服務取用 Refer

09. 註冊中心 Registry

10. 動態編譯 Compile

11. 動態代理 Proxy

12. 服務呼叫 Invoke

13. 呼叫特性 

14. 過濾器 Filter

15. NIO 伺服器

16. P2P 伺服器

17. HTTP 伺服器

18. 序列化 Serialization

19. 叢集容錯 Cluster

20. 優雅停機

21. 日誌適配

22. 狀態檢查

23. 監控中心 Monitor

24. 管理中心 Admin

25. 運維命令 QOS

26. 鏈路追蹤 Tracing

… 一共 69+ 篇

目前在知識星球更新了《Netty 原始碼解析》目錄如下:

01. 除錯環境搭建
02. NIO 基礎
03. Netty 簡介
04. 啟動 Bootstrap

05. 事件輪詢 EventLoop

06. 通道管道 ChannelPipeline

07. 通道 Channel

08. 位元組緩衝區 ByteBuf

09. 通道處理器 ChannelHandler

10. 編解碼 Codec

11. 工具類 Util

… 一共 61+ 篇

目前在知識星球更新了《資料庫物體設計》目錄如下:


01. 商品模組
02. 交易模組
03. 營銷模組
04. 公用模組

… 一共 17+ 篇


目前在知識星球更新了《Spring 原始碼解析》目錄如下:


01. 除錯環境搭建
02. IoC Resource 定位
03. IoC BeanDefinition 載入

04. IoC BeanDefinition 註冊

05. IoC Bean 獲取

06. IoC Bean 生命週期

… 一共 35+ 篇

贊(0)

分享創造快樂