1.前言
在前面的文章中讲了单机版zookeeper的搭建,现在在单机版的基础上搭建集群。默认单机版的搭建好了。我这里只有一台服务器,所以在单机上搭建的为集群
2.将单机安装好的zookeeper复制三份
apache-zookeeper-3.5为单机版安装好的,另外三份是复制的,且重命名.三个zookeeper的端口分别是2181、2182、2183
3.修改zookeeper-2181的配置文件
参数dataDir修改
改成对应的路径dataDir=/usr/local/programs/zookeeper/zookeeper-2181/data
参数clientPort修改:修改端口号
clientPort=2181
添加下列配置:
#集群配置信息
#server.A=B:C:D
#A:是一个数字,表示这个是服务器的编号
#B:是这个服务器的ip地址
#C:Zookeeper服务器之间的通信端口
#D:Leader选举的端口
server.1=59.111.138.4.:2287:3387
server.2=59.111.138.4:2288:3388
server.3=59.111.138.4:2289:3389
注意:由于我这里三个zookeeper在同一个服务器上面,所以ip是一样的,也可以写成0.0.0.0
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/usr/local/programs/zookeeper/zookeeper-2181/data # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 #集群配置信息 #server.A=B:C:D #A:是一个数字,表示这个是服务器的编号 #B:是这个服务器的ip地址 #C:Zookeeper服务器之间的通信端口 #D:Leader选举的端口 server.1=0.0.0.0:2287:3387 server.2=0.0.0.0:2288:3388 server.3=0.0.0.0:2289:3389
4.myid文件
在data文件夹下新建文件,文件名 myid,删除data下其他的文件
在文件中加入数字1,这个1对应的是server.1=0.0.0.0:2287:3387中server.后面的数字1
5. zookeeper-2182和2183重复上面3/4两步的操作
6.到各个的bin目录下启动服务器
./zkServer.sh start 启动服务器
./zkServer.sh status 查看服务器状态
7.启动可能出现的问题
启动服务器后,./zkServer.sh status发现服务器没有起来
查看log文件夹下的日志文件
7.1可能遇到的问题1
服务器实际是启动了,只是没有连接到另外两台,因为另外两台还没启动。
解决方案:先不管服务器的状态,一个一个的把zookeeper启动(./zkServer.sh start),最后一个服务器启动了,等待一会儿,再查看这台服务器的状态(./zkServer.sh status),发现,最后一个正常启动了,再回去吧之前的服务器一个个的重新启动(./zkServer.sh restart),再查看状态,发现正常启动,问题解决
7.2可能问题2:服务器防火墙,关闭即可
7.3可能问题3:如果用的阿里云服务器,端口需要开放
7.4可能问题4:
8080端口占用,因为Zookeeper AdminServer,默认使用8080端口(这个被占用了可以先不处理)
8.测试
在2181出创建节点 你好
在2182出去获取
获取到了,成功
peerType=observer
server.3=192.168.60.130:2289:3389:observer
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了