安装Zookeeper和Kafka集群

向右看齐 2023-09-26 20:58 128阅读 0赞

安装Zookeeper和Kafka集群

本文介绍如何安装Zookeeper和Kafka集群。为了方便,介绍的是在一台服务器上的安装,实际应该安装在多台服务器上,但步骤是一样的。

安装Zookeeper集群

下载安装包

从官网上下载安装包:

  1. curl https://dlcdn.apache.org/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz -o apache-zookeeper-3.7.1-bin.tar.gz

解压:

  1. tar xvf apache-zookeeper-3.7.1-bin.tar.gz

配置

创建目录 zk1,然后添加如下配置:

zk1/myid:

  1. 1

zk1/zk.config:

  1. tickTime=2000
  2. initLimit=10
  3. syncLimit=5
  4. dataDir=/Users/larry/IdeaProjects/pkslow-samples/other/install-kafka-cluster/src/main/zookeeper/zk1
  5. clientPort=2181
  6. server.1=127.0.0.1:2888:3888
  7. server.2=127.0.0.1:2889:3889
  8. server.3=127.0.0.1:2890:3890

对于zk2zk3也重复同样的步骤,并修改相应的配置:

zk2/myid:

  1. 2

zk2/zk.config:

  1. tickTime=2000
  2. initLimit=10
  3. syncLimit=5
  4. dataDir=/Users/larry/IdeaProjects/pkslow-samples/other/install-kafka-cluster/src/main/zookeeper/zk2
  5. clientPort=2182
  6. server.1=127.0.0.1:2888:3888
  7. server.2=127.0.0.1:2889:3889
  8. server.3=127.0.0.1:2890:3890

zk3/myid:

  1. 3

zk3/zk.config:

  1. tickTime=2000
  2. initLimit=10
  3. syncLimit=5
  4. dataDir=/Users/larry/IdeaProjects/pkslow-samples/other/install-kafka-cluster/src/main/zookeeper/zk3
  5. clientPort=2183
  6. server.1=127.0.0.1:2888:3888
  7. server.2=127.0.0.1:2889:3889
  8. server.3=127.0.0.1:2890:3890

启动集群

启动三个服务如下:

  1. $ ./apache-zookeeper-3.7.1-bin/bin/zkServer.sh start ./zk1/zk.config
  2. ZooKeeper JMX enabled by default
  3. Using config: ./zk1/zk.config
  4. Starting zookeeper ... STARTED
  5. $ ./apache-zookeeper-3.7.1-bin/bin/zkServer.sh start ./zk2/zk.config
  6. ZooKeeper JMX enabled by default
  7. Using config: ./zk2/zk.config
  8. Starting zookeeper ... STARTED
  9. $ ./apache-zookeeper-3.7.1-bin/bin/zkServer.sh start ./zk3/zk.config
  10. ZooKeeper JMX enabled by default
  11. Using config: ./zk3/zk.config
  12. Starting zookeeper ... STARTED

查看状态

通过status命令查看:

  1. $ ./apache-zookeeper-3.7.1-bin/bin/zkServer.sh status ./zk1/zk.config
  2. ZooKeeper JMX enabled by default
  3. Using config: ./zk1/zk.config
  4. Client port found: 2181. Client address: localhost. Client SSL: false.
  5. Mode: follower
  6. $ ./apache-zookeeper-3.7.1-bin/bin/zkServer.sh status ./zk2/zk.config
  7. ZooKeeper JMX enabled by default
  8. Using config: ./zk2/zk.config
  9. Client port found: 2182. Client address: localhost. Client SSL: false.
  10. Mode: leader
  11. $ ./apache-zookeeper-3.7.1-bin/bin/zkServer.sh status ./zk3/zk.config
  12. ZooKeeper JMX enabled by default
  13. Using config: ./zk3/zk.config
  14. Client port found: 2183. Client address: localhost. Client SSL: false.
  15. Mode: follower

连接其中一个服务并添加数据:

  1. $ ./apache-zookeeper-3.7.1-bin/bin/zkCli.sh -server localhost:2181
  2. [zk: localhost:2181(CONNECTED) 0] create /pkslow
  3. Created /pkslow
  4. [zk: localhost:2181(CONNECTED) 1] create /pkslow/website www.pkslow.com
  5. Created /pkslow/website

连接另外一个服务,并查看数据,发现与之前创建的是一样的:

  1. $ ./apache-zookeeper-3.7.1-bin/bin/zkCli.sh -server localhost:2182
  2. [zk: localhost:2182(CONNECTED) 1] get /pkslow/website
  3. www.pkslow.com

目录结构如下:

a575b5c0ad1286609de07eadb891e922.png

安装Kafka集群

下载安装包

通过官网下载如下:

  1. curl https://downloads.apache.org/kafka/3.4.0/kafka_2.13-3.4.0.tgz -o kafka_2.13-3.4.0.tgz

解压安装包:

  1. tar -xzf kafka_2.13-3.4.0.tgz

配置

Broker 1的配置如下:

  1. broker.id=1
  2. port=9091
  3. listeners=PLAINTEXT://:9091
  4. zookeeper.connect=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
  5. log.dirs=/Users/larry/IdeaProjects/pkslow-samples/other/install-kafka-cluster/src/main/kafka/kafka1/kafka-logs

Broker 2的配置如下:

  1. broker.id=2
  2. port=9092
  3. listeners=PLAINTEXT://:9092
  4. zookeeper.connect=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
  5. log.dirs=/Users/larry/IdeaProjects/pkslow-samples/other/install-kafka-cluster/src/main/kafka/kafka2/kafka-logs

Broker 3的配置如下:

  1. broker.id=3
  2. port=9093
  3. listeners=PLAINTEXT://:9093
  4. zookeeper.connect=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
  5. log.dirs=/Users/larry/IdeaProjects/pkslow-samples/other/install-kafka-cluster/src/main/kafka/kafka3/kafka-logs

目录结构如下:

8e79c51d087a7087490fce9f5a3f91aa.png

启动集群

启动kafka服务如下:

  1. ./kafka_2.13-3.4.0/bin/kafka-server-start.sh ./kafka1/server.properties
  2. ./kafka_2.13-3.4.0/bin/kafka-server-start.sh ./kafka2/server.properties
  3. ./kafka_2.13-3.4.0/bin/kafka-server-start.sh ./kafka3/server.properties

检查如测试

创建topic:

  1. $ kafka_2.13-3.4.0/bin/kafka-topics.sh --create --topic pkslow-topic --bootstrap-server localhost:9091,localhost:9092,localhost:9093 --partitions 3 --replication-factor 3
  2. Created topic pkslow-topic.

列出topic:

  1. $ kafka_2.13-3.4.0/bin/kafka-topics.sh --list --bootstrap-server localhost:9091,localhost:9092,localhost:9093
  2. pkslow-topic

查看topic:

  1. $ kafka_2.13-3.4.0/bin/kafka-topics.sh --describe --topic pkslow-topic --bootstrap-server localhost:9091,localhost:9092,localhost:9093
  2. Topic: pkslow-topic TopicId: 7CLy7iZeRvm8rCrn8Dw_mA PartitionCount: 3 ReplicationFactor: 3 Configs:
  3. Topic: pkslow-topic Partition: 0 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2
  4. Topic: pkslow-topic Partition: 1 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
  5. Topic: pkslow-topic Partition: 2 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1

生产者发消息到brokers:

  1. $ kafka_2.13-3.4.0/bin/kafka-console-producer.sh --broker-list localhost:9091,localhost:9092,localhost:9093 --topic pkslow-topic
  2. >My name is Larry Deng.
  3. >My website is www.pkslow.com.
  4. >

消费者从brokers收消息:

  1. $ kafka_2.13-3.4.0/bin/kafka-console-consumer.sh --bootstrap-server localhost:9091,localhost:9092,localhost:9093 --topic pkslow-topic --from-beginning
  2. My name is Larry Deng.
  3. My website is www.pkslow.com.

#

发表评论

表情:
评论列表 (有 0 条评论,128人围观)

还没有评论,来说两句吧...

相关阅读