kafka简记-快速开始(一)

2018-09-27  本文已影响16人  78240024406c

new無语 转载请注明原创出处,谢谢!

快速开始

由于Kafka控制台脚本对于基于UnixWindows的平台是不同的,因此在Windows平台上使用bin\windows\而不是bin/将脚本扩展名更改为.bat

1.下载

下载 2.0.0版本并解压。

> tar -xzf kafka_2.11-2.0.0.tgz
> cd kafka_2.11-2.0.0
2.启动服务器

Kafka使用ZooKeeper,安装可以看上一篇。很简单。
Kafka内也有ZooKeeper的启动脚本,都可以使用。

> bin/zookeeper-server-start.sh config/zookeeper.properties
[2013-04-22 15:01:37,495] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...

启动Kafka服务器:

> bin/kafka-server-start.sh config/server.properties
[2013-04-22 15:01:47,028] INFO Verifying properties (kafka.utils.VerifiableProperties)
[2013-04-22 15:01:47,051] INFO Property socket.send.buffer.bytes is overridden to 1048576 (kafka.utils.VerifiableProperties)
...
3.创建topic

创建一个名为testtopic,一个分区一个副本。

> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

运行list topic命令,进行查看。

> bin/kafka-topics.sh --list --zookeeper localhost:2181
test
4.发送消息,启动生产者

Kafka附带一个命令行客户端,它将从文件或标准输入中获取输入,并将其作为消息发送到Kafka集群。默认情况下,每行将作为单独的消息发送。

运行生产者,然后在控制台中键入一些消息以发送到服务器。

> 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
6.设置集群

为每个server创建一份配置。

> 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

启动:

> bin/kafka-server-start.sh config/server-1.properties &
...
> bin/kafka-server-start.sh config/server-2.properties &
...

官方文档:http://kafka.apache.org/quickstart

上一篇 下一篇

猜你喜欢

热点阅读