安裝 Java
https://blog.csdn.net/yitengtongweishi/article/details/81946349
安裝 Zookeeper
https://blog.csdn.net/yitengtongweishi/article/category/7952362
下載 Kafka
http://kafka.apache.org/downloads,
去官網下載相應的版本,筆者選擇的是 kafka_2.11-1.1.1.tgz。
校驗該壓縮文件
blockchain@Dao:~/Downloads$ sha512sum kafka_2.11-1.1.1.tgz
2bf14a07c569c84736271471a9abb7b937b311780ed2a3d969ac0123737319e9151e0a69d6b8bd309a57b92cb00a90400e8e19e0512a6ee9206b2c91826af930 kafka_2.11-1.1.1.tgz
blockchain@Dao:~/Downloads$
和 https://www.apache.org/dist/kafka/1.1.1/kafka_2.11-1.1.1.tgz.sha512 保持一致,則校驗通過。
解壓縮
blockchain@Dao:~/Downloads$ tar -zxvf kafka_2.11-1.1.1.tgz -C ~
切換至 kafka的 安裝目錄
blockchain@Dao:~$ cd kafka_2.11-1.1.1/ ; ls -lt
total 52
drwxr-xr-x 2 blockchain blockchain 4096 9月 7 14:58 config
drwxr-xr-x 2 blockchain blockchain 4096 9月 7 14:43 libs
drwxr-xr-x 2 blockchain blockchain 4096 7月 7 12:15 site-docs
drwxr-xr-x 3 blockchain blockchain 4096 7月 7 12:15 bin
-rw-r--r-- 1 blockchain blockchain 28824 7月 7 12:12 LICENSE
-rw-r--r-- 1 blockchain blockchain 336 7月 7 12:12 NOTICE
blockchain@Dao:~/kafka_2.11-1.1.1$
blockchain@Dao:~/kafka_2.11-1.1.1$ ls -lt config/
total 64
-rw-r--r-- 1 blockchain blockchain 6894 9月 7 14:58 server.properties
-rw-r--r-- 1 blockchain blockchain 906 7月 7 12:12 connect-console-sink.properties
-rw-r--r-- 1 blockchain blockchain 909 7月 7 12:12 connect-console-source.properties
-rw-r--r-- 1 blockchain blockchain 5807 7月 7 12:12 connect-distributed.properties
-rw-r--r-- 1 blockchain blockchain 883 7月 7 12:12 connect-file-sink.properties
-rw-r--r-- 1 blockchain blockchain 881 7月 7 12:12 connect-file-source.properties
-rw-r--r-- 1 blockchain blockchain 1111 7月 7 12:12 connect-log4j.properties
-rw-r--r-- 1 blockchain blockchain 2730 7月 7 12:12 connect-standalone.properties
-rw-r--r-- 1 blockchain blockchain 1221 7月 7 12:12 consumer.properties
-rw-r--r-- 1 blockchain blockchain 4727 7月 7 12:12 log4j.properties
-rw-r--r-- 1 blockchain blockchain 1919 7月 7 12:12 producer.properties
-rw-r--r-- 1 blockchain blockchain 1032 7月 7 12:12 tools-log4j.properties
-rw-r--r-- 1 blockchain blockchain 1023 7月 7 12:12 zookeeper.properties
修改 config 目錄下的 server.properties,修改後的部分如下,其餘均不變。
############################# Log Basics #############################
# A comma separated list of directories under which to store log files
#log.dirs=/tmp/kafka-logs
log.dirs=/home/blockchain/tmp/kafka-logs
啓動 Kafka
- 啓動 Zookeeper
blockchain@Dao:~$
blockchain@Dao:~$ zookeeper-3.4.13/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/blockchain/zookeeper-3.4.13/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
blockchain@Dao:~$
blockchain@Dao:~$ jps
10098 Jps
10070 QuorumPeerMain
blockchain@Dao:~$
- 後臺啓動 Kafka server
blockchain@Dao:~/kafka_2.11-1.1.1$
blockchain@Dao:~/kafka_2.11-1.1.1$ bin/kafka-server-start.sh config/server.properties &
blockchain@Dao:~$ jps
10161 Kafka
10070 QuorumPeerMain
10510 Jps
blockchain@Dao:~$
安裝成功。
簡單測試
- 創建主題
blockchain@Dao:~/kafka_2.11-1.1.1$
blockchain@Dao:~/kafka_2.11-1.1.1$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Created topic "test".
blockchain@Dao:~/kafka_2.11-1.1.1$
replication-factor : 複製數目,提供failover機制;1代表只在一個broker上有數據記錄,一般值都大於1,代表一份數據會自動同步到其他的多個broker,防止某個broker宕機後數據丟失。
partitions : 一個topic可以被切分成多個partitions,一個消費者可以消費多個partitions,但一個partitions只能被一個消費者消費,所以增加partitions可以增加消費者的吞吐量。kafka只保證一個partitions內的消息是有序的,多個partitions之間的數據是無序的。
- 查看創建的主題
blockchain@Dao:~/kafka_2.11-1.1.1$
blockchain@Dao:~/kafka_2.11-1.1.1$ bin/kafka-topics.sh --list --zookeeper localhost:2181
test
blockchain@Dao:~/kafka_2.11-1.1.1$
- 啓動生產者 發送message
啓動後,在命令行下每輸入一些字符串按下回車時,就作爲一個消息 發送給 kafka
blockchain@Dao:~/kafka_2.11-1.1.1$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
>
>Hello
>World
>Kafka
>Spark
>Over
>!!!
- 啓動消費者 消費message
啓動消費者時,建議另開一個ssh窗口,這樣可以一邊通過生產者命令行輸入消息,一邊觀察消費者消費的數據
blockchain@Dao:~/kafka_2.11-1.1.1$
blockchain@Dao:~/kafka_2.11-1.1.1$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
Hello
World
Kafka
Spark
Over
!!!
參考鏈接: