kafuka學習之路(一)kafuka安裝和簡單使用

一,安裝環境與軟件版本

linux centOs6 64
jdk      jdk-8u191-linux-x64.tar.gz
zookeeper zookeeper-3.4.10.tar.gz
kafuka kafka_2.11-0.11.0.2

二,安裝

##解壓
-rwxrw-rw-.  1 root root 42136632 Jun 11 01:55 kafka_2.11-0.11.0.2.tgz
drwxr-xr-x. 12 1001 1001     4096 Jun 11 05:35 zookeeper-3.4.10
[root@localhost module]# tar -xvf kafka_2.11-0.11.0.2.tgz 
[root@localhost kafka_2.11-0.11.0.2]# ll
total 56
drwxr-xr-x. 3 root root  4096 Nov 10  2017 bin
drwxr-xr-x. 2 root root  4096 Nov 10  2017 config
drwxr-xr-x. 2 root root  4096 Jun 11 20:09 libs
-rw-r--r--. 1 root root 28824 Nov 10  2017 LICENSE
drwxr-xr-x. 2 root root  4096 Jun 11 20:10 logs
-rw-r--r--. 1 root root   336 Nov 10  2017 NOTICE
drwxr-xr-x. 2 root root  4096 Nov 10  2017 site-docs

##添加日誌文件夾
[root@localhost kafka_2.11-0.11.0.2]# mkdir logs
[root@localhost kafka_2.11-0.11.0.2]# ll

#修改配文件
[root@localhost kafka_2.11-0.11.0.2]# cd config/
[root@localhost config]# vim server.properties 
broker.id=1  #broker的全局唯一編號,不能重複(我的是跟zk的myid一樣)
delete.topic.enable=true
listeners=PLAINTEXT://192.168.8.132:9092
log.dirs=/opt/module/kafka_2.11-0.11.0.2/logs
zookeeper.connect=192.168.8.129:2181,192.168.8.132:2181,192.168.8.133:2181

三,啓動和創建分區使用

     注:zookeeper集羣啓動正常的前提下

#啓動
[root@localhost kafka_2.11-0.11.0.2]# bin/kafka-server-start.sh config/server.properties &

#關閉
[root@localhost kafka_2.11-0.11.0.2]# bin/kafka-server-stop.sh stop

##創建topic
#topic 定義topic名
#replication-factor  定義副本數
#partitions  定義分區數
[root@localhost kafka_2.11-0.11.0.2]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 3 --topic test 

bin/kafka-topics.sh  --zookeeper localhost:2181 --create --replication-factor 3 --partitions 3 --topic test 
Created topic "test".  
##查看topic 列表
[root@localhost kafka_2.11-0.11.0.2]# bin/kafka-topics.sh --zookeeper localhost:2181 --list
test

##查看詳情
[root@localhost kafka_2.11-0.11.0.2]# bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic test
Topic:test      PartitionCount:3        ReplicationFactor:3     Configs:        MarkedForDeletion:true
        Topic: test     Partition: 0    Leader: -1      Replicas: 0,1,2 Isr: 2
        Topic: test     Partition: 1    Leader: -1      Replicas: 1,2,0 Isr: 2
        Topic: test     Partition: 2    Leader: -1      Replicas: 2,0,1 Isr: 2

##刪除topic
[root@localhost kafka_2.11-0.11.0.2]# bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic test
Topic test is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
#如果集羣裏有某個kafuka沒有設置 delete.topic.enable=true ,
#則不會刪除,需要全部重新啓動後,再刪除纔可
#刪除成功後的標記
[root@localhost kafka_2.11-0.11.0.2]#  bin/kafka-topics.sh --zookeeper localhost:2181 --list
test - marked for deletion
#再zk裏刪除註冊的節點
rmr /brokers/topics/【topic name】

四,簡單使用

##發送消息(localhost 必須是本機的ip)
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic topicTest

##消費消息(localhost 必須是本機的ip)
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic topicTest

#生產
[root@localhost kafka_2.11-0.11.0.2]# bin/kafka-console-producer.sh --broker-list 192.168.8.129:9092 --topic test1
>123
>123
>123
>123
>123
>123
>123
>hool^H^H
>holl
>hello
>hello
>

#消費1
[root@localhost kafka_2.11-0.11.0.2]#  bin/kafka-console-consumer.sh --zookeeper 192.168.8.132:2181 --from-beginning --topic test1
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].
123
123
123
123
123
123
123
hool
holl
hello
hello

#消費2,中途推出後在進來,前期的消息會亂序
[root@localhost kafka_2.11-0.11.0.2]# bin/kafka-console-consumer.sh --zookeeper 192.168.8.133:2181 --from-beginning --topic test1
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].
123
123
123
hello
123
123
hool
123
123
holl
hello

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章