Kafka快速部署

以下内容分享自:http://colobu.com/2014/08/06/kafka-quickstart/

========================================================

第一步: 下载代码

下载 0.8.1 版本并解压。 (当前最新的稳定版本是0.8.1.1)

     
     
1
2
     
     
> tar -xzf kafka_2. 9.2- 0.8. 1.1.tgz
> cd kafka_2. 9.2- 0.8. 1.1

第二步: 启动服务

Kafka使用Zookeeper所以你可能先要安装一个ZooKeeper.你可以使用kafka中打包好的脚本或者一个配置好的Zookeeper.

     
     
1
2
3
     
     
> 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了:

     
     
1
2
3
4
     
     
> 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)
...

第三步: 新建一个话题Topic

Topic的名字叫"test",只有一个分区和一个备份。

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

查看存在的Topic列表:

     
     
1
2
     
     
> bin/kafka-topics.sh --list --zookeeper localhost: 2181
test

除了手工创建Topic,你也可以配置你的broker当发布一个不存在的topic时自动创建topic。

第四步: 发送消息

Kafka提供了一个命令行的工具,可以从输入文件或者命令行中读取消息并发送给Kafka集群。每一行是一条消息。

     
     
1
2
3
     
     
> bin/kafka-console-producer.sh --broker-list localhost: 9092 --topic test
This is a message
This is another message

第五步: 消费消息

Kafka也提供了一个消费消息的命令行工具。

     
     
1
2
3
     
     
> bin/kafka-console-consumer.sh --zookeeper localhost: 2181 --topic test --from-beginning
This is a message
This is another message

这些命令行工具有很多的选项,你可以查看他们的文档来了解更多的功能。

第六步: 设置多个broker

目前我们运行在一个broker,不好玩。
让我们来点大的。

首先为每个broker创建一个配置文件。

     
     
1
2
     
     
> cp config/server.properties config/server- 1.properties
> cp config/server.properties config/server- 2.properties

修改文件如下:

     
     
1
2
3
4
5
6
7
8
9
     
     
config/server- 1.properties:
broker. id= 1
port= 9093
log. dir=/tmp/kafka-logs- 1
config/server- 2.properties:
broker. id= 2
port= 9094
log. dir=/tmp/kafka-logs- 2

broker.id属性别重样。为了在一台机器上启动两个broker,改了一下它们的port的。
Zookeeper还在,上面用的broker还活着。 来启动这两个broker.

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

创建一个topic试试, 奢侈一把,把备份设置为3:

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

成了。运行 "describe topics" 命令瞧瞧:

     
     
1
2
3
     
     
> 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

第一行给出了分区的汇总信息。每个分区行给出分区信息。

"leader" 节点是1.
"replicas" 信息,在节点1,2,0上,不管node死活,只是列出信息而已.
"isr" 工作中的复制节点的集合. 也就是活的节点的集合.

来看看一开始创建的节点:

     
     
1
2
3
     
     
> 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

毫无新意,想必你已经明了了。

发布个消息:

     
     
1
2
3
4
5
     
     
> bin/kafka-console-producer.sh --broker-list localhost: 9092 --topic my-replicated-topic
...
my test message 1
my test message 2
^C

消费它:

     
     
1
2
3
4
5
     
     
> bin/kafka-console-consumer.sh --zookeeper localhost: 2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C

测试一下容错. 干掉leader,也就是Broker 1:

     
     
1
2
3
     
     
> ps | grep server- 1.properties
7564 ttys002 0: 15.91 /System/Library/Frameworks/JavaVM.framework/Versions/ 1.6/Home/bin/java...
> kill - 9 7564

Leader被切换到一个follower上节, 点 1 不会被列在isr中了,因为它死了:

     
     
1
2
3
     
     
> 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: 2 Replicas: 1, 2, 0 Isr: 2, 0

但是,消息没丢啊,不信你试试:

     
     
1
2
3
4
5
     
     
> bin/kafka-console-consumer.sh --zookeeper localhost: 2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C

生产者的例子

查看这里

消费者的例子

查看这里


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值