Kafka是一种高吞吐量的分布式发布订阅消息系统。
Kafka启动方式有Zookeeper和Kraft,两种方式只能选择其中一种启动,不能同时使用。

【Kafka安装】
Kafka下载地址  点击进入:Index of /kafka

比如选择kafka_2.12-3.8.0.tgz

直接下载命令:wget https://downloads.apache.org/kafka/3.8.0/kafka_2.12-3.8.0.tgz

下载后进行解压:tar -xzf kafka_2.12-3.8.0.tgz

【Kafka启动】
启动Kafka本地环境需Java 8+以上, (yum安装Java,sudo yum install java-1.8.0-openjdk)
Kafka依赖Zookeeper,本文使用kafka自带Zookeeper进行启动。

1、启动Zookeeper 
./bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

2、启动Kafka
./bin/kafka-server-start.sh -daemon config/server.properties

3、查看端口启动
netstat -tpln


Zookeeper默认启动2181端口,可修改配置文件config/zookeeper.properties。

# the port at which the clients will connect
clientPort=2181

同时修改配置文件config/server.properties。
############################# 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

Kafka默认启动9092端口(需要耐心等待数分钟),可修改配置文件config/server.properties。
############################# 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
#listeners=PLAINTEXT://:9092


【Kafka使用】
进入kafka服务的bin目录执行创建topic主题
(老版本)./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic topic1
报错如下:
zookeeper is not a recognized option joptsimple.UnrecognizedOptionException: zookeeper is not a recognized option
kafka2.2+=的版本,已经不需要依赖zookeeper来查看/创建topic,新版本使用 --bootstrap-server替换老版本的 --zookeeper-server。

创建topic
./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic topic1

查看topic列表
./kafka-topics.sh --list --bootstrap-server 127.0.0.1:9092
查特定topic
./kafka-topics.sh --describe --topic TestTopic --bootstrap-server localhost:9092

生产者
./kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic topic1

消费者
./kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic topic1 --from-beginning

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐