Kafka 快速入门(安装)

kafka学习目录:kafka目录


二、Kafka 快速入门

2.1、windows版安装

2.1.1、Quick Start

本次安装学习在Windows操作系统进行。(Linux版本的差别不大,运行脚本文件后缀从bat改为sh,配置路径改用Unix风格的)

本节教程来源

Step 1: Download the code

下载代码并解压。

下载kafka 0.11.0.0版本,解压到C:\Kafka\路径下,Kafka主目录文件为C:\Kafka\kafka_2.11-0.11.0.0(下文用KAFKA_HOME表示)。

Step 2: Start the server

Kafka 用到 ZooKeeper 功能,所以要预先运行ZooKeeper。了解更多ZooKeeper信息,可点击阅读ZooKeeper学习笔记

  • 首先,修改%KAFKA_HOME%\conf\zookeeper.properties中的dataDir=/tmp/zookeeper,改为dataDir=C:\\Kafka\\data\\zookeeper
  • 创建新目录C:\\Kafka\\data\\zookeeper
  • 启动cmd,工作目录切换到%KAFKA_HOME%,执行命令行:
start bin\windows\zookeeper-server-start.bat config\zookeeper.properties
  • 修改%KAFKA_HOME%\conf\server.properties中的log.dirs=/tmp/kafka-logs,改为log.dirs=C:\\Kafka\\data\\kafka-logs
  • 创建新目录C:\\Kafka\\data\\kafka-logs
  • 另启动cmd,工作目录切换到%KAFKA_HOME%,执行命令行:
start bin\windows\kafka-server-start.bat config\server.properties
  • 可写一脚本,一键启动
  • 关闭服务,bin\windows\kafka-server-stop.batbin\windows\zookeeper-server-stop.bat

TODO:一个问题,通过kafka-server-stop.bat或右上角关闭按钮来关闭Kafka服务后,马上下次再启动Kafka,抛出异常,说某文件被占用,需清空log.dirs目录下文件,才能重启Kafka。

[2020-07-21 21:43:26,755] ERROR There was an error in one of the threads during logs loading: java.nio.file.FileSystemException: C:\Kafka\data\kafka-logs-0\my-replicated-topic-0\00000000000000000000.timeindex: 另一个程序正在使用此文件,进程无法访问。
 (kafka.log.LogManager)
...

参阅网络,这可能是在windows下的一个Bug,没有更好的解决方案,暂时写个py脚本用来对kafka的log文件进行删除。下次启动kafka,先运行这个删除脚本吧。

好消息,当你成功启动kafka,然后在对应的命令行窗口用Ctrl + C结束Kakfa,下次不用清理kafka日志,也能正常启动。

Step 3: Create a topic
  • 用单一partition和单一replica创建一个名为test的topic:
bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
  • 查看已创建的topic,也就刚才创建的名为test的topic:
bin\windows\kafka-topics.bat --list --zookeeper localhost:2181

或者,你可配置你的broker去自动创建未曾发布过的topic,代替手动创建topic

Step 4: Send some messages

运行producer,然后输入几行文本,发至服务器:

bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test
>hello, kafka.
>what a nice day!
>to be or not to be. that' s a question.

请勿关闭窗口,下面步骤需要用到

Step 5: Start a consumer

运行consumer,将Step 4中输入的几行句子,标准输出。

bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning
hello, kafka.
what a nice day!
to be or not to be. that' s a question.

若你另启cmd,执行命令行bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning来运行consumer,然后在 Step 4 中producer窗口输入一行句子,如I must admit, I can't help but feel a twinge of envy.,两个consumer也会同时输出I must admit, I can't help but feel a twinge of envy.

Step 6: Setting up a multi-broker cluster

目前为止,我们仅作为一个单一broker,这不好玩。让我们弄个有三个节点的集群来玩玩。

  • 首先,在%KAFKA%\config\server.properties的基础上创建两个副本server-1.propertiesserver-2.properties
copy config\server.properties config\server-1.properties
copy config\server.properties config\server-2.properties
  • 打开副本,编辑如下属性
#config/server-1.properties:broker.id=1listeners=PLAINTEXT://127.0.0.1:9093log.dir=C:\\Kafka\\data\\kafka-logs-1 #config/server-2.properties:broker.id=2listeners=PLAINTEXT://127.0.0.1:9094log.dir=C:\\Kafka\\data\\kafka-logs-2

这个broker.id属性是集群中每个节点的唯一永久的名称。

我们必须重写端口和日志目录,只是因为我们在同一台机器上运行它们,并且我们希望阻止brokers试图在同一个端口上注册或覆盖彼此的数据。

  • 我们已经启动了Zookeeper和我们的单个节点,所以我们只需要启动两个新节点:
start bin\windows\kafka-server-start.bat config\server-1.propertiesstart bin\windows\kafka-server-start.bat config\server-2.properties
  • 创建一个replication-factor为3的topic:
bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
  • OK,现在我们有了一个集群,但是我们怎么知道哪个broker在做什么呢?那就运行describe topics命令:
bin\windows\kafka-topics.bat --describe --zookeeper localhost:2181 --topic my-replicated-topicTopic:my-replicated-topic       PartitionCount:1        ReplicationFactor:3Configs:		Topic: my-replicated-topic      Partition: 0    Leader: 0       		Replicas: 0,1,2        Isr: 0,1,2
  • 以下是输出的说明。第一行给出所有Partition的摘要,每一行提供有关一个Partition的信息。因为这个Topic只有一个Partition,所以只有一行。
    • "leader"是负责给定Partition的所有读写的节点。每个节点都可能成为Partition随机选择的leader。
    • "replicas"是复制此Partition日志的节点列表,无论它们是leader还是当前处于存活状态。
    • "isr"是一组 “in-sync” replicas。这是replicas列表的一个子集,它当前处于存活状态,并补充leader。

注意,在我的示例中,node 0是Topic唯一Partition的leader。(下面操作需要用到)

  • 让我们为我们的新Topic发布一些信息:
bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic my-replicated-topic>There's sadness in your eyes, I don't want to say goodbye to you.>Love is a big illusion, I should try to forget, but there's something left in my head.>
  • 让我们接收刚刚发布的信息吧!
bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topicThere's sadness in your eyes, I don't want to say goodbye to you.Love is a big illusion, I should try to forget, but there's something left in my head.
  • 让我们测试一下容错性,由上文可知,Broker 0 身为 leader,因此,让我们干掉它吧:
    • 先找出 Broker 0 的进程pid。
    • 杀掉 Broker 0 的进程。
wmic process where "caption='java.exe' and commandline like '%server.properties%'" get processid,captionCaption   ProcessIdjava.exe  7528taskkill /pid 7528 /f成功: 已终止 PID 为 7528 的进程。
  • 原leader已被替换成它的flowers中的其中一个,并且 node 0 不在 in-sync replica 集合当中。
bin\windows\kafka-topics.bat --describe --zookeeper localhost:2181 --topic my-replicated-topicTopic:my-replicated-topic       PartitionCount:1        ReplicationFactor:3Configs:		Topic: my-replicated-topic      Partition: 0    Leader: 1       Replicas: 0,1,2 Isr: 1,2
  • 尽管原leader已逝,当原来消息依然可以接收。(注意,参数--bootstrap-server localhost:9093,而不是--bootstrap-server localhost:9092
bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9093 --from-beginning --topic my-replicated-topicThere's sadness in your eyes, I don't want to say goodbye to you.Love is a big illusion, I should try to forget, but there's something left in my head.I don't forget the way your kissing, the feeling 's so strong which is lasting for so long.

2.1.2、server.properties配置文件一瞥

#broker 的全局唯一编号,不能重复broker.id=0#删除 topic 功能使能delete.topic.enable=true#处理网络请求的线程数量num.network.threads=3#用来处理磁盘 IO 的现成数量num.io.threads=8#发送套接字的缓冲区大小socket.send.buffer.bytes=102400#接收套接字的缓冲区大小socket.receive.buffer.bytes=102400#请求套接字的缓冲区大小socket.request.max.bytes=104857600#kafka 运行日志存放的路径log.dirs=/opt/module/kafka/logs#topic 在当前 broker 上的分区个数num.partitions=1#用来恢复和清理 data 下数据的线程数量num.recovery.threads.per.data.dir=1#segment 文件保留的最长时间,超时将被删除log.retention.hours=168#配置连接 Zookeeper 集群地址zookeeper.connect=hadoop102:2181,hadoop103:2181,hadoop104:2181

2.1.3、命令行操作Topic增删查

1)查看当前服务器中的所有 topic
bin\windows\kafka-topics.bat --list --zookeeper localhost:2181
2)创建 topic
bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic

选项说明:

  • –topic 定义 topic 名
  • –replication-factor 定义副本数
  • –partitions 定义分区数

为了实现扩展性,一个非常大的 topic 可以分布到多个 broker(即服务器)上,一个 topic 可以分为多个 partition,每个 partition 是一个有序的队列;

a broker = a kafka server a broker can contain N topic a topic can contain N partition a broker can contain a part of a topic (a broker can contain M(N>M) partition)

3)删除 topic
bin\windows\kafka-topics.bat --zookeeper localhost:2181 --delete --topic my-replicated-topic

需要 server.properties 中设置 delete.topic.enable=true 否则只是标记删除。

4)查看某个 Topic 的详情
bin\windows\kafka-topics.bat --zookeeper localhost:2181 --describe --topic first
5)修改分区数
bin\windows\kafka-topics.bat --zookeeper localhost:2181 --alter --topic first --partitions 6
6)命令行控制台生产者消费者测试
发送消息
bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test>hello, kafka.>what a nice day!>to be or not to be. that' s a question.
消费消息
bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginninghello, kafka.what a nice day!to be or not to be. that' s a question.
  • –from-beginning: 会把主题中以往所有的数据都读取出来。

2.2、Linux版安装

2.2.1、安装部署

2.2.1.1 集群规划

hadoop102 hadoop103 hadoop104
zk zk zk
kafka kafka kafka

2.2.1.2 jar 包下载

http://kafka.apache.org/downloads.html

2.2.1.3 集群部署

1)解压安装包

[atguigu@hadoop102 software]$ tar -zxvf kafka_2.11-0.11.0.0.tgz -C /opt/module/

2)修改解压后的文件名称

[atguigu@hadoop102 module]$ mv kafka_2.11-0.11.0.0/ kafka

3)在/opt/module/kafka 目录下创建 logs 文件夹

[atguigu@hadoop102 kafka]$ mkdir logs

4)修改配置文件

[atguigu@hadoop102 kafka]$ cd config/[atguigu@hadoop102 config]$ vi server.properties

输入以下内容:

#broker 的全局唯一编号,不能重复broker.id=0#删除 topic 功能使能delete.topic.enable=true#处理网络请求的线程数量num.network.threads=3#用来处理磁盘 IO 的现成数量num.io.threads=8#发送套接字的缓冲区大小socket.send.buffer.bytes=102400#接收套接字的缓冲区大小socket.receive.buffer.bytes=102400#请求套接字的缓冲区大小socket.request.max.bytes=104857600#kafka 运行日志存放的路径log.dirs=/opt/module/kafka/logs#topic 在当前 broker 上的分区个数num.partitions=1#用来恢复和清理 data 下数据的线程数量num.recovery.threads.per.data.dir=1#segment 文件保留的最长时间,超时将被删除log.retention.hours=168#配置连接 Zookeeper 集群地址zookeeper.connect=hadoop102:2181,hadoop103:2181,hadoop104:2181

5)配置环境变量

[atguigu@hadoop102 module]$ sudo vi /etc/profile#KAFKA_HOMEexport KAFKA_HOME=/opt/module/kafkaexport PATH=$PATH:$KAFKA_HOME/bin[atguigu@hadoop102 module]$ source /etc/profile

6)分发安装包

[atguigu@hadoop102 module]$ xsync kafka/

注意:分发之后记得配置其他机器的环境变量

7)分别在 hadoop103 和 hadoop104 上修改配置文件/opt/module/kafka/config/server.properties
中的 broker.id=1、broker.id=2
注:broker.id 不得重复

8)启动集群
依次在 hadoop102、hadoop103、hadoop104 节点上启动 kafka

[atguigu@hadoop102 kafka]$ bin/kafka-server-start.sh -daemonconfig/server.properties[atguigu@hadoop103 kafka]$ bin/kafka-server-start.sh -daemonconfig/server.properties[atguigu@hadoop104 kafka]$ bin/kafka-server-start.sh -daemonconfig/server.properties

9)关闭集群

[atguigu@hadoop102 kafka]$ bin/kafka-server-stop.sh stop[atguigu@hadoop103 kafka]$ bin/kafka-server-stop.sh stop[atguigu@hadoop104 kafka]$ bin/kafka-server-stop.sh stop

10)kafka 群起脚本

case $1 in "start"){    for i in hadoop102 hadoop103 hadoop104    do    echo "========== $i =========="     ssh $i '/opt/module/kafka/bin/kafka-server-start.sh -daemon     /opt/module/kafka/config/server.properties'    done};;"stop"){    for i in hadoop102 hadoop103 hadoop104    do    echo "========== $i =========="     ssh $i '/opt/module/kafka/bin/kafka-server-start.sh -daemon     /opt/module/kafka/config/server.properties'    done};;esac

2.2.2、 Kafka 命令行操作

1)查看当前服务器中的所有 topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --list
2)创建 topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --create --replication-factor 3 --partitions 1 --topic first

选项说明:
–topic 定义 topic 名
–replication-factor 定义副本数
–partitions 定义分区数

3)删除 topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --delete --topic first

需要 server.properties 中设置 delete.topic.enable=true 否则只是标记删除。

4)发送消息
[atguigu@hadoop102 kafka]$ bin/kafka-console-producer.sh --broker-list hadoop102:9092 --topic first>hello world>atguigu atguigu
5)消费消息
[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh --zookeeper hadoop102:2181 --topic first[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic first[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --from-beginning --topic first

–from-beginning:会把主题中以往所有的数据都读取出来。

6)查看某个 Topic 的详情
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --describe --topic first
7)修改分区数
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --alter --topic first --partitions 6
8)数据日志分离

其实就是把配置文件中的

#kafka 运行数据存放的路径log.dirs=/opt/module/kafka/logs

改为:

log.dirs=/opt/module/kafka/data

名字无所谓,只是log.dirs=/opt/module/kafka/logs 这个文件下也会存放kafka的日志,换个文件夹之后,数据就会单独存放在新的文件夹中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悬浮海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值