kafka

160 阅读8分钟

1. 先到官网下载kafka

链接地址:www.apache.org/dyn/closer.…

2. tar命令解压包

image.png

[root@localhost kafka_2.13-2.4.1]# ll
总用量 56
drwxr-xr-x. 3 root root  4096 3月   3 08:35 bin         #执行脚本
drwxr-xr-x. 2 root root  4096 4月  14 10:58 config      #配置文件
drwxr-xr-x. 2 root root     6 4月  14 10:57 data        #存储日志文件的目录
drwxr-xr-x. 2 root root  8192 4月  14 10:52 libs        #依赖jar包
-rw-r--r--. 1 root root 32216 3月   3 08:32 LICENSE
drwxr-xr-x. 2 root root   182 4月  14 11:34 logs        #日志
-rw-r--r--. 1 root root   337 3月   3 08:32 NOTICE
drwxr-xr-x. 2 root root    44 3月   3 08:35 site-docs   #文档
config目录:
[root@localhost config]# ll
总用量 76
-rw-r--r--. 1 root root  906 3月   3 08:32 connect-console-sink.properties
-rw-r--r--. 1 root root  909 3月   3 08:32 connect-console-source.properties
-rw-r--r--. 1 root root 5321 3月   3 08:32 connect-distributed.properties
-rw-r--r--. 1 root root  883 3月   3 08:32 connect-file-sink.properties
-rw-r--r--. 1 root root  881 3月   3 08:32 connect-file-source.properties
-rw-r--r--. 1 root root 2247 3月   3 08:32 connect-log4j.properties
-rw-r--r--. 1 root root 1539 3月   3 08:32 connect-mirror-maker.properties
-rw-r--r--. 1 root root 2262 3月   3 08:32 connect-standalone.properties
-rw-r--r--. 1 root root 1221 3月   3 08:32 consumer.properties
-rw-r--r--. 1 root root 4675 3月   3 08:32 log4j.properties
-rw-r--r--. 1 root root 1925 3月   3 08:32 producer.properties
-rw-r--r--. 1 root root 8889 4月  14 11:23 server.properties    #服务配置文件
-rw-r--r--. 1 root root 1032 3月   3 08:32 tools-log4j.properties
-rw-r--r--. 1 root root 1169 3月   3 08:32 trogdor.conf
-rw-r--r--. 1 root root 1205 3月   3 08:32 zookeeper.properties
# 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 #############################

# The id of the broker. This must be set to a unique integer for each broker.
# 经纪人的id。必须为每个代理将其设置为唯一整数。
broker.id=0

############################# 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 = listener_name://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().
# 在项目中使用,别忘了打开注释 - 并修改成自己的ip
#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
# 套接字服务器使用的发送缓冲区(sou SNDBUF)
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
# 套接字服务器使用的接收缓冲区(SO RCVBUF)
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
# 套接字服务器将接受的请求的最大大小(防止OOM)
socket.request.max.bytes=104857600


############################# Log Basics #############################

# A comma separated list of directories under which to store log files
# 以逗号分隔的存储日志文件的目录列表 - 自己创建的目录
log.dirs=/root/kafka_2.13-2.4.1/data

# 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.
# 每个主题的默认日志分区数。更多的分区允许使用更大的并行性,但这也会导致代理之间有更多的文件。
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阵列中的安装,建议增加此值。
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.
# 对于组元数据内部主题“消费者补偿”和“交易状态”的复制因子,对于开发测试以外的任何内容,建议使用大于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.

# 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.
# 日志的基于大小的保留策略。除非剩余的段低于log.retention.bytes,否则将从日志中删除段。功能独立于log.retention.hours。
#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.connect=localhost:2181

# Timeout in ms for connecting to zookeeper
# 连接到zookeeper超时(毫秒)
zookeeper.connection.timeout.ms=6000


############################# 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将延迟初始使用者重新平衡的时间(毫秒)。
# 当新成员加入组时,重新平衡将被group.initial.rebalance.delay.ms的值进一步延迟,最大值为max.poll.interval.ms。
# 默认值为3秒。
# 我们在这里将其重写为0,因为它可以为开发和测试提供更好的开箱即用体验。
# 然而,在生产环境中,3秒的默认值更合适,因为这将有助于避免在应用程序启动期间不必要的、潜在的昂贵的重新平衡。
group.initial.rebalance.delay.ms=0

###启动服务

启动一个 Kafka 服务。命令行操作如下:
[root@localhost bin]# ./kafka-server-start.sh ../config/server.properties 
[2020-04-14 14:09:16,882] INFO [TransactionCoordinator id=0] Starting up. (kafka.coordinator.transaction.TransactionCoordinator)
[2020-04-14 14:09:16,894] INFO [TransactionCoordinator id=0] Startup complete. (kafka.coordinator.transaction.TransactionCoordinator)
[2020-04-14 14:09:16,924] INFO [Transaction Marker Channel Manager 0]: Starting (kafka.coordinator.transaction.TransactionMarkerChannelManager)
[2020-04-14 14:09:17,005] INFO [ExpirationReaper-0-AlterAcls]: Starting (kafka.server.DelayedOperationPurgatory$ExpiredOperationReaper)
[2020-04-14 14:09:17,137] INFO [SocketServer brokerId=0] Started data-plane processors for 1 acceptors (kafka.network.SocketServer)
[2020-04-14 14:09:17,141] INFO [/config/changes-event-process-thread]: Starting (kafka.common.ZkNodeChangeNotificationListener$ChangeEventProcessThread)
[2020-04-14 14:09:17,171] INFO Kafka version: 2.4.1 (org.apache.kafka.common.utils.AppInfoParser)
[2020-04-14 14:09:17,171] INFO Kafka commitId: c57222ae8cd7866b (org.apache.kafka.common.utils.AppInfoParser)
[2020-04-14 14:09:17,171] INFO Kafka startTimeMs: 1586844557138 (org.apache.kafka.common.utils.AppInfoParser)
[2020-04-14 14:09:17,173] INFO [KafkaServer id=0] started (kafka.server.KafkaServer)
启动成功.
  • 默认情况下,Kafka 日志文件所在地址为 logs/server.log 。如果想要自定义,可以通过 config/log4j.properties 配置文件来进行修改。

测试kafka 消息发送接收

  • 创建一个Topic 为 demoTopic
[root@localhost bin]# ./kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic demoTopic
Created topic demoTopic.
  • 发送消息
[root@localhost bin]# ./kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic demoTopic
每输入一行,回车,都会发送一条消息
>test1
>test2
>test3
>test4
  • 消费消息
[root@localhost bin]# ./kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic demoTopic --from-beginning
test1
test2
test3
test4

kafka管理控制台

[root@localhost ~]# wget https://github.com/wolfogre/kafka-manager-docker/releases/download/3.0.0.4/cmak-3.0.0.4.zip
  • 解压
    1. 修改conf目录下application.conf kafka-manager.zkhosts="127.0.0.1:2181"
    2. 切换到bin目录 ./kafka-manager 启动
    3. 访问地址 http://192.168.2.26:9000/

总结