一. Kafka中的相關概念的介紹
Kafka是一個scala實現的分布式消息中間件,其中涉及到的相關概念如下:
Kafka中傳遞的內容稱為message(消息),message 是通過topic(話題)進行分組的 topic 和message 的關系是一對多的關系
我們稱發布message的進程為producer ,就是說producer生成<topic->message>對然后 丟進kafka集群
相對應的稱訂閱topic處理對應message的進程為consumer
Kafka集群中的節點被稱為broker 附加個圖說明下:http://kafka.apache.org/documentation.html#introduction
二. Kafka中的關鍵參數的配置
Broker(集群總的節點)配置
broker.id : 唯一確定的一個int 類型數字 log.dirs :存儲kafka數據,默認路徑 為/tmp/kafka-logs port:comsumer連接的端口號 zookeeper.conect: zookeeper的鏈接字符串,定義的格式如下 hostname1:port1,hostname2:port2,hostname3
num.partitions : 一個topic可以被分成多個paritions 管道,每個partiions中的message有序但是多個paritions中的順序不能保證
2.Consumer 配置
group.id :string 類型 標志consumer隸屬的consumer process 組 zookeeper.connect: hostname1:port1,hostname2:port2(/chroot/path 統一數據存儲路徑) zookeeper 中存儲了kafka的comsumers和brokers(包括topic和partition)的基本信息
3.Producer配置 metadata.book.list :host1:port1,host2:port2 request.required.acks: 0.數據完成就直接提交(可能在服務器崩潰了的時候丟失數據) 1.wait until the server acknowledges the request as successful -1.no messages lost producer.type : 確定messages是否同步提交 sync serializer.class :kafka.serializer.DefaultEncoder message 的序列化類,默認編碼器處理類型都是byte[]類型
三.Kafka的簡單命令
Step 1: 啟動服務器
首先啟動zookeeper
> bin/zookeeper-server-start.sh config/zookeeper.properties (遠程啟動的時候需要在后面加上一個 & 作為后台進程,然后斷開和遠程的鏈接)
接着啟動kafka服務器
> bin/kafka-server-start.sh config/server.properties
Step 2: 創建 一個 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
Step 3: Send some messages
> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test This is a message This is another message
Step 4: 啟動一個客戶端(消費者)
> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
