Zookeeper的搭建方式
- 单机模式
- 单机伪集群模式 ⼀台服务器上运⾏多个Zookeeper 实例;
- 集群模式 Zookeeper运⾏于⼀个集群上,适合⽣产环境,这个计算机集群被称为⼀个“集合体”
- 如果在分布式的生产环境中使用ZooKeeper,推荐使用大于3的奇数节点个数搭建集群,这样能提升系统的可用性。
集群模式、伪集群模式安装方式都差不多只是真正的集群模式需要使用不同的IP。单机模式则更简单了,只需要取消掉zoo.cfg配置文件中的集群配置就行。
二、伪集群模式安装
Zookeeper 官网下载安装包
- 官网地址:https://zookeeper.apache.org/
- 下载页面:https://zookeeper.apache.org/releases.html
- 下载最新稳定版本,下载页面上带有 lastest stable release 备注的连接就是最新稳定版。
- 有源代码发行版和二进制发行版,直接下载二进制发行版就行
2.1 解压后的目录
[root@localhost zookeeper-3.7.1-01]# ll
总用量 36
drwxr-xr-x. 2 root root 4096 5月 7 2022 bin
drwxr-xr-x. 2 root root 70 7月 1 10:40 conf
drwxr-xr-x. 5 root root 4096 5月 7 2022 docs
drwxr-xr-x. 2 root root 4096 6月 29 17:42 lib
-rw-r--r--. 1 root root 11358 5月 7 2022 LICENSE.txt
drwxr-xr-x. 2 root root 88 6月 30 09:51 logs
-rw-r--r--. 1 root root 2084 5月 7 2022 NOTICE.txt
-rw-r--r--. 1 root root 2214 5月 7 2022 README.md
-rw-r--r--. 1 root root 3570 5月 7 2022 README_packaging.md
2.2 从模板创建配置文件
- 进入解压目录下的conf目录下,里面包含 zoo_sample.cfg 文件
- 将zoo_sample.cfg 改名为 zoo.cfg 因为zookeeper默认加载zoo.cfg文件。如果需要使用其他名称,可以使用如下传递配置文件参数:
zkServer.sh start your_config zkServer.sh stop your_config zkServer.sh status your_config
2.3 zoo.cfg 配置文件修改
# 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= /root/Desktop/studySoftWare/zookeeper-3.7.1-localCluster/zookeeper-3.7.1-01/data
# 配置日志存储路径
dataLogDir= /root/Desktop/studySoftWare/zookeeper-3.7.1-localCluster/zookeeper-3.7.1-01/data/logs
# 客户端通信端口
# the port at which the clients will connect
clientPort=15881
# 集群配置
# server.服务器ID=服务器IP地址:服务器之间通信端⼝:服务器之间投票选举端⼝
server.1=localhost:16881:17881
server.2=localhost:16882:17882
server.3=localhost:16883:17883
# 管理平台端口配置
admin.serverPort:18881
# 最大客户端连接数
# 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.
#
# https://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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
2.3.1 单机需要的配置
- dataDir= 数据保存路径
- dataLogDir= 事务日志文件保存路径
- clientPort= 供客户端连接的监听端口
- admin.serverPort=管理平台端口配置,对于3.6以后的版本会默认占用8080启动一个内嵌的管理控制台。可以使用这个配置修改端口。或者启动时添加参数,禁用该控制台。-Dzookeeper.admin.enableServer=false
2.3.3 集群模式额外需要的配置
- 集群机器和对应通信端口配置。这些端口就需要和上面的 admin.serverPort 还有clientPort 不同,不然会导致启动失败。单机安装伪集群时,这些端口都不能重复。
# server.服务器ID=服务器IP地址:服务器之间通信端⼝:服务器之间投票选举端⼝
server.1=localhost:16881:17881
server.2=localhost:16882:17882
server.3=localhost:16883:17883
- 在配置的dataDir 路径下,根据上面配置的集群服务id新增一个myid的文件,文件内容就是服务器id号即可。
[root@localhost data]# pwd
/studySoftWare/zookeeper-3.7.1-localCluster/zookeeper-3.7.1-01/data
[root@localhost data]# cat myid
1
[root@localhost data]#
2.4 启动服务
进入安装目录下的bin目录 ./zkServer start
如果是部署了多个服务的集群,则依次启动每个服务。