以下是Kafka在Linux上的使用技巧,涵蓋安裝、配置、優化及監控等方面:
環境準備
# Ubuntu/Debian
sudo apt install openjdk-11-jdk
# CentOS/RHEL
sudo yum install java-11-openjdk
wget https://downloads.apache.org/kafka/3.6.1/kafka_2.13-3.6.1.tgz
tar -xzf kafka_2.13-3.6.1.tgz
mv kafka_2.13-3.6.1 /opt/kafka
核心配置
config/server.properties
:
broker.id
:唯一標識Broker(集群中需唯一)。log.dirs
:指定日志存儲路徑(建議SSD磁盤)。zookeeper.connect
:連接ZooKeeper集群(如localhost:2181
)。硬件與系統參數
-Xms8g -Xmx8g
),選擇G1垃圾回收器。num.network.threads
(建議CPU核心數×2)和num.io.threads
(建議CPU核心數×2)。Kafka配置優化
log.retention.hours
:設置日志保留時間(如168小時,即7天)。log.segment.bytes
:控制日志段大?。ㄈ?GB),避免頻繁切換。batch.size
:增大批量大?。ㄈ?6KB),減少網絡交互。linger.ms
:等待批量發送時間(如10ms),提升吞吐量。compression.type=gzip/snappy
,減少傳輸數據量。網絡優化
net.ipv4.tcp_nodelay=1
、net.core.rmem_max=16MB
)。ulimit -n 65535
。工具使用
log.retention.hours=168
),避免磁盤占滿。集群管理
bin/kafka-topics.sh --create --topic test-topic --partitions 3 --replication-factor 2 --bootstrap-server localhost:9092
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092
bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092
ssl.keystore.location
和ssl.truststore.location
。kafka-acls.sh
設置用戶權限。參考來源: