Ubuntu下kafka集群环境搭建及测试

时间:2023-03-09 07:37:41
Ubuntu下kafka集群环境搭建及测试

kafka介绍:

Kafka[1是一种高吞吐量[2]  的分布式发布订阅消息系统,有如下特性:
  • 通过O(1)的磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能。
  • 高吞吐量[2]  :即使是非常普通的硬件Kafka也可以支持每秒数百万[2]  的消息。
  • 支持通过Kafka服务器和消费机集群来分区消息。
  • 支持Hadoop并行数据加载。[3] 

Kafka相关术语介绍

  • Broker
    Kafka集群包含一个或多个服务器,这种服务器被称为broker[5] 
  • Topic
    每条发布到Kafka集群的消息都有一个类别,这个类别被称为Topic。(物理上不同Topic的消息分开存储,逻辑上一个Topic的消息虽然保存于一个或多个broker上但用户只需指定消息的Topic即可生产或消费数据而不必关心数据存于何处)
  • Partition
    Partition是物理上的概念,每个Topic包含一个或多个Partition.
  • Producer
    负责发布消息到Kafka broker
  • Consumer
    消息消费者,向Kafka broker读取消息的客户端。
  • Consumer Group
    每个Consumer属于一个特定的Consumer Group(可为每个Consumer指定group name,若不指定group name则属于默认的group)。

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

单节点模式:

1,解压

root@Ubuntu-:/usr/local# tar zxvf kafka_2.-0.8.2.2.tgz

2,重命名

root@Ubuntu-:/usr/local# mv /usr/local/kafka_2.-0.8.2.2 /usr/local/kafka

3,起zookeeper集群到指定后台文件(不占用页面)

root@Ubuntu-:/usr/local/kafka# bin/zookeeper-server-start.sh config/zookeeper.properties > logs/kafka131-.log >& &

4,起kafka集群到指定后台文件(不占用页面)

bin/kafka-server-start.sh config/server.properties >logs/kafka131-server-.log >& &

5,查看zookeeper和kafka启动情况

root@Ubuntu-:/usr/local/kafka# jps
QuorumPeerMain
Kafka
Jps

6,新增一个topic

root@Ubuntu-:/usr/local/kafka# bin/kafka-topics.sh --create --topic huxing --zookeeper localhost: --partitions  --replication
Created topic "huxing".

7,所有可以使用的topic

root@Ubuntu-:/usr/local/kafka# bin/kafka-topics.sh --list --zookeeper localhost:
huxing

8,查询某个topic的信息

root@Ubuntu-:/usr/local/kafka# bin/kafka-topics.sh --describe --topic huxing --zookeeper localhost:
Topic:huxing PartitionCount: ReplicationFactor: Configs:
Topic: huxing Partition: Leader: Replicas: Isr:
Topic: huxing Partition: Leader: Replicas: Isr:

9,删除某个topic

在此之前需要在server.properties的配置文件中加入一行

delete.topic.enable=true

重启,然后执行代码

root@Ubuntu-:/usr/local/kafka# bin/kafka-topics.sh --delete --topic huxing --zookeeper localhost:
Topic huxing is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
root@Ubuntu-:/usr/local/kafka# bin/kafka-topics.sh --list --zookeeper localhost:
hello
world

在jps中可以查询到确实已经被删除了

10,创建producer和consumer用户

在创建producer用户时,出现下列错误:Ubuntu下kafka集群环境搭建及测试

解决办法:在server.properties的配置文件中加入一行

advertised.host.name=192.168.22.131

在server.properties 上该参数用来配置返回的host.name值,把这个参数配置为外网IP地址。
这个参数默认没有启用,默认是返回的 java.net.InetAddress.getCanonicalHostName 的值,这个值并不等于 hostname 的值而是返回IP,但在linux上这个值就是 hostname 的值。

配置好后重启,在两个shell框中输入下列命令:

producer:

root@Ubuntu-:/usr/local/kafka# bin/kafka-console-producer.sh --topic hello --broker-list localhost:
[-- ::,] WARN Property topic is not valid (kafka.utils.VerifiableProperties)
aa
aaa

consumer:

root@Ubuntu-:/usr/local/kafka# bin/kafka-console-consumer.sh --topic hello --zookeeper localhost:

于是,在producer的shell框中输入的内容将会同步更新到consumer中

标记删除的topic也可以使用

--------------------------------------------

集群模式:

由于我用kafka中内置的zookeeper怎么配置也无法启动,所以打算放弃kafka中内置的ZK,转而自己去下载

zookeeper

1,解压压缩包

2,进入zookeeper目录下的conf子目录, 创建zoo.cfg文本文件,内容如下:

initLimit=
tickTime=
syncLimit=
dataDir=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/logs
clientPort=
server.=192.168.22.131::
server.=192.168.22.132::
server.=192.168.22.135::

三个服务器上都是一样的内容

参数说明:

  • tickTime: zookeeper中使用的基本时间单位, 毫秒值.
  • dataDir: 数据目录. 可以是任意目录.
  • dataLogDir: log目录, 同样可以是任意目录. 如果没有设置该参数, 将使用和dataDir相同的设置.
  • clientPort: 监听client连接的端口号.

3,在配置的dataDir目录下,创建myid文本文件,内容为server.1等的”.”后的数字相同,每个服务器下的myid文件都不相同

kafka:

1,在server.propoties文件中更改:

Ubuntu下kafka集群环境搭建及测试

Ubuntu下kafka集群环境搭建及测试

Ubuntu下kafka集群环境搭建及测试

启动:

启动zookeeper服务器(三台):

 bin/zkServer.sh start

启动kafka服务器(三台):

bin/kafka-server-start.sh config/server.properties

创建topic:

root@Ubuntu-:/usr/local/kafka# bin/kafka-topics.sh --create --topic huxing2 --zookeeper 192.168.22.131:,192.168.22.132:,192.168.22.135: --replication-factor  --partitions
Created topic "huxing2".

列出可使用的topic:

root@Ubuntu-:/usr/local/kafka# bin/kafka-topics.sh --list --zookeeper localhost:
huxing2

查询某个topic的信息:

root@Ubuntu-:/usr/local/kafka#  bin/kafka-topics.sh --describe --topic huxing2 --zookeeper localhost:
Topic:huxing2 PartitionCount: ReplicationFactor: Configs:
Topic: huxing2 Partition: Leader: Replicas: ,, Isr: ,,
Topic: huxing2 Partition: Leader: Replicas: ,, Isr: ,,

从图中可以看出来,Partition分别存在不同的broker中,每个broker都不同,所以broker无副本

windows下运行kafka(自带zookeeper)

1,下载kafka

https://www.apache.org/dyn/closer.cgi?path=/kafka/2.2.1/kafka_2.11-2.2.1.tgz

2,解压到windows目录,并进入到该目录的cmd中

启动kafka自带的zookeeper

.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties

启动kafka

.\bin\windows\kafka-server-start.bat .\config\server.properties

创建topic主题:windows目录下面

kafka-topics.bat --create --zookeeper localhost: --replication-factor  --partitions  --topic test

创建生产者发送消息:windows目录下面

kafka-console-producer.bat --broker-list localhost: --topic test

创建消费者消费消息:windows目录下面

kafka-console-consumer.bat --bootstrap-server localhost: --topic test --from-beginning

删除topic test

kafka-topics.bat --delete --topic test --bootstrap-server localhost:

查看topic

kafka-topics.bat --list --bootstrap-server localhost: