Kafka 服务安装
1、下载安装 Kafka
官方地址:https://kafka.apache.org/downloads
下载安装最新的 Kafka 版本(官方推荐 2.13 版本).
$ cd /usr/local/src && wget https://downloads.apache.org/kafka/3.6.0/kafka_2.13-3.6.0.tgz --no-check-certificate
$ tar -xzf kafka_2.13-3.6.0.tgz -C /usr/local
$ echo 'export KAFKA_HOME=/usr/local/kafka_2.13-3.6.0' >>/etc/profile
$ echo 'export PATH=$PATH:$KAFKA_HOME/bin' >>/etc/profile
$ source /etc/profile
2、创建文件目录
mkdir -p /home/kafka/data;
mkdir -p /home/kafka/logs;
# 如有独立 ZooKeeper ,则忽略
mkdir -p /home/zookeeper/data;
mkdir -p /home/zookeeper/logs;
3、修改配置文件
在修改配置前,我们需要先对默认配置的各个参数进行了解,下面是默认配置说明:
# 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.
#
# This configuration file is intended for use in ZK-based mode, where Apache ZooKeeper is required.
# See kafka.server.KafkaConfig for additional details and defaults
#
############################# Server Basics #############################
# The id of the broker. This must be set to a unique integer for each broker.
# Broker的全局唯一编号,不能重复
broker.id=0
############################# Socket Server Settings #############################
# The address the socket server listens on. If not configured, the host name will be equal to the value of
# java.net.InetAddress.getCanonicalHostName(), with PLAINTEXT listener name, and port 9092.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
# 指定 Kafka 本机的监听地址及端口(是Kafka真正bind的地址)
#listeners=PLAINTEXT://:9092
# Listener name, hostname and port the broker will advertise to clients.
# If not set, it uses the value for "listeners".
# Broker将向客户端暴露的地址和端口;如果没有设置,它将使用“listeners”的值。否则,它将使用该值
#advertised.listeners=PLAINTEXT://your.host.name:9092
# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
# The number of threads that the server uses for receiving requests from the network and sending responses to the network
# 服务器用于从网络接收请求并向网络发送响应的线程数, 可调优此参数,提高性能
# 接收线程会将接收到的消息放到内存中,然后再从内存中写入磁盘。
num.network.threads=3
# The number of threads that the server uses for processing requests, which may include disk I/O
# 服务器用于处理请求的线程数,其中可能包括磁盘I/O, 可调优此参数,提高性能
num.io.threads=8
# The send buffer (SO_SNDBUF) used by the socket server
# Socket 服务使用的发送缓冲区(SO_SNDBUF), 可调优此参数,提高性能
socket.send.buffer.bytes=102400
# The receive buffer (SO_RCVBUF) used by the socket server
# Socket 服务使用的接收缓冲区(SO_RCVBUF), 可调优此参数,提高性能
socket.receive.buffer.bytes=102400
# The maximum size of a request that the socket server will accept (protection against OOM)
# Socket 服务器将接受的请求的最大大小(防止OOM),message.max.bytes 需要小于socket.request.max.bytes,可被topic创建时的指定参数覆盖
socket.request.max.bytes=104857600
############################# Log Basics #############################
# A comma separated list of directories under which to store log files
# 存储日志文件的目录,多个地址可用逗号分割
# 如果指定了多个地址,那么broker就会根据"最少使用原则",把同一个分区数据片段放在同一个路径下
log.dirs=/tmp/kafka-logs
# 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的默认日志分区数。过多的分区将产生较大的并行消耗,但这也会导致brokers之间有更多的文件。
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.
# 启动时用于日志恢复和关闭时用于刷新的每个数据目录的线程数。
# 对于数据目录位于RAID阵列中的部署,建议增加此值。
# Kafka 会使用可配置的线程池来处理日志片段:
# a、服务器正常启动,用于打开每个分区的日志片段
# b、服务器崩愤后重启,用于检查和截短每个分区的日志片段
# c、服务器正常关闭,用于关闭日志片段。
# 默认情况下,每个日志目录只使用一个线程。
# 因为这些线程只是在服务器启动和关闭时会用到,所以完全可以设置大量的线程来达到并行操作
# 特别是对于包含大量分区的服务器来说,一旦发生崩愤,在进行恢复时使用井行操作可能会省下数小时的时间
# 设置此参数时需要注意,所配置的数字对应的是log.dirs指定的单个日志目录。
# 也就是说,如果num.recovery.threads.per.data.dir被设为8,井且log.dirs指定了3个路径,那么总共需要24个线程。
num.recovery.threads.per.data.dir=1
############################# Internal Topic Settings #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
# 组元数据内部 topic "__consumer_offsets" 和 "__transaction_state" 的 复制系数
# 对于开发测试以外的任何测试,建议使用大于1的值来确保可用性,例如3。
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=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 excessive 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.
# 日志刷新政策
# 消息立即写入文件系统,但默认情况下我们只有fsync()才能同步
# 懒惰的操作系统缓存。以下配置控制将数据刷新到磁盘。
# 这里有一些重要的权衡:
# 1。持久性:如果您不使用复制,则可能会丢失未刷新的数据。
# 2。延迟:当刷新确实发生时,非常大的刷新间隔可能会导致延迟峰值,因为会有大量数据需要刷新。
# 3。吞吐量:冲洗通常是最昂贵的操作,并且小的冲洗间隔可能导致过多的搜索。
# 以下设置允许配置刷新策略以在一段时间后刷新数据或
# 每N条消息(或两者)。这可以在全局范围内完成,并在每个主题的基础上进行覆盖。
# 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.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 due to age
# 日志文件在可被删除前,最大的保存时间
log.retention.hours=168
# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
# 基于日志大小的保留策略。
# 默认的日志文件保留大小是 1073741824 bytes (即1 Gb),当日志文件从创建开始,一旦超过设置的日志保留大小,日志文件可能会被删除
#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
# 日志分片大小检查的周期时间
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 连接地址
zookeeper.connect=localhost:2181
# Timeout in ms for connecting to zookeeper
# Zookeeper 连接超时时间(毫秒)
zookeeper.connection.timeout.ms=18000
############################# Group Coordinator Settings #############################
# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
# The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
# The default value for this is 3 seconds.
# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
# However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
# 指定GroupCoordinator延迟初始使用者重新平衡的时间(以毫秒为单位)。官方建议生产环境设置为3秒
group.initial.rebalance.delay.ms=0
3、启动 Kafka 环境
注意:您的本地环境必须安装 Java 8+。
Apache Kafka 可以使用 ZooKeeper 或 KRaft 启动。要开始使用任一配置,可参考以下部分,但不要同时执行这两个部分。
3.1、Kafka with ZooKeeper
按正确顺序执行以下命令以启动所有服务:
# Start the ZooKeeper service
$ bin/zookeeper-server-start.sh config/zookeeper.properties
打开另一个终端会话并运行:
# Start the Kafka broker service
$ bin/kafka-server-start.sh config/server.properties
一旦所有服务都成功启动,您将拥有一个正在运行并可以使用的基本 Kafka 环境。
3.2、Kafka with KRaft
生成集群 UUID
$ KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
格式化日志目录
$ bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/kraft/server.properties
启动 Kafka Server
$ bin/kafka-server-start.sh config/kraft/server.properties
Kafka 服务器成功启动后,您将拥有一个正在运行并可以使用的基本 Kafka 环境。
4、终止 Kafka 环境(如果需要)
现在您已经完成了快速入门,可以随意拆除 Kafka 环境,或者继续尝试。
- 使用
Ctrl-C
停止生产者和消费者客户,如果您还没有这样做的话。 - 使用
Ctrl-C
停止 Kafka 代理。 - 最后,如果遵循 Kafka with ZooKeeper 部分,请使用
Ctrl-C
停止 ZooKeeper 服务器。
如果您还想删除本地 Kafka 环境的任何数据,包括您在此过程中创建的任何事件,请运行以下命令:
$ rm -rf /tmp/kafka-logs /tmp/zookeeper /tmp/kraft-combined-logs
5、开机启动
5.1、ZooKeeper 开机启动(如有独立 ZooKeeper ,则忽略)
[root@nacos nacos]# vi /lib/systemd/system/zookeeper.service
脚本内容:
[Unit]
Description=Zookeeper 3.x
After=network.target
[Service]
User=root
Type=forking
Environment=ZOO_LOG_DIR='/home/zookeeper/logs'
Environment=JAVA_HOME='/usr/local/java'
ExecStart=/usr/local/kafka_2.13-3.6.0/bin/zookeeper-server-start.sh -daemon /usr/local/kafka/config/zookeeper.properties
ExecStop=/usr/local/kafka_2.13-3.6.0/bin/zookeeper-server-stop.sh
Restart=on-failure
StartLimitInterval=60
StartLimitBurst=50
[Install]
WantedBy=multi-user.target
设置随机启动:
[root@nacos nacos]# systemctl daemon-reload
[root@nacos nacos]# systemctl enable zookeeper.service
[root@nacos nacos]# systemctl start zookeeper.service
[root@nacos nacos]# systemctl status zookeeper.service
[root@nacos nacos]# systemctl stop zookeeper.service
5.2、Kafka 开机启动
[root@kafka kafka]# vi /lib/systemd/system/kafka.service
脚本内容:
[Unit]
Description=Apache Kafka server (broker)
Documentation=http://kafka.apache.org/documentation.html
Requires=network.target
After=zookeeper.service
[Service]
User=root
Type=forking
Environment="JAVA_HOME=/usr/local/java"
ExecStart=/usr/local/kafka_2.13-3.6.0/bin/kafka-server-start.sh -daemon /usr/local/kafka_2.13-3.6.0/config/server.properties
ExecStop=/usr/local/kafka_2.13-3.6.0/bin/kafka-server-stop.sh
Restart=on-failure
StartLimitInterval=60
StartLimitBurst=50
[Install]
WantedBy=multi-user.target
设置随机启动:
[root@kafka kafka]# systemctl daemon-reload
[root@kafka kafka]# systemctl enable kafka.service
[root@kafka kafka]# systemctl start kafka.service
[root@kafka kafka]# systemctl status kafka.service
[root@kafka kafka]# systemctl stop kafka.service
作者:Jeebiz 创建时间:2023-01-26 13:42
最后编辑:Jeebiz 更新时间:2024-11-01 10:06
最后编辑:Jeebiz 更新时间:2024-11-01 10:06