要调整Linux Kafka配置以适应高并发,可以从以下几个方面进行优化:
kafka-topics.sh
脚本增加主题的分区数量。kafka-topics.sh --zookeeper <zookeeper_host:port> --alter --topic <topic_name> --partitions <new_partition_count>
kafka-topics.sh
脚本调整副本因子。kafka-topics.sh --zookeeper <zookeeper_host:port> --alter --topic <topic_name> --replication-factor <new_replication_factor>
log.flush.interval.messages
和log.flush.interval.ms
参数。log.flush.interval.messages=100000
log.flush.interval.ms=1000
message.max.bytes
和replica.fetch.max.bytes
参数。message.max.bytes=104857600 # 100MB
replica.fetch.max.bytes=104857600 # 100MB
# 调整TCP缓冲区大小
sysctl -w net.core.rmem_max=16777216
sysctl -w net.core.wmem_max=16777216
sysctl -w net.ipv4.tcp_rmem="4096 87380 16777216"
sysctl -w net.ipv4.tcp_wmem="4096 65536 16777216"
# 在kafka-server-start.sh中添加或修改以下参数
export KAFKA_HEAP_OPTS="-Xmx8g -Xms8g -XX:MetaspaceSize=1g -XX:MaxMetaspaceSize=1g"
compression.type=gzip
以下是一个示例的server.properties
文件的部分配置:
# Broker ID
broker.id=1
# 监听地址
listeners=PLAINTEXT://:9092
# Zookeeper连接
zookeeper.connect=localhost:2181
# 日志目录
log.dirs=/tmp/kafka-logs
# 分区数量
num.partitions=10
# 副本因子
default.replication.factor=3
# 消息最大大小
message.max.bytes=104857600
# 日志刷新间隔
log.flush.interval.messages=100000
log.flush.interval.ms=1000
# 堆内存大小
KAFKA_HEAP_OPTS="-Xmx8g -Xms8g -XX:MetaspaceSize=1g -XX:MaxMetaspaceSize=1g"
# 启用压缩
compression.type=gzip
通过以上步骤,可以显著提高Kafka在高并发环境下的性能和稳定性。记得在调整配置后重启Kafka Broker以使更改生效。