Kafka集群搭建
本文使用3台虚拟机构建了Kafka集群(3台虚拟机也提供zookeeper服务),详细流程如下:
一、系统环境
zookeeper服务地址:
server.1=192.168.19.10:2888:3888
server.2=192.168.19.11:2888:3888
server.3=192.168.19.12:2888:3888
二、Kafka相关概念
需理解kafka的相关概念,会有助下面的理解:
- Topics & Logs & Partitions
- Broker & Producers & Consumers
三、部署方案
cdh0..2三台机器的broker.id分别为0-2;
四、安装流程
4.1. 解压kafka_2.11-0.11.0.0.tgz压缩包,并把解压的目录重命名为kafka;
4.2. 进入kafka/config目录,修改server.properties配置文件如下;
broker.id=0
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=cdh0:2181,cdh1:2181,cdh2:2181
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
修改broker.id和zookeeper.connect两个配置,所有机器配置如下:
机器名 | broker.id | zookeeper.connect |
---|---|---|
cdh0 | 0 | cdh0:2181,cdh1:2181,cdh2:2181 |
cdh1 | 1 | cdh0:2181,cdh1:2181,cdh2:2181 |
cdh2 | 2 | cdh0:2181,cdh1:2181,cdh2:2181 |
4.3 在cdh0..1三台机器上的kafka目录下,执行“bin/kafka-server-start.sh config/server.properties &”命令启动kafka集群;
4.4 最后可以使用jps命令查看kafka进程是否启动成功!
五、使用实例
启动kafka集群成功后,再用kafka提供的命令行producer和consumer试试集群。
5.1. 创建topic(创建helloworld主题)
bin/kafka-topics.sh –zookeeper cdh0:2181,cdh1:2181,cdh2:2181 –create –replication-factor 3 –partitions 1 –topic helloworld
5.2. 查看已创建的topic
bin/kafka-topics.sh –zookeeper cdh0:2181,cdh1:2181,cdh2:2181 –list
5.3 启动producer
bin/kafka-console-producer.sh –broker-list cdh0:9092,cdh1:9092,cdh2:9092 –topic helloworld
5.4 启动consumer
bin/kafka-console-consumer.sh –bootstrap-server cdh0:9092,cdh1:9092,cdh2:9092 –from-beginning –topic helloworld