环境:MAC OS X EI Capitan 10.11.2 + Kafka 0.9.0
1. 下载
地址:https://kafka.apache.org/downloads.html
介质:kafka_2.11-0.9.0.0.tgz
2. 安装
(1)tar zxvf kafka_2.11-0.9.0.0.tgz
(2)ln -s kafka_2.11-0.9.0.0 kafka
3. 启动一个单节点的 zookeeper
(1)cd kafka
(2)bin/zookeeper-server-start.sh config/zookeeper.properties
或 nohup bin/zookeeper-server-start.sh config/zookeeper.properties &
4. 启动一个单节点的kafka server (即 broker)
(1)cd kafka
(2)bin/kafka-server-start.sh config/server.properties
(3)创建 Topic
$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Created topic "test".
(4)查看 Topic
$ bin/kafka-topics.sh --list --zookeeper localhost:2181
test
(5)启动 Producer,并发送消息
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
This is a message
This is another message
(6)启动 Consumer,等待接收消息
$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
This is a message
This is another message
5. 启动3个kafka server,组成 broker 集群
(1)cd kafka
(2)创建并修改server-1配置文件
$ cp config/server.properties config/server-1.properties
$ vim config/server-1.properties
broker.id=1
listeners=PLAINTEXT://:9093
port=9093
log.dir=/tmp/kafka-logs-1
(3)创建并修改server-2配置文件
$ cp config/server.properties config/server-2.properties
$ vim config/server-2.properties
broker.id=2
listeners=PLAINTEXT://:9094
port=9094
log.dir=/tmp/kafka-logs-2
(4)启动 kafka server-1 和 kafka server-2
$ bin/kafka-server-start.sh config/server-1.properties &
$ bin/kafka-server-start.sh config/server-2.properties &
加上之前已经启动的 server,这样一共是3个kafka server,组成一个集群。
(5)创建一个新的 Topic,带 replication-factor 参数
$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
(6)查看 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: 2 Replicas: 2,1,0 Isr: 0,1,2
说明:
partiton: partion id,由于此处只有一个partition,因此 partition id 为 0。
leader:当前负责读写的lead broker id。
relicas:当前 partition 的所有replication broker list。
isr:relicas 的子集,只包含出于活动状态的broker。
(7)查看 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
(8)发布消息到新的 Topic
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
my test message 1
my test message 2
(9)消费消息
$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
(10)测试容错:fault-tolerance
$ ps | grep server-1.properties
$ kill -9 34650
$ 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: 2,1,0 Isr: 0,2
继续发送消息,消息接收正常。
$ ps | grep server-2.properties
$ kill -9 34653
$ 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: 0 Replicas: 2,1,0 Isr: 0
继续发送消息,消息接收正常。
参考文献:
1. http://blog.csdn.net/yfkiss/article/details/17381351
2. https://kafka.apache.org
1. 下载
地址:https://kafka.apache.org/downloads.html
介质:kafka_2.11-0.9.0.0.tgz
2. 安装
(1)tar zxvf kafka_2.11-0.9.0.0.tgz
(2)ln -s kafka_2.11-0.9.0.0 kafka
3. 启动一个单节点的 zookeeper
(1)cd kafka
(2)bin/zookeeper-server-start.sh config/zookeeper.properties
或 nohup bin/zookeeper-server-start.sh config/zookeeper.properties &
4. 启动一个单节点的kafka server (即 broker)
(1)cd kafka
(2)bin/kafka-server-start.sh config/server.properties
(3)创建 Topic
$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Created topic "test".
(4)查看 Topic
$ bin/kafka-topics.sh --list --zookeeper localhost:2181
test
(5)启动 Producer,并发送消息
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
This is a message
This is another message
(6)启动 Consumer,等待接收消息
$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
This is a message
This is another message
5. 启动3个kafka server,组成 broker 集群
(1)cd kafka
(2)创建并修改server-1配置文件
$ cp config/server.properties config/server-1.properties
$ vim config/server-1.properties
broker.id=1
listeners=PLAINTEXT://:9093
port=9093
log.dir=/tmp/kafka-logs-1
(3)创建并修改server-2配置文件
$ cp config/server.properties config/server-2.properties
$ vim config/server-2.properties
broker.id=2
listeners=PLAINTEXT://:9094
port=9094
log.dir=/tmp/kafka-logs-2
(4)启动 kafka server-1 和 kafka server-2
$ bin/kafka-server-start.sh config/server-1.properties &
$ bin/kafka-server-start.sh config/server-2.properties &
加上之前已经启动的 server,这样一共是3个kafka server,组成一个集群。
(5)创建一个新的 Topic,带 replication-factor 参数
$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
(6)查看 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: 2 Replicas: 2,1,0 Isr: 0,1,2
说明:
partiton: partion id,由于此处只有一个partition,因此 partition id 为 0。
leader:当前负责读写的lead broker id。
relicas:当前 partition 的所有replication broker list。
isr:relicas 的子集,只包含出于活动状态的broker。
(7)查看 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
(8)发布消息到新的 Topic
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
my test message 1
my test message 2
(9)消费消息
$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
(10)测试容错:fault-tolerance
$ ps | grep server-1.properties
$ kill -9 34650
$ 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: 2,1,0 Isr: 0,2
继续发送消息,消息接收正常。
$ ps | grep server-2.properties
$ kill -9 34653
$ 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: 0 Replicas: 2,1,0 Isr: 0
继续发送消息,消息接收正常。
参考文献:
1. http://blog.csdn.net/yfkiss/article/details/17381351
2. https://kafka.apache.org
没有评论:
发表评论