在CentOS上配置Kafka涉及多个步骤,包括安装Java、下载Kafka、配置Kafka以及启动Kafka服务。以下是详细的步骤指南:
Kafka需要Java运行环境,因此首先需要安装Java。
sudo yum install java-1.8.0-openjdk-devel
验证Java安装:
java -version
从Apache Kafka官方网站下载最新版本的Kafka。
wget https://downloads.apache.org/kafka/3.4.0/kafka_2.13-3.4.0.tgz
解压下载的文件:
tar -xzf kafka_2.13-3.4.0.tgz
mv kafka_2.13-3.4.0 /opt/kafka
进入Kafka目录并进行基本配置。
cd /opt/kafka/config
编辑server.properties
文件,配置Kafka服务器的基本参数。
# The directory under which the log files will be stored.
log.dirs=/tmp/kafka-logs
# The port at which the server will listen for client connections.
listeners=PLAINTEXT://:9092
# The address the server uses to listen to clients.
advertised.listeners=PLAINTEXT://your_server_ip:9092
# The number of partitions for the default topic.
num.partitions=1
# The default number of partition replicas.
default.replication.factor=1
# The minimum age of a log file to be eligible for deletion due to age.
log.retention.hours=168
# The maximum size of the log files.
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
将your_server_ip
替换为你的服务器IP地址。
Kafka使用Zookeeper进行集群管理,因此需要配置Zookeeper。
# The directory where ZooKeeper data will be stored.
dataDir=/tmp/zookeeper
# The client port at which ZooKeeper connections will be accepted.
clientPort=2181
# The maximum number of client connections.
maxClientCnxns=0
在Kafka目录下启动Zookeeper。
cd /opt/kafka
bin/zookeeper-server-start.sh config/zookeeper.properties &
在另一个终端窗口中启动Kafka服务器。
bin/kafka-server-start.sh config/server.properties &
创建一个Kafka主题以进行测试。
bin/kafka-topics.sh --create --topic test --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1
发送和接收消息以验证Kafka是否正常工作。
bin/kafka-console-producer.sh --topic test --bootstrap-server localhost:9092
在提示符下输入消息并按回车键发送。
bin/kafka-console-consumer.sh --topic test --from-beginning --bootstrap-server localhost:9092
你应该能够看到之前发送的消息。
确保防火墙允许Kafka和Zookeeper的端口。
sudo firewall-cmd --zone=public --add-port=9092/tcp --permanent
sudo firewall-cmd --zone=public --add-port=2181/tcp --permanent
sudo firewall-cmd --reload
如果你打算配置一个多节点的Kafka集群,需要在每台服务器上进行类似的配置,并确保advertised.listeners
指向正确的IP地址。
通过以上步骤,你应该能够在CentOS上成功配置和运行Kafka。