一、硬件配置优化
dataDir(快照)和dataLogDir(事务日志)。二、操作系统层优化
/etc/sysctl.conf添加vm.swappiness=0(禁用交换),并执行sysctl -p生效;或通过swapoff -a临时关闭。/etc/security/limits.conf,添加zookeeper soft nofile 65536、zookeeper hard nofile 65536(假设Zookeeper以zookeeper用户运行),并修改/etc/pam.d/common-session添加session required pam_limits.so。/etc/sysctl.conf添加:net.core.somaxconn=65536(最大连接队列长度)、net.ipv4.tcp_max_syn_backlog=65536(SYN队列长度)、net.ipv4.tcp_tw_reuse=1(复用TIME-WAIT连接),执行sysctl -p生效。三、Zookeeper配置参数优化
initLimit是Follower与Leader初始连接的超时时间(默认5tickTime),syncLimit是Follower与Leader同步数据的超时时间(默认2tickTime)。若集群节点分布在高延迟网络(如跨机房),可适当增大(如initLimit=10、syncLimit=5)。.txn)和快照文件(.snapshot),避免磁盘空间耗尽。设置autopurge.snapRetainCount=5(保留最新5份快照)、autopurge.purgeInterval=1(每天凌晨执行清理),需在zoo.cfg中添加并重启服务。/data/zookeeper/snapshot、/data/zookeeper/log),减少磁盘I/O竞争,提升写入性能。zoo.cfg中添加jute.maxbuffer=10485760(10MB)。四、JVM层优化
-Xms4g -Xmx4g),避免堆内存波动。在zkEnv.sh(或zkServer.sh)中添加:export JVMFLAGS="-Xms4g -Xmx4g"。-XX:+UseG1GC,并设置最大GC停顿时间(如-XX:MaxGCPauseMillis=200),减少对客户端请求的影响。五、网络层优化
六、应用程序使用优化
multi API将多个操作(如创建、更新Znode)合并为一次请求,减少网络往返次数(如批量创建10个Znode,原本需10次请求,批量后仅需1次)。zoo.cfg中添加peerType=observer),减少Leader的同步负担,提升集群性能。七、监控与维护
avg_latency、事务处理量packets_received/packets_sent、内存使用jvm_memory_used、连接数num_alive_connections),及时发现性能瓶颈(如延迟升高可能因磁盘I/O瓶颈)。/var/log/zookeeper/zookeeper.log),查找异常(如ConnectionLoss表示客户端连接丢失、SyncLimit exceeded表示同步超时),定位问题根源(如网络延迟高、节点负载不均)。