Apache Kafka以其有效处理实时数据流的能力而闻名。它是日志聚合、数据管道和事件驱动应用程序等方案的完美选择。
在 Ubuntu 22.04 LTS Jammy Jellyfish 上安装 Apache Kafka
第 1 步。首先,通过在终端中运行以下命令,确保所有系统软件包都是最新的。apt
<span class="pln">sudo apt update sudo apt upgrade</span>
第 2 步。Java 开发工具包 (JDK) 安装。
Kafka 依赖于 Java,因此请确保您已安装 JDK。让我们验证它的存在并设置环境变量。JAVA_HOME
通过运行以下命令确认是否安装了 Java:
<span class="pln">java </span><span class="pun">-</span><span class="pln">version</span>
如果没有,请使用以下命令安装它:
<span class="pln">sudo apt install openjdk</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="pln">jdk</span>
要设置环境变量,请打开文件:JAVA_HOME
.bashrc
<span class="pln">nano </span><span class="pun">~/.</span><span class="pln">bashrc</span>
在文件末尾添加以下行(替换为您的 Java 安装路径):<your_java_home_path>
<span class="kwd">export</span><span class="pln"> JAVA_HOME</span><span class="pun">=<</span><span class="pln">your_java_home_path</span><span class="pun">></span>
保存文件并运行:
<span class="pln">source </span><span class="pun">~/.</span><span class="pln">bashrc</span>
第 3 步。在 Ubuntu 22.04 上安装 Apache Kafka。
导航到官方 Apache Kafka 下载页面,选择所需的版本,然后复制其 URL。在您的终端中,用于下载 Kafka:wget
<span class="pln">wget https</span><span class="pun">:</span><span class="com">//downloads.apache.org/kafka/3.5.1/kafka-3.5.1-src.tgz</span>
提取下载的存档:
<span class="pln">tar </span><span class="pun">-</span><span class="pln">xzf kafka</span><span class="pun">-</span><span class="lit">3.5</span><span class="pun">.</span><span class="lit">1</span><span class="pun">-</span><span class="pln">src</span><span class="pun">.</span><span class="pln">tgz</span>
第 4 步。配置 Apache Kafka。
首先,创建一个 Kafka 配置目录:
<span class="pln">sudo mkdir </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">kafka</span>
编辑 Kafka 文件:server.properties
<span class="pln">sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">/</span><span class="pln">server</span><span class="pun">.</span><span class="pln">properties</span>
下面是要配置的一些基本属性:
- 经纪商编号:确保每个代理都有一个唯一的 ID。
- 端口配置:默认端口为纯文本的默认端口 9092 和 SSL 的默认端口 9093。
- 日志目录:设置 Kafka 日志的路径。
- 动物园管理员连接:指定 Zookeeper 服务器地址。
第5步。启动 Kafka 服务器。
要在后台启动 Kafka,请运行:
<span class="pun">.</span><span class="str">/kafka_3.5.1/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">-</span><span class="pln">server</span><span class="pun">-</span><span class="pln">start</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-</span><span class="pln">daemon </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">/</span><span class="pln">server</span><span class="pun">.</span><span class="pln">properties</span>
您可以验证 Kafka 是否正在运行:
<span class="pun">.</span><span class="str">/kafka_3.5.1/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">-</span><span class="pln">server</span><span class="pun">-</span><span class="pln">stop</span><span class="pun">.</span><span class="pln">sh</span>
第 6 步。创建卡夫卡主题。
主题对于 Kafka 中的数据组织至关重要。要创建主题,请使用脚本:kafka-topics.sh
<span class="pun">.</span><span class="str">/kafka_3.5.1/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">-</span><span class="pln">topics</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">--</span><span class="pln">create </span><span class="pun">--</span><span class="pln">topic test</span><span class="pun">-</span><span class="pln">topic </span><span class="pun">--</span><span class="pln">partitions </span><span class="lit">3</span> <span class="pun">--</span><span class="pln">replication</span><span class="pun">-</span><span class="pln">factor </span><span class="lit">1</span> <span class="pun">--</span><span class="pln">bootstrap</span><span class="pun">-</span><span class="pln">server localhost</span><span class="pun">:</span><span class="lit">9092</span>
步骤 7.测试Apache Kafka。
现在您的 Kafka 集群已启动并运行,让我们通过设置生产者和使用者来测试它。
创建 Kafka 生产者:
<span class="pun">.</span><span class="str">/kafka_3.5.1/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">-</span><span class="pln">console</span><span class="pun">-</span><span class="pln">producer</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">--</span><span class="pln">topic test</span><span class="pun">-</span><span class="pln">topic </span><span class="pun">--</span><span class="pln">bootstrap</span><span class="pun">-</span><span class="pln">server localhost</span><span class="pun">:</span><span class="lit">9092</span>
创建 Kafka 消费者:
<span class="pun">.</span><span class="str">/kafka_3.5.1/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">-</span><span class="pln">console</span><span class="pun">-</span><span class="pln">consumer</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">--</span><span class="pln">topic test</span><span class="pun">-</span><span class="pln">topic </span><span class="pun">--</span><span class="pln">bootstrap</span><span class="pun">-</span><span class="pln">server localhost</span><span class="pun">:</span><span class="lit">9092</span> <span class="pun">--</span><span class="kwd">from</span><span class="pun">-</span><span class="pln">beginning</span>
第8步。管理Apache Kafka。
启动和停止卡夫卡:
<span class="pun">.</span><span class="str">/kafka_3.5.1/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">-</span><span class="pln">server</span><span class="pun">-</span><span class="pln">stop</span><span class="pun">.</span><span class="pln">sh</span>
要再次启动卡夫卡:
<span class="pun">.</span><span class="str">/kafka_3.5.1/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">-</span><span class="pln">server</span><span class="pun">-</span><span class="pln">start</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">kafka</span><span class="pun">/</span><span class="pln">server</span><span class="pun">.</span><span class="pln">properties</span>
第9步。排查常见问题。
卡夫卡服务器错误
- 检查 Kafka 日志目录中的日志。
- 确保正确配置 Kafka 属性。
卡夫卡生产者和消费者问题
- 验证创建者和使用者配置是否匹配。
- 检查网络或防火墙问题。
资源约束和性能调优
- 使用 和 等工具监视系统资源。
top
htop
- 调整 Kafka 配置以获得最佳性能。
感谢您使用本教程在 Ubuntu 系统上安装 Apache Kafka。有关其他帮助或有用信息,我们建议您查看 Apache Kafka 官方网站。