八. Kafka 生產者結構
Kafka Producer 主要有三個部分組成:主線程、Sender 線程、RecordAccumulator。
- 主線程:執行序列化、分區、攔截器處理等主要操作,並將消息緩存到 RecordAccumulator 中;
- Sender 線程:從 RecordAccumulator 中拉數據,發送到 broker;
- RecordAccumulator:緩存主線程的消息,並提供給 Sender 線程;
生產者執行流程:
- 主線程處理數據,包括處理攔截器操作、序列化、分區;
- 主線程將數據發送給 RecordAccumulator;
- RecordAccumulator 將消息 (ProducerRecord) 壓縮成 (ProducerBatch),並以分區 (TopicPartition) - 消息隊列 (Deque(ProducerBatch)) 的消息存儲主線程發來的消息;
- Sender 從 RecordAccumulator 中拉取各分區的消息,並轉換爲 Broker - Deque(ProducerBatch) 的形式(因爲對於 Sender 而言,只關心把消息發送到哪個 Broker 上);
- Sender 最後將消息轉換成 Broker - Request 的形式,推送給指定的 Broker;