zookeeper对外提供分布式协调服务,我们的分布式应用,通过zookeeper可以很轻松的实现协调调度
zookeeper经常组成集群提供服务,如下图所示,集群内各主机之间互联互通,通过选举产生leader,即使有主机因意外宕机,剩余的主机也能选举出一台作为leader继续对外提供服务
更多细节可以查看apach的官方描述
我们有时候自己本地调试服务需要zookeeper提供服务,但是有没有太多资源使用,可以选择安装单机版zookeeper
准备工作:安装Java环境,配置path
1.下载zookeeper安装包,本次使用3.6.0版本,可以去官网下载自己需要的版本
wget https://downloads.apache.org/zookeeper/zookeeper-3.6.0/apache-zookeeper-3.6.0-bin.tar.gz
2.解压tar包
tar -zxvf apache-zookeeper-3.6.0-bin.tar.gz
3.进入解压好的文件夹,并复制一份配置文件
cd /usr/local/zookeeper/apache-zookeeper-3.6.0/conf
cp zoo_sample.cfg zoo.cfg
4.编辑配置文件
vi coo.cfg
配置文件如下,修改dataDir和dataLogDir
其中dataDir里需要创建一个myid文件,里面配置的当前服务的id编号,本例子里把myid设为了2181
dataLogDir里是日志文件,如果缺少目录文件夹及文件,请自己补全
# 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/zookeeper/apache-zookeeper-3.6.0/data
dataLogDir=/usr/local/zookeeper/apache-zookeeper-3.6.0/logs
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
5.启动zookeeper
/usr/local/zookeeper/apache-zookeeper-3.6.0/bin/zkServer.sh start
启动成功显示如下
/usr/local/jdk1.8.0_251/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.6.0/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
6.如果报Java环境的错误就进入/usr/local/zookeeper/apache-zookeeper-3.6.0/bin文件夹修改启动文件
在#!/usr/bin/env bash下添加jdk路径,如果启动不报错可以不配置这项
#!/usr/bin/env bash
export=/usr/local/jdk1.8.0_251/bin:$PATH