shiyanlou:/opt/ $ sudo wget http://labfile.oss.aliyuncs.com/courses/859/kafka_2.10-0.10.2.1.tgz
shiyanlou:/opt/ $ sudo tar -zxvf kafka_2.10-0.10.2.1.tgz
shiyanlou:/opt/ $ sudo mv kafka_2.10-0.10.2.1 kafka
shiyanlou:config/ $ sudo cp server.properties server1-.properties
shiyanlou:config/ $ sudo cp server.properties server2-.properties
[email protected]:/opt/kafka# sudo chmod 777 -R /opt/kafka
[email protected]:/opt/kafka# cd bin/
启动zookeeper:
[email protected]:/opt/kafka/bin# ./zookeeper-server-start.sh ../config/zookeeper.properties
这是一个java进程,在新的窗口输入:
shiyanlou:Desktop/ $ jps
683 Jps
411 QuorumPeerMain
看到QuorumPeerMain说明zookeeper启动成功
修改server.properties3个配置文件
server.properties 此文件不修改,保持默认值
server-1.properties 修改 broker.id=1,log.dir=/tmp/kafka-logs-1,新增 listeners=PLAINTEXT://:9093三个参数值
server-2.properties 修改broker.id=2,log.dir=/tmp/kafka-logs-2 listeners=PLAINTEXT://:9094三个参数值
三个 Kafka配置文件(server.properties、 server-1.properties、 server-2.properties),每个配置文件对应 Kafka 集群中一个节点(称为 Broker)
shiyanlou:config cd /opt/kafka/bin
启动kafka集群:
shiyanlou:bin/ $ ./kafka-server-start.sh ../config/server.properties &
shiyanlou:bin/ $ ./kafka-server-start.sh ../config/server-1.properties &
shiyanlou:bin/ $ ./kafka-server-start.sh ../config/server-2.properties &
创建主题:
shiyanlou:bin/ $ ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic myFirstTopic
1、kafka-topics.sh:Kafka 提供的一个 shell 脚本文件(位于 bin 目录中),用于创建或查看 topic 信息。
2、--create:shell 脚本的参数,告诉 shell 脚本:要创建一个 topic。
3、--zookeeper localhost:2181:shell 脚本的参数,告诉 shell 脚本 Zookeeper 的地址,用于保存 topic 元数据信息。
4、--partitions 1:shell 脚本参数,告诉 shell 脚本:所创建的这个 topic 的 partition 个数为1
5、--replication-factor 1:shell脚本参数,告诉 shell 脚本:每个 partition 的副本数为1
6、--topic myFirstTopic:shell 脚本参数,告诉 shell 脚本:创建的 topic 的名称为 myFirstTopic。
查看主题是否新建成功
shiyanlou:bin/ $ ./kafka-topics.sh --zookeeper localhost:2181 --list [10:45:39]
myFirstTopic启动消息生产者:
./kafka-console-producer.sh --broker-list localhost:9092 --topic myFirstTopic
hello kingleoric
启动消息消费者:
shiyanlou:bin/ $ ./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myFirstTopic
hello kingleoric
创建一个包含3个 partition、每个 partition 有2个副本的 topic:mySecondTopic
shiyanlou:bin/ $ ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 3 --topic mySendTopic
Created topic "mySendTopic".
查看状态:
shiyanlou:bin/ $ ./kafka-topics.sh --zookeeper localhost:2181 --describe --topic mySendTopic
Topic:mySendTopic PartitionCount:3 ReplicationFactor:2 Configs:
Topic: mySendTopic Partition: 0 Leader: 2 Replicas: 2,0 Isr: 2,0
Topic: mySendTopic Partition: 1 Leader: 0 Replicas: 0,1 Isr: 0,1
Topic: mySendTopic Partition: 2 Leader: 1 Replicas: 1,2 Isr: 1,2
结果的第一行:topic 信息概述:Topic 名称为 mySecondTopic,partition 数量为3,副本数为2,三个 partition 的详细配置信息见下面三行。 紧接着三行信息,每一行描述了一个 partition 的信息,后面的 leader、replicas和ISR 涉及到 kafka 内核结构,这些参数保证了 kafka 集群的健壮性,保证消息不丢失,保证消息的高吞吐量。
查看 Zookeeper 上的元数据信息:
hiyanlou:bin/ $ ./zookeeper-shell.sh localhost:2181 [12:55:59] Connecting to localhost:2181 Welcome to ZooKeeper! JLine support is disabled WATCHER:: WatchedEvent state:SyncConnected type:None path:null lls / ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port
查看元信息
ls /brokers
[ids, topics, seqid]
ls /brokers/ids 查看集群
[0, 1, 2]
ls /brokers/topics 查看主题
[mySendTopic, myFirstTopic, __consumer_offsets]
如果ls命令返回[],说明这个节点是叶子节点,可以运行 get 命令获取内容
ls /brokers/topics/mySendTopic/partitions
[0, 1, 2]
ls /brokers/topics/mySendTopic/partitions/0
[state]
ls /brokers/topics/mySendTopic/partitions/0/state
[]
get /brokers/topics/mySendTopic/partitions/0/state
{"controller_epoch":1,"leader":2,"version":1,"leader_epoch":0,"isr":[2,0]}
cZxid = 0xf4
ctime = Tue Dec 12 04:51:39 UTC 2017
mZxid = 0xf4
mtime = Tue Dec 12 04:51:39 UTC 2017
pZxid = 0xf4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 74
numChildren = 0