前言:公司最近要做个人画像及推荐相关业务,
官网快速入门教程:http://kafka.apache.org/quickstart
快速入门
注:假设你电脑上没有安装Kafka和Zookeeper
1.下载2.1.0 release版本
http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.1.0/kafka_2.11-2.1.0.tgz
下载完成后并解压:
tar -xzf kafka_2.11-2.1.0.tgz
cd kafka_2.11-2.1.0
2.启动服务
kafka使用了zookeeper服务,同时kafka也自带了zookeeper,我们启动就行了。
bin/zookeeper-server-start.sh config/zookeeper.properties
现在启动kafka服务
bin/kafka-server-start.sh config/server.properties
3.创建一个topic
这里创建一个test的topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
运行下面的命令看下topic列表
bin/kafka-topics.sh --list --zookeeper localhost:2181
test
4.发送一个消息
这里我们往topic发送2条消息
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
打印信息如下:
This is a message
This is another message
5.启动消费者
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
打印信息如下:
This is a message
This is another message
到此,但节点kafka安装完毕。下面我们讨论下多节点kafka怎么处理
6.启动多个broker集群
cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties
现在修改新文件如下内容:
config/server-1.properties:
broker.id=1
listeners=PLAINTEXT://:9093
log.dirs=/tmp/kafka-logs-1
config/server-2.properties:
broker.id=2
listeners=PLAINTEXT://:9094
log.dirs=/tmp/kafka-logs-2
注:broker.id是唯一的。
我们启动新增的2个节点:
bin/kafka-server-start.sh config/server-1.properties &
…
bin/kafka-server-start.sh config/server-2.properties &
…
为集群创建一个新的topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
我们来看一下topic为my-replicated-topic的描述信息
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
打印信息如下:
Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs:
Topic: my-replicated-topic Partition: 0 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0
再看一下topic为test的描述信息
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
打印信息如下:
Topic:test PartitionCount:1 ReplicationFactor:1 Configs:
Topic: test Partition: 0 Leader: 0 Replicas: 0 Isr: 0
往新增的topic上发送消息
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
打印信息如下:
…
my test message 1
my test message 2
消费者消费信息
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic
打印信息如下:
…
my test message 1
my test message 2
完,上面都是参考官网快速入门文档。