kafka在linux服务器上的安装配置及常用命令

安装kafka前先确认服务器上是否已经安装了Java和zookeeper

zookeeper的安装和配置

1. kafka的安装和配置

1.1 下载

wget https://mirrors.bfsu.edu.cn/apache/kafka/2.8.0/kafka_2.12-2.8.0.tgz

说明:kafka名中的2.12是Scala语言版本,后面的2.8.0是kafka版本,端口默认为9092

1.2 解压

tar -xzf kafka_2.12-2.8.0.tgz

1.3 修改配置文件server.properties

1.3.1 修改kafka端口

advertised.listeners=PLAINTEXT://your.host.name:9092

修改为

advertised.listeners=PLAINTEXT://IP:9092

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

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

1.3.2 修改日志存储路径

logDirs=/data/kafka/logs

1.3.3 修改zookeeper的集群配置

zookeeper.connect=IP1:2181,IP2:2181,IP3:2181

1.3.4 kafka集群配置

broker.id=0

默认为0,可以设置node-1:1,node-2:2,node-3:3

2. 启动Kafka

2.1 启动zookeeper

注意:必须先启动zookeeper再启动kafka,否则会报错,错误信息如下所示:

[2021-08-23 16:53:45,825] INFO Socket error occurred: localhost/0:0:0:0:0:0:0:1:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
[2021-08-23 16:53:46,927] INFO Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)

bin/zkServer.sh start

2.2 启动Kafka

bin/kafka-server-start.sh config/server.properties
bin/kafka-server-start.sh -daemon config/server.properties(-daemon以后台的方式启动)

查看9092端口状态,确保服务已经启动

2.3检查测试

2.3.1连接zookeeper

bin/zkCli.sh -server localhost:2181

zookeeper集群建好之后,通过“ls /”出来的只有zookeeper,连接kafka使用后,/ 下面多了很多信息,其中通过查看/brokers/ids可以发现已经检查到了已经安装的三台kafka的broker.id[1,2,3]。

[zk: 192.168.1.21:2181(CONNECTED) 2] ls /          
 [admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zookeeper]
 [zk: 192.168.1.21:2181(CONNECTED) 2] ls /brokers
 [ids, seqid, topics]
 [zk: 192.168.1.21:2181(CONNECTED) 3] ls /brokers/ids
 [1, 2, 3]
 [zk: 192.168.1.21:2181(CONNECTED) 4]
 

3. kafka常用命令

3.1 创建topic

bin/kafka-topics.sh --create --zookeeper IP1:2181,IP2:2181,IP3:2181 --replication-factor 3 --partitions 3 --topic gw651_upstream_drainage

replication-factor:用来设置主题的副本数。每个主题可以有多个副本,副本位于集群中不同的broker上,也就是说副本的数量不能超过broker的数量,否则创建主题时会失败。
partitions:主题分区数。kafka通过分区策略,将不同的分区分配在一个集群中的broker上,一般会分散在不同的broker上,当只有一个broker时,所有的分区就只分配到该Broker上。消息会通过负载均衡发布到不同的分区上,消费者会监测偏移量来获取哪个分区有新数据,从而从该分区上拉取消息数据。

3.2 查看topics列表

bin/kafka-topics.sh --list --zookeeper IP1:2181,IP2:2181,IP3:2181

3.3 生产指定topic消息

bin/kafka-console-producer.sh --broker-list {kafka连接地址} --topic test

3.4 消费指定topic消息

bin/kafka-console-consumer.sh --bootstrap-server {kafka连接地址} --topic test --from-beginning

3.5 删除指定topic

bin/kafka-topics.sh --delete --zookeeper IP1:2181,IP2:2181,IP3:2181 --topic test

3.6 查询消费组

bin/kafka-consumer-groups.sh --bootstrap-server {kafka连接地址} --list

3.7 查询消费组详情

kafka-consumer-groups.sh --bootstrap-server {kafka连接地址} --describe --group {消费组}

3.8 删除消费组

kafka-consumer-groups.sh --bootstrap-server {kafka连接地址} --delete --group {消费组}

4. server.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 #############################

##################################################################################
#  broker就是一个kafka的部署实例,在一个kafka集群中,每一台kafka都要有一个broker.id
#  并且,该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 = 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=/data/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所对应的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=IP1:2181,IP2:2181,IP3:2181

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一些常用的 Linux Kafka 命令: 1. 启动 Kafka: ``` bin/kafka-server-start.sh config/server.properties ``` 2. 创建主题: ``` bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic my_topic ``` 3. 查看主题列表: ``` bin/kafka-topics.sh --list --zookeeper localhost:2181 ``` 4. 生产者发送消息: ``` bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic ``` 5. 消费者消费消息: ``` bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_topic --from-beginning ``` 6. 查看主题详情: ``` bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my_topic ``` 以上是一些常用的 Kafka 命令,你可以根据实际需求进行相应的使用。 ### 回答2: Kafka是一个分布式流处理平台,广泛用于构建高性能、可扩展的实时数据管道。下面是一些常见的Linux Kafka命令: 1. 启动Kafka服务器:使用命令`bin/kafka-server-start.sh config/server.properties`来启动Kafka服务器。在启动之前,你可能需要先配置服务器属性。 2. 创建一个主题:使用命令`bin/kafka-topics.sh --create --topic <topic-name> --bootstrap-server <bootstrap-server> --partitions <num-partitions> --replication-factor <replication-factor>`来创建一个新的主题。在命令中,你需要指定主题的名称、分区数和复制因子。 3. 查看主题列表:使用命令`bin/kafka-topics.sh --list --bootstrap-server <bootstrap-server>`来查看当前Kafka服务器中的所有主题。 4. 查看主题详情:使用命令`bin/kafka-topics.sh --describe --topic <topic-name> --bootstrap-server <bootstrap-server>`来查看特定主题的详细信息,包括它的分区、领导者等信息。 5. 创建一个生产者:使用命令`bin/kafka-console-producer.sh --broker-list <broker-list> --topic <topic-name>`来创建一个简单的控制台生产者,用于向指定主题发送消息。 6. 创建一个消费者:使用命令`bin/kafka-console-consumer.sh --bootstrap-server <bootstrap-server> --topic <topic-name> --from-beginning`来创建一个简单的控制台消费者,用于从指定主题接收消息。 这些只是一些常见的Kafka命令,Kafka还有很多其他功能和命令供使用。你可以通过查阅官方文档或者在命令行中输入`bin/kafka-topics.sh`来获取更多详细的命令和选项。 ### 回答3: Kafka是一个开源的分布式流媒体平台,用于构建高性能、可扩展的实时数据流应用程序。Kafka提供了一组命令行工具,用于管理和操作Kafka集群。下面是一些常用的Kafka命令及其说明: 1. kafka-topics.sh:用于创建、查看和操作主题(topics)。可以使用该命令创建新的主题、查看现有主题的详细信息,并执行其他与主题相关的操作。例如,创建一个新的主题可以使用以下命令: kafka-topics.sh --bootstrap-server <kafka服务器地址> --create --topic <主题名称> --partitions <分区数> --replication-factor <复制因子> 2. kafka-console-producer.sh:用于从命令行中发送消息到Kafka主题中。可以使用该命令将指定的消息发送到指定的主题中。例如,发送一条消息可以使用以下命令: kafka-console-producer.sh --bootstrap-server <kafka服务器地址> --topic <主题名称> 3. kafka-console-consumer.sh:用于从Kafka主题中消费消息,并将其打印到命令行中。可以使用该命令订阅指定的主题,并消费该主题中的消息。例如,消费主题中的消息可以使用以下命令: kafka-console-consumer.sh --bootstrap-server <kafka服务器地址> --topic <主题名称> --from-beginning 4. kafka-configs.sh:用于管理Kafka集群的配置。可以使用该命令查看和更新Kafka配置的详细信息,例如,查看或更新集群的最大存储容量、最大连接数等。例如,查看集群的配置可以使用以下命令: kafka-configs.sh --bootstrap-server <kafka服务器地址> --describe --entity-type brokers --entity-name <broker-id> 5. kafka-consumer-groups.sh:用于管理和监控Kafka消费者组。可以使用该命令查看消费者组的详细信息,例如,消费者组的偏移量、消费者组的成员等。例如,查看消费者组的信息可以使用以下命令: kafka-consumer-groups.sh --bootstrap-server <kafka服务器地址> --group <消费者组名称> --describe 这些命令是Kafka提供的主要工具,通过这些命令可以对Kafka集群中的主题、消息、配置和消费者组进行管理和操作,从而实现高效的数据流处理和分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值