免费资源网 – https://freexyz.cn/
目录一、Kafka集群的搭建1.拉取相关镜像2.运行zookeeper3.运行kafka4.设置topic5.进行生产者和消费者测试
一、Kafka集群的搭建
1.拉取相关镜像
docker pull wurstmeister/kafka docker pull zookeeper2.运行zookeeper
docker run -d –name zookeeper -p 2181:2181 -t zookeeper3.运行kafka
Kafka0:
docker run -d –name kafka0 -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=192.168.16.129:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.16.129:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafkaKafka1:
docker run -d –name kafka1 -p 9093:9093 -e KAFKA_BROKER_ID=1 -e KAFKA_ZOOKEEPER_CONNECT=192.168.16.129:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.16.129:9093 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 -t wurstmeister/kafkaKafka2:
docker run -d –name kafka2 -p 9094:9094 -e KAFKA_BROKER_ID=2 -e KAFKA_ZOOKEEPER_CONNECT=192.168.16.129:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.16.129:9094 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 -t wurstmeister/kafka参数说明:
-e KAFKA_BROKER_ID=0 在kafka集群中,每个kafka都有一个BROKER_ID来区分自己-e KAFKA_ZOOKEEPER_CONNECT=10.20.8.50:2181/kafka 配置zookeeper管理kafka的路径10.20.8.50:2181/kafka-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.20.8.50:9092 把kafka的地址端口注册给zookeeper,如果是远程访问要改成外网IP。-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 配置kafka的监听端口:这个不能改-v /etc/localtime:/etc/localtime 容器时间同步虚拟机的时间启动3个Kafka节点
4.设置topic
进入kafka0
docker exec -it kafka0 /bin/bash进入bin目录
cd /opt/kafka_2.13-2.8.1/bin创建topic
kafka-topics.sh –create –zookeeper 192.168.16.129:2181 –replication-factor 3 –partitions 5 –topic TestTopic查看topic
kafka-topics.sh –describe –zookeeper 192.168.16.129:2181 –topic TestTopicKafka的topic所有分区会分散在不同Broker上,所以该topic的5个分区会被分散到3个Broker上,其中有两个Broker得到两个分区,另一个Broker只有1个分区,如图所示:
集群节点说明:
Topic: TestTopic PartitionCount: 5 ReplicationFactor:3代表TestTopic有5个分区,3个副本节点;Topic: 代表主题名称Leader代表主题节点号,Replicas代表他的副本节点有Broker.id = 2、0、1(包括Leader Replica和Follower Replica,且不管是否存活),Isr表示存活并且同步Leader节点的副本有Broker.id = 2、0、15.进行生产者和消费者测试
分别在Broker0上运行一个生产者,Broker1、2上分别运行一个消费者:
kafka-console-producer.sh –broker-list 192.168.16.129:9092 –topic TestTopic kafka-console-consumer.sh –bootstrap-server 192.168.16.129:9093 –topic TestTopic –from-beginning kafka-console-consumer.sh –bootstrap-server 192.168.16.129:9094 –topic TestTopic –from-beginning
免费资源网 – https://freexyz.cn/
© 版权声明
THE END
暂无评论内容