您的位置:首页 > 其它

[kafka] 004_kafka_安装运行

2016-07-26 16:34 288 查看
1、下载和安装

目前kafka的稳定版本为0.10.0.0

下载地址:http://kafka.apache.org/downloads.html

下载后解压缩安装包到系统即可完成安装

> tar -xzf kafka_2.11-0.10.0.0.tgz
> cd kafka_2.11-0.10.0.0


2、运行kafka服务

kafka依赖于zookeeper服务提供调度。因此在启动kafka之前,需要启动zookeeper。

如果你本地之前没有安装过zookeeper服务,kafka已提供一个内置zookeeper服务供您使用。

> bin/zookeeper-server-start.sh config/zookeeper.properties &


启动zookeeper完成后,再来启动kafka服务:

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


如果在启动过程中,报错信息为: replication factor: 1 larger than available brokers: 0

这是因为brokers和replication不匹配造成的

解决方法:在server.properties中 配置 offsets.topic.replication.factor=1

3、创建Topic

创建一个只包含一个patition和一个replication的名为'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


查看创建的topic的详细信息:

>bin/kafka-topics.sh
--describe
--zookeeper localhost:2181
--topic test


4、使用producer向kafka中推送一个消息

Run the producer and then type a few messages into the console to send to the server.

> bin/kafka-console-producer.sh
--broker-list  localhost:9092
--topic test

This is a message
This is another message


通常这一步是由程序来执行,具体API参考官网。

5、查看推送到kafka的消息

> bin/kafka-console-consumer.sh
--zookeeper localhost:2181
--topic test
--from-beginning


番外:设置一个多broker的集群kafka服务

上述的安装和启动是基于单个broker。现在我们需要拓展下我们的集群,在一台server上安装一个包含3个broker的伪集群kafka服务。

首先,拷贝配置文件server.properties并改名:

> 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.dir=/tmp/kafka-logs-1

config/server-2.properties:
broker.id=2
listeners=PLAINTEXT://:9094
log.dir=/tmp/kafka-logs-2


最后,启动新创建的这两个kafka服务:

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


现在我们可以创建一个备份为3的topic了

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


查看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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: