MrZhangAdd 2020-01-13
(下面操作,都已配置环境变量)
先启动zookeeper
$ zkServer.sh status # 查看zookeeper状态 JMX enabled by default Using config: /home/whr/workbench/zookeeper/bin/../conf/zoo.cfg Mode: follower
启动kafka
# 后台启动,需要配置参数 # 不加-daemon,会是一个阻塞进程,可以方便看日志 $ kafka-server-start.sh -daemon config/server.properties
关闭kafka
$ kafka-server-stop.sh config/server.properties
topic的分区会被创建在当前主机,每个分区的副本会分配到集群的其他机器;
比如下面例子:
# -create创建 # --zookeeper master:2181;信息写入zookeeper(新版:--bootstrap-server) # --partitions 2;partition数量 # --replication-factor 2;副本数,根据集群数量来定,不能超过集群数 # --topic first;topic的名字 $ kafka-topics.sh --create --zookeeper master:2181 --partitions 2 --replication-factor 2 --topic first Created topic "first".
# 查看所有topic $ kafka-topics.sh --list --zookeeper master:2181 first # 查看某一个topic详细信息 $ kafka-topics.sh --zookeeper master:2181 --describe --topic first Topic:first PartitionCount:2 ReplicationFactor:2 Configs: Topic: first Partition: 0 Leader: 2 Replicas: 2,1 Isr: 2,1 Topic: first Partition: 1 Leader: 0 Replicas: 0,2 Isr: 0,2
$ kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test test:0:16 test:1:18
并不能真正删除,会被标记为删除
$ kafka-topics.sh --delete --zookeeper master:2181 --topic first Topic first is marked for deletion.
若想真正删除:
# 启动zookeeper客户端 $ zkCli.sh # 找到目录 ls /brokers/topics # 删除 rmr /brokers/topics/first
创建producer
$ kafka-console-producer.sh --broker-list master:9092 --topic first >hello # 发送消息
创建consumer
连接的是zookeeper
$ kafka-console-consumer.sh --bootstrap-server master:9092 --topic first --from-beginning hello # 收到消息