版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yitengtongweishi/article/details/82385663
安装 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
!!!
参考链接: