Kafka简单使用

1. 单节点单broker的部署及使用

1.1.修改配置文件$KAFKA_HOME/config/server.properties的如下项:

broker.id=0
listeners
host.name
log.dirs
zookeeper.connect

1.2.启动Kafka
kafka-server-start.sh
提示帮助信息:

USAGE: /home/hadoop/app/kafka_2.11-0.9.0.0/bin/kafka-server-start.sh [-daemon] server.properties [--override property=value]*

1.3.根据提示信息,正常启动Kafaka服务
kafka-server-start.sh $KAFKA_HOME/config/server.properties

1.4.创建topic:zk
kafka-topics.sh --create --zookeeper hadoop:2181 --replication-factor 1 --partitions 1 --topic hello_topic

1.5.查看所有topic:zk
kafka-topics.sh --list --zookeeper hadoop:2181

1.6.发送消息:broker
kafka-console-producer.sh --broker-list hadoop:9092 --topic hello_topic

1.7.消费消息:zk
kafka-console-consumer.sh --zookeeper hadoop:2181 --topic hello_topic --from-beginning

1.8.from-beginning的使用

  • 查看所有topic的详细信息:
    kafka-topics.sh --list --zookeeper hadoop:2181
  • 查看指定topic的详细信息:
    kafka-topics.sh --describe --zookeeper hadoop:2181 --topic hello_topic

1.9.jps -m查看kafka状态,一目了然。

[fuser@hadoop bin]$ jps -m
8386 Jps -m
3237 QuorumPeerMain /home/feiyue/app/zookeeper-3.4.5-cdh5.7.0/bin/../conf/zoo.cfg
8251 ConsoleConsumer --zookeeper hadoop:2181 --topic hello-kafka-topic
8172 ConsoleProducer --broker-list hadoop:9092 --topic hello-kafka-topic
6748 Kafka /home/feiyue/app/kafka_2.11-0.9.0.0/config/server.properties

2.单节点多broker

2.1.cp三个配置文件,修改如下:

server-1.properties
    log.dirs=/home/hadoop/app/tmp/kafka-logs-1
    listeners=PLAINTEXT://:9093
    broker.id=1
server-2.properties
    log.dirs=/home/hadoop/app/tmp/kafka-logs-2
    listeners=PLAINTEXT://:9094
    broker.id=2
server-3.properties
    log.dirs=/home/hadoop/app/tmp/kafka-logs-3
    listeners=PLAINTEXT://:9095
    broker.id=3

2.2.启动各个broker:

kafka-server-start.sh -daemon $KAFKA_HOME/config/server-1.properties &
kafka-server-start.sh -daemon $KAFKA_HOME/config/server-2.properties &
kafka-server-start.sh -daemon $KAFKA_HOME/config/server-3.properties &

2.3.jps查看

$ jps -m
3237 QuorumPeerMain /home/feiyue/app/zookeeper-3.4.5-cdh5.7.0/bin/../conf/zoo.cfg
9621 Jps -m
9462 Kafka /home/feiyue/app/kafka_2.11-0.9.0.0/config/server-1.properties
9463 Kafka /home/feiyue/app/kafka_2.11-0.9.0.0/config/server-2.properties
9499 Kafka /home/feiyue/app/kafka_2.11-0.9.0.0/config/server-3.properties

2.4.创建topic:
kafka-topics.sh --create --zookeeper hadoop:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
2.5.向多个broker生产topic
kafka-console-producer.sh --broker-list hadoop:9093,hadoop:9094,hadoop:9095 --topic my-replicated-topic
2.6.消费topic
kafka-console-consumer.sh --zookeeper hadoop:2181 --topic my-replicated-topic
2.7.查看有几个topic

$ kafka-topics.sh --list --zookeeper hadoop:2181
hello-kafaka-topic - marked for deletion
hello-kafka-topic
my-replicated-topic

2.8.查看topic的详情kafka-topics.sh --describe --zookeeper hadoop:2181 --topic my-replicated-topic
2.9.控制台输出:

Topic:my-replicated-topic   PartitionCount:1    ReplicationFactor:3 Configs:
    Topic: my-replicated-topic  Partition: 0    Leader: 3   Replicas: 3,1,2 Isr: 3,1,2

3. 容错性测试

leader:3
Isr: 3,1,2   //三个broker存活

kill -9 进程号码 干掉2和3,结果:
Isr: 1存活broker 1,
发送的消息依然可以被收到。

参考:http://kafka.apache.org/quickstart

你可能感兴趣的:(Kafka简单使用)