Kafka主要構成:
html
一、Broker:集羣中的每個對應的服務器被稱之爲broker。java
二、Topic 用於區別消息的類別,每一個topic中包含多個Partition;Partition主要能夠減小每一個日誌的數據量,也能夠增長請求的併發量。shell
三、Producer 消息的生產者,可經過配置partitioner.class設置消息發送到那個Partition上。apache
四、Consumer 消息的消費者,能夠訂閱須要的topics。
bootstrap
其中更多的其餘術語能夠參考kafka官方文檔。服務器
Kafka配置:經過官網下載並安裝,經過文件config下的相關properties進行配置;併發
server.properties啓動服務時的相關配置,可拷貝多個配置多個broker服務(單臺服務器上配置多個broker服務的方式),如server-1.propertiesapp
console-xxxx.properties啓動相關控制檯的配置。dom
一、server.properties配置文件,啓動命令async
kafka:bin/kafka-server-start.sh config/server.properties
# broker_id,集羣環境下不能重複 broker.id=0 # broker 開放的端口 port=9092 # broker 綁定的主機名 host.name=localhost # producers、consumers鏈接的地址. 若沒配置,則使用host.name # 可是在編碼過程當中發現如不配置這項的話,系統沒法啓動,比較費解 advertised.host.name=192.168.52.128 # 同advertised.host.name相似 #advertised.port=<port accessible by clients> #存放消息文件的目錄 log.dirs=/tmp/kafka-logs #topic默認的分區數量,數量多少能夠影響併發量 num.partitions=1 ##zookeeper的配置 zookeeper.connect=192.168.52.128:2181 # Timeout in ms for connecting to zookeeper zookeeper.connection.timeout.ms=6000
其它配置屬性可參考server內部自行配置。
二、producer.properties配置文件
# list of brokers used for bootstrapping knowledge about the rest of the cluster # format: host1:port1,host2:port2 ... # 配置broker節點數據信息,用於獲取元數據信息 metadata.broker.list=192.168.52.128:9092 # name of the partitioner class for partitioning events; default partition spreads data randomly # 消息發送到那個分區的配置,默認使用org.apache.kafka.clients.producer.internals.DefaultPartitioner #partitioner.class= # specifies whether the messages are sent asynchronously (async) or synchronously (sync) producer.type=sync # specify the compression codec for all data generated: none, gzip, snappy, lz4. # the old config values work as well: 0, 1, 2, 3 for none, gzip, snappy, lz4, respectively compression.codec=none # message encoder serializer.class=kafka.serializer.DefaultEncoder # allow topic level compression # 壓縮方式 #compressed.topics=
三、consumer.properties配置文件v
# Zookeeper connection string # comma separated host:port pairs, each corresponding to a zk # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" # 監控的zookeeper連接,消費者主要經過zookeeper獲取數據 zookeeper.connect=192.168.52.128:2181 # timeout in ms for connecting to zookeeper zookeeper.connection.timeout.ms=6000 #consumer group id #消費者分組信息,每一個消息只能被分組中的某個消費者消費 group.id=test-consumer-group #consumer timeout #consumer.timeout.ms=5000
四、connect-xxxx.properties配置文件主要對kafka 0.9的新版特性的相關配置,connect能夠對大數據量的源數據拷貝到目標系統中。
connect-standalone.properties和connect-distributed.properties兩個爲單機和分佈式下的connect啓動配置文件,配置詳細信息可見具體文件。其啓動命令:
bin/connect-distributed.sh config/connect-distributed.properties
connect-file-source.properties配置源系統的connector:
#connector惟一的名稱 name=local-console-source #connector的實現 connector.class=org.apache.kafka.connect.file.FileStreamSourceConnector #connector須要建立的最大任務數 tasks.max=1 #須要處理的topic類別 topic=connect-test
connect-file-sink.properties配置目標系統的connector(和以上配置相似):
name=local-file-sink connector.class=org.apache.kafka.connect.file.FileStreamSinkConnector tasks.max=1 file=test.sink.txt #接收topic類別列表 topics=connect-test
kafka相關命令
一、topic相關命令
#建立topic bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic mykafka #查看topic topic:bin/kafka-topics.sh --list --zookeeper localhost:2181
二、producer發送消息
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka
三、customer消費消息
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic mykafka --from-beginning
以上是對kafka簡單的配置及命令操做。
Kafka的優缺點:
kafka相較於其餘mq而言,消費者採用主動poll的模式,poll模式有利於customer根據自身處理能力獲取數據。
kafka的容錯性,kafka提供對partitioner的備份到其餘broker服務器上;若有服務器掛掉,則zookeeper會從新選擇leader處理消息信息。
配置多個partitioner可提升併發,但不能保證消息的順序性;若要保證消息的順序,則只需配置一個分區便可。
不支持消息的事務性,消息發送包括三種狀況(可配置重試次數):
一、最多隻發送一次,消息有可能會丟失,但不會再重發。
二、至少發送一次,消息不會丟失,但有可能會出現重複發送。
三、保證消息會且僅會發送一次。
更多的信息能夠查看http://www.infoq.com/cn/articles/kafka-analysis-part-1或者