本套系列博客從真實商業環境抽取案例進行總結和分享,並給出Spark商業應用實戰指導,請持續關注本套博客。版權聲明:本套Spark商業應用實戰歸做者(秦凱新)全部,禁止轉載,歡迎學習。react
Apache Kafka® is a distributed streaming platform. What exactly does that mean?
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.
Kafka is generally used for two broad classes of applications:
- Building real-time streaming data pipelines that reliably get data between systems or applications
- Building real-time streaming applications that transform or react to the streams of data
To understand how Kafka does these things, let's dive in and explore Kafka's capabilities from the bottom up.
First a few concepts:
- 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.
複製代碼
Any message queue that allows publishing messages decoupled from consuming them
is effectively acting as a storage system for the in-flight messages. What is
different about Kafka is that it is a very good storage system.
- Data written to Kafka is written to disk and replicated for fault-tolerance.
Kafka allows producers to wait on acknowledgement so that a write isn't considered
complete until it is fully replicated and guaranteed to persist even if the server
written to fails.
- The disk structures Kafka uses scale well,Kafka will perform the same whether you
have 50 KB or 50 TB of persistent data on the server.
- As a result of taking storage seriously and allowing the clients to control
their read position, you can think of Kafka as a kind of special purpose
distributed filesystem dedicated to high-performance, low-latency commit
log storage, replication, and propagation.
複製代碼
在Linux kernel2.2 以後出現了一種叫作"零拷貝(zero-copy)"系統調用機制,就是跳過「用戶緩衝區」的拷貝,創建一個磁盤空間和內存的直接映射,數據再也不復制到「用戶態緩衝區」後端
惟一的整數來標識每一個broker,不能與其餘broker衝突,建議從0開始。緩存
確保該目錄有比較大的硬盤空間。若是須要指定多個目錄,以逗號分隔便可,好比/xin/kafka1,/xin/kafka2。這樣作的好處是Kafka會力求均勻地在多個目錄下存放分區(partition)數據。若是掛載多塊磁盤,那麼會有多個磁頭同時執行寫操做。對吞吐量具備很是強的提高。安全
該參數則徹底沒有默認值,必需要配置。這個參數也能夠是一個逗號分隔值的列表,好比zk1:2181,zk2:2181,zk3:2181/kafka。注意結尾的/kafka,它是zookeeper的chroot,是可選的配置,若是不指定的話就默認使用zookeeper的根路徑。網絡
協議配置包括PLAINTEXT,SSL, SASL_SSL等,格式是[協議]://[主機名]:[端口],[[協議]://[主機名]:[端口]],該參數是Brocker端開發給clients的監聽端口。建議配置:app
PLAINTEXT://hostname:port(未啓用安全認證)
SSL://hostname:port(啓用安全認證)
複製代碼
解決ISR全部副本爲空,leader又出現宕機的狀況。此時leader該如何選擇呢?截止kafka 1.0.0版本,該參數默認爲false,表示不容許選擇非ISR副本集以外的broker。由於高可用性與數據的完整性,kafka官方選擇了後者。socket
很少說,是否容許刪除topic,鑑於0.9.0.0新增了ACL機制權限機制,誤操做基本是不存在的。ide
優先選取ms的配置,minutes次之,hours最後,默認留存機制是7天。如何判斷:post
新版本:基於消息中的時間戳來進行判斷。 老版本:根據日誌文件的最新修改時間進行比較.性能
Kafka會按期刪除那些大小超過該參數值的日誌文件。默認值是-1,表示Kafka永遠不會根據大小來刪除日誌
持久化級別,用於最少須要多少副本同步。在acks=all(或-1) 時纔有意義。min.insync.replicas指定了必需要應答寫請求的最小數量的副本數。若是不能知足,producer將會拋出NotEnoughReplicas或NotEnoughReplicasAfterAppend異常。該參數用於實現更好的消息持久性。
舉例以下:
5臺broker ack =-1 min.insync.replicas = 3
上述表示最少須要3個副本同步後,Broker纔可以對外提供服務,不然將會拋出異常。若3臺Broker宕機,即便剩餘2臺所有同步結束,知足了 ack =-1也要報錯。
默認值爲3,主要負責轉發來自broker和clients發送過來的各類請求。強調這裏只是轉發,真實環境下,須要監聽 NetWorkerProcessorAvgIdlePercent JMX指標,若指標低於0.3,則建議調高該值。
默認是8,也即broker後端有8個線程以輪詢的方式不停的監聽轉發過來的網絡請求並進行實時處理。
broker可以接收的最大消息大小,默認是977KB。所以注意,生產環境應該調高該值。
默認是5秒,間隔實在過短,適當增長該值能夠很高的提行OS物理寫入操做的性能。LinkedIn設置爲2分鐘來提高吞吐量。
官方測試XFS文件系統寫入時間爲160秒,Ext4大約是250毫秒。建議生產環境最好使用XFS文件系統。
OS系統最大打開的文件描述符是有上限的,舉例:一個kafka集羣主要有3個副本,50個分區,若每個分區文件大小爲10G,而分區內日誌段大小爲1GB,則一個Broker須要維護1500個左右的文件描述符。所以根據須要設置:
ulimit -n 100000
複製代碼
本機立足於Broker進行參數詳細講解,有問題,歡迎留言。
秦凱新 於深圳 2018