大家好,又见面了,我是你们的朋友全栈君。
1.安装准备 jdk-8u162-linux-x64.tar zookeeper-3.4.12.tar.gz kafka_2.11-2.0.0 .tgz
2.准备至少三台虚拟机 192.168.196.128 192.168.196.131 192.168.196.132
3.Java环境安装 (略…自行百度)
4.搭建zookeeper集群 在每台主机下执行以下步骤:
移动至/usr/local下:
代码语言:javascript复制mv zookeeper-3.4.12.tar.gz /usr/local
解压,重命名:
代码语言:javascript复制tar -zxvf zookeeper-3.4.12.tar.gz
mv zookeeper-3.4.12 zookeeper
配置环境变量:
代码语言:javascript复制vim /etc/profile
追加以下内容:
代码语言:javascript复制#set zookeeper environment
export ZK_HOME=/usr/local/zookeeper
export PATH=$ZK_HOME/bin:$PATH
保存文件后,使修改生效:
代码语言:javascript复制source /etc/profile
打开zookeeper配置文件:
代码语言:javascript复制cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
vim /usr/local/zookeeper/zoo.cfg
修改zookeeper配置文件:
代码语言:javascript复制#修改数据文件夹路径
dataDir=/usr/local/zookeeper/data
#在文件末尾添加
server.1=192.168.196.128:2888:3888
server.2=192.168.196.131:2888:3888
server.3=192.168.196.132:2888:3888
创建数据文件夹和myid文件:
代码语言:javascript复制mkdir /usr/local/zookeeper/data
touch /usr/local/zookeeper/data/myid
分别在各个机器添加myid信息:
代码语言:javascript复制echo "1" > /usr/local/zookeeper/data/myid #kafka-1 192.168.196.128
echo "2" > /usr/local/zookeeper/data/myid #kafka-1 192.168.196.131
echo "3" > /usr/local/zookeeper/data/myid #kafka-1 192.168.196.132
分别在各个机器启动zookeeper:
代码语言:javascript复制/usr/local/zookeeper/bin/zkServer.sh start
全部启动后查看状态:
代码语言:javascript复制 /usr/local/zookeeper/bin/zkServer.sh status
显示如下则zookeeper集群搭建成功:
5.搭建kafka集群 在每台主机下执行以下步骤: 移动至/usr/local下:
代码语言:javascript复制mv kafka_2.11-2.0.0 .tgz /usr/local
解压,重命名:
代码语言:javascript复制tar -zxvf kafka_2.11-2.0.0 .tgz
mv kafka_2.11-2.0.0 kafka
配置环境变量:
代码语言:javascript复制vim /etc/profile
追加以下内容:
代码语言:javascript复制#set kafka environment
export KAFKA_HOME=/usr/local/kafka
export PATH=${KAFKA_HOME}/bin:$PATH
保存文件后,使修改生效:
代码语言:javascript复制source /etc/profile
修改server.properties配置文件:
代码语言:javascript复制vim /usr/local/kafka/config/server.properties
在kafka-1主机中:
代码语言:javascript复制#修改
broker.id=1
listeners=PLAINTEXT://192.168.196.128:9092
zookeeper.connect=192.168.196.128:2181,192.168.196.131:2181,192.168.196.132:2181
offsets.topic.replication.factor=3
在kafka-2主机中:
代码语言:javascript复制#修改
broker.id=2
listeners=PLAINTEXT://192.168.196.131:9092
zookeeper.connect=192.168.196.128:2181,192.168.196.131:2181,192.168.196.132:2181
offsets.topic.replication.factor=3
在kafka-3主机中:
代码语言:javascript复制#修改
broker.id=3
listeners=PLAINTEXT://192.168.196.132:9092
zookeeper.connect=192.168.196.128:2181,192.168.196.131:2181,192.168.196.132:2181
offsets.topic.replication.factor=3
在每台机器上分别启动kafka
代码语言:javascript复制/usr/local/kafka/bin/kafka-server-start.sh -daemon config/server.properties
(测试)在其中一台机器上创建topic:
代码语言:javascript复制/usr/local/kafka/bin/kafka-topics.sh --create --zookeeper 192.168.196.128:2181,192.168.196.131:2181,192.168.196.132:2181 --replication-factor 3 --partitions 3 --topic test-topic
(测试)查看创建的topic信息:
代码语言:javascript复制/usr/local/kafka/bin/kafka-topics.sh --describe --zookeeper 192.168.196.128:2181,192.168.196.131:2181,192.168.196.132:2181 --topic test-topic
(测试)选其中一台机器创建生产者:
代码语言:javascript复制bin/kafka-console-producer.sh --broker-list 192.168.196.128:9092 -topic test-topic
(测试)另外两台机器创建消费者:
代码语言:javascript复制bin/kafka-console-consumer.sh --bootstrap-server 192.168.196.128:9092,192.168.196.131:9092,192.168.196.132:9092 --topic test-topic2 --from-beginning
尝试发送消息,收到则部署成功!
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/134087.html原文链接:https://javaforall.cn