最近在作測試平臺的開發過程當中,須要用到消息隊列,在簡單調研後選取了主流的kafka作爲咱們平臺的消息中間件,如下是對kafka的學習筆記。html
consumer group:消費者組,每一個consumer歸屬於一個groupjava
消息在同一個consumer group下的consumer間,實行負載均衡
消息在不一樣consumer group下的consumer間,實行廣播python
offset:偏移量,每條消息的惟一標識。apache
kafka中的每一條數據都有一對key和value,存放在磁盤上。通常不會被永久保留,而是在到達必定的數量或者時間後對最先寫入的數據進行刪除。bootstrap
Log compaction確保kafka將始終保留單個topic partition的數據日誌中每一個key值的最後一個已知值。它給出在應用程序崩潰或系統故障後恢復狀態,或者在操做維護期間從新啓動應用程序後從新加載緩存這些場景的解決方法。
api
一個消息能夠被多個consumer group消費,但只能被同一個group下的一個consumer消費。好處是能夠同時提供離線和實時處理(離線和實時分屬兩個group,各自消費,互不影響)。緩存
kafka搭建: http://www.orchome.com/6服務器
kafka啓動命令:架構
bin/kafka-server-start.sh config/server.properties &
建立topic:負載均衡
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看topic:
bin/kafka-topics.sh --list --zookeeper localhost:2181
生產消息:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test This is a message This is another message
消費消息:
#從頭開始消費消息 bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning #從客戶端啓動後消費新到達的消息: bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test #指定消費者組,從該消費者組上次的偏移量開始消費 bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --consumer.config config/consumer.properties
查看topic的分區、副本信息
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic
kafka架構:
segment中,index和log文件的對應關係