kafka常用命令及配置参数详解

1. 安装Kafka

1.1 下载

wget http://mirrors.hust.edu.cn/apache/kafka/2.0.0/kafka_2.12-2.0.0.tgz

如果下载很慢或者不方便,也可以用这里已经下载好的压缩包。链接: https://pan.baidu.com/s/1u8mSfubwZupFqKtK6PH6Qw 提取码: v5em

1.2 解压

tar -xzf kafka_2.12-2.0.0.tgz

注意,kafka_2.12-2.0.0.tgz版本是已经编译好的版本,解压就能使用。

1.3 配置server.properties

默认配置 advertised.listeners=PLAINTEXT://:your.host.name:9092 修改为advertised.listeners=PLAINTEXT://:ip:9092

ip为服务器ip。

hostname和端口是用来建议给生产者和消费者使用的,如果没有设置,将会使用listeners的配置,如果listeners也没有配置,将使用java.net.InetAddress.getCanonicalHostName()来获取这个hostname和port,对于ipv4,基本就是localhost了。

"PLAINTEXT"表示协议,可选的值有PLAINTEXT和SSL,hostname可以指定IP地址,也可以用"0.0.0.0"表示对所有的网络接口有效,如果hostname为空表示只对默认的网络接口有效。也就是说如果你没有配置advertised.listeners,就使用listeners的配置通告给消息的生产者和消费者,这个过程是在生产者和消费者获取源数据(metadata)。

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# see kafka.server.KafkaConfig for additional details and defaults

############################# Server Basics #############################

##################################################################################
#  broker就是一个kafka的部署实例,在一个kafka集群中,每一台kafka都要有一个broker.id
#  并且,该id唯一,且必须为整数
##################################################################################
broker.id=10

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = security_protocol://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092

##################################################################################
#The number of threads handling network requests
# 默认处理网络请求的线程个数 3个
##################################################################################
num.network.threads=3
##################################################################################
# The number of threads doing disk I/O
# 执行磁盘IO操作的默认线程个数 8
##################################################################################
num.io.threads=8

##################################################################################
# The send buffer (SO_SNDBUF) used by the socket server
# socket服务使用的进行发送数据的缓冲区大小,默认100kb
##################################################################################
socket.send.buffer.bytes=102400

##################################################################################
# The receive buffer (SO_SNDBUF) used by the socket server
# socket服务使用的进行接受数据的缓冲区大小,默认100kb
##################################################################################
socket.receive.buffer.bytes=102400

##################################################################################
# The maximum size of a request that the socket server will accept (protection against OOM)
# socket服务所能够接受的最大的请求量,防止出现OOM(Out of memory)内存溢出,默认值为:100m
# (应该是socker server所能接受的一个请求的最大大小,默认为100M)
##################################################################################
socket.request.max.bytes=104857600

############################# Log Basics (数据相关部分,kafka的数据称为log)#############################

##################################################################################
# A comma seperated list of directories under which to store log files
# 一个用逗号分隔的目录列表,用于存储kafka接受到的数据
##################################################################################
log.dirs=/home/uplooking/data/kafka

##################################################################################
# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
# 每一个topic所对应的log的partition分区数目,默认1个。更多的partition数目会提高消费
# 并行度,但是也会导致在kafka集群中有更多的文件进行传输
# (partition就是分布式存储,相当于是把一份数据分开几份来进行存储,即划分块、划分分区的意思)
##################################################################################
num.partitions=1

##################################################################################
# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
# 每一个数据目录用于在启动kafka时恢复数据和在关闭时刷新数据的线程个数。如果kafka数据存储在磁盘阵列中
# 建议此值可以调整更大。
##################################################################################
num.recovery.threads.per.data.dir=1

############################# Log Flush Policy (数据刷新策略)#############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs(平衡) here:
#    1. Durability 持久性: Unflushed data may be lost if you are not using replication.
#    2. Latency 延时性: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
#    3. Throughput 吞吐量: The flush is generally the most expensive operation, and a small flush interval may lead to exceessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.
# kafka中只有基于消息条数和时间间隔数来制定数据刷新策略,而没有大小的选项,这两个选项可以选择配置一个
# 当然也可以两个都配置,默认情况下两个都配置,配置如下。

# The number of messages to accept before forcing a flush of data to disk
# 消息刷新到磁盘中的消息条数阈值
#log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
# 消息刷新到磁盘生成一个log数据文件的时间间隔
#log.flush.interval.ms=1000

############################# Log Retention Policy(数据保留策略) #############################

# The following configurations control the disposal(清理) of log segments(分片). The policy can
# be set to delete segments after a period of time, or after a given size has accumulated(累积).
# A segment will be deleted whenever(无论什么时间) *either* of these criteria(标准) are met. Deletion always happens
# from the end of the log.
# 下面的配置用于控制数据片段的清理,只要满足其中一个策略(基于时间或基于大小),分片就会被删除

# The minimum age of a log file to be eligible for deletion
# 基于时间的策略,删除日志数据的时间,默认保存7天
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log as long as the remaining
# segments don't drop below log.retention.bytes. 1G
# 基于大小的策略,1G
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
# 数据分片策略
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies 5分钟
# 每隔多长时间检测数据是否达到删除条件
log.retention.check.interval.ms=300000

############################# Zookeeper #############################

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=uplooking01:2181,uplooking02:2181,uplooking03:2181

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000

2. 启动Kafka

2.1 启动ZooKeeper

/usr/local/zookeeper-3.4.13/bin/zkServer.sh start

注意,需要先启动ZooKeeper再启动kafka,不然会报错。

2.2 启动kafka

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

启动Kafka Broker后,在ZooKeeper终端上键入命令 jps,查看kafka进程。

2.3 停止kafka

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

3. Kafka topic创建

3.1 创建topic

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic demo

其中demo为创建的topic名称。

创建了一个名为 demo 的主题,其中包含一个分区和一个副本因子。 创建成功之后会输出:Created topic "demo".

3.2 查询topic列表

bin/kafka-topics.sh --list --zookeeper localhost:2181

3.3 查看topic信息

bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic demo

3.3 删除topic

bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic demo

 

4. Kafka 生产/消费

4.1 启动生产者

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic demo

从上面的语法,生产者命令行客户端需要两个主要参数 -

代理列表 - 我们要发送邮件的代理列表。 在这种情况下,我们只有一个代理。 Config / server.properties文件包含代理端口ID,因为我们知道我们的代理正在侦听端口9092,因此您可以直接指定它。主题名称:demo。

4.2 启动消费者

为了方便测试,另启一个sheel窗口 这样效果更明显。需要注意的是旧版本和新版本的命令是不一样的

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic demo --from-beginning

报错提示: zookeeper is not a recognized option

发现在启动的时候说使用 --zookeeper是一个过时的方法,最新的版本中命令如下:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic demo --from-beginning

可以开启两个终端,一个发送消息,一个接受消息。效果如下:

5. Kafka常用命令

5.1 查看消费者状态和消费详情

有时候我们需要关心消费者应用的状态,一般消费者应用会自己通过日志获知当前消费到了哪个topic的哪个partition的哪个offset,但当消费者出问题之后,或者出于监控的原因,我们需要知道消费者的状态和详情,那么需要借助kafka提供的相关命令。

命令格式:

bin/kafka-consumer-groups.sh --bootstrap-server BORKER_HOST1:PORT1,BORKER_HSOT2:PORT2 --list

bin/kafka-consumer-groups.sh --bootstrap-server BORKER_HOST1:PORT1,BORKER_HSOT2:PORT2 --group GROUP_NAME --describe

Tips:

  • BROKER_HOST是kafka server的ip地址,PORT是server的监听端口。多个host port之间用逗号隔开
  • 第一条命令是获取group列表,一般而言,应用是知道消费者group的,通常在应用的配置里,如果已知,该步骤可以省略
  • 第二条命令是查看具体的消费者group的详情信息,需要给出group的名称

示例:

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group console-consumer-68682 --describe ​

  • TOPIC:该group里消费的topic名称
  • PARTITION:分区编号
  • CURRENT-OFFSET:该分区当前消费到的offset
  • LOG-END-OFFSET:该分区当前latest offset
  • LAG:消费滞后区间,为LOG-END-OFFSET-CURRENT-OFFSET,具体大小需要看应用消费速度和生产者速度,一般过大则可能出现消费跟不上,需要引起应用注意
  • CONSUMER-ID:server端给该分区分配的consumer编号
  • HOST:消费者所在主机
  • CLIENT-ID:消费者id,一般由应用指定

5.2 查询topic的offset的范围

用下面命令可以查询到topic:demo broker:localhost:9092的offset的最小值:

bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 -topic demo --time -2

查询offset的最大值:

bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 -topic demo --time -1

5.3 有些场景可能希望修改消费者消费到的offset位置,以达到重新消费,或者跳过一部分消息的目的,这时候重置offset的工具就非常实用。

命令格式:

bin/kafka-consumer-groups.sh --bootstrap-server BORKER_HOST1:PORT1,BORKER_HSOT2:PORT2 --group GROUP_NAME --reset-offsets --execute --to-offset NEW_OFFSET --topic TOPIC_NAME

bin/kafka-consumer-groups.sh --bootstrap-server BORKER_HOST1:PORT1,BORKER_HSOT2:PORT2 --group GROUP_NAME --reset-offsets --execute --to-earliest/--to-latest --topic TOPIC_NAME

Tips:

  • BROKER_HOST是kafka server的ip地址,PORT是server的监听端口。多个host port之间用逗号隔开
  • 第一条命令是将指定GROUP_NAME和topic的offset修改到NEW_OFFSET的位置,重启消费者后,消费中将从指定的offset处消费。注意这里只能NEW_OFFSET只能设置一个值,也就是说,所有的分区都将使用这个值,如果分区消息负载不均衡,需要考虑是否适用。
  • 第二条命令是将指定GROUP_NAME和topic的offset修改到earliest或者latest位置,使得消费者从头或者从尾部消费。

示例:

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group console-consumer-68682 --reset-offsets --execute --to-offset 3 --topic demo

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group console-consumer-68682 --reset-offsets --execute --to-latest --topic demo

可以通过直接更换消费者group id的方式,配合消费者默认的消费策略,可以达到类似的效果,反而更加简单、高效和安全。

5.4 查看topic的状态和分区负载详情

当broker出现宕机,恢复之后,我们可以看下topic的leader是否负载均衡。因为kafka的所有读写消息的请求,都是发送到partition leader上的,因此在生产环境,负载均衡显得尤其重要。

命令格式:

bin/kafka-topics.sh --zookeeper ZOOKEEPER_HOST1:PORT1,ZOOKEEPER_HOST2:PORT2 --describe --topic TOPIC_NAME

Tips:

  • ZOOKEEPER_HOST是kafka所使用的zookeeper的ip地址,PORT是zookeeper监听的端口。多个host port之间用逗号隔开
  • 类似的,zookeeper集群不需要全部列上,给出一个可用的zk地址和端口即可

示例:

bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic demo ​

如果发现以下现象说明kafka异常:

  • 某个topic的每个分区,同步副本数量和设定的副本数量不一致
  • 某个topic的每个分区,leader的id数值是-1或者none

5.5 消费消息

从头开始消费:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic demo --from-beginning

从尾部开始:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic demo --offset latest --partition 0

指定分区:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic demo --offset latest --partition 0

取指定个数:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic demo --offset latest --partition 0 --max-messages 2

如示例部分,取2个消息,取完自动结束回话。

5.6 自带压测工具

测试使用Kafka自带的测试脚本,通过命令对Kafka发起写入MQ消息和Kafka消费MQ消息的请求。模拟不同数量级的MQ消息写入和MQ消息消费场景,根据Kafka的处理结果,评估Kafka是否满足处理亿级以上的消息的能力。

命令格式:

bin/kafka-producer-perf-test.sh --topic demo --num-records 100 --record-size 1 --throughput 100 --producer-props bootstrap.servers=localhost:9092

示例:

测试项

压测消息数(单位:W) 

测试命令

写入MQ消息

10 

bin/kafka-producer-perf-test.sh --topic demo --num-records 100000 --record-size 1000  --throughput 2000 --producer-props bootstrap.servers=localhost:9092

 

100 

bin/kafka-producer-perf-test.sh --topic demo --num-records 1000000 --record-size 2000  --throughput 5000 --producer-props bootstrap.servers=localhost:9092

 

1000

bin/kafka-producer-perf-test.sh --topic demo --num-records 10000000 --record-size 2000  --throughput 5000 --producer-props bootstrap.servers=localhost:9092

消费MQ消息

10 

bin/kafka-consumer-perf-test.sh --broker-list localhost:9092 --topic demo --fetch-size 1048576 --messages 100000 --threads 1

 

100 

bin/kafka-consumer-perf-test.sh --broker-list localhost:9092 --topic demo --fetch-size 1048576 --messages 1000000 --threads 1

 

1000 

bin/kafka-consumer-perf-test.sh --broker-list localhost:9092 --topic demo --fetch-size 1048576 --messages 10000000 --threads 1

 

 

你可能感兴趣的:(Kafka)