标准:
1.根据拆分的思想进行分布式的设计
2.当异常发生时,可以自动的故障迁移无需人为的干预
服务中心
zookeeper:
单台搭建:
安装:下载zookeeper
配置:
解压后进入zookeeper根目录
创建文件夹mkdir data log
[root@127 zookeeper]# cd data
[root@127 data]# pwd
/usr/local/src/zookeeper/data
[root@127 data]#
复制当前路径
进如conf文件夹进行配置
[root@127 conf]# cd ../conf/
[root@127 conf]# cp zoo_sample.cfg zoo.cfg
[root@127 conf]# vim 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=/usr/local/src/zookeeper/data
dataLogDir=/usr/local/src/zookeeper/log
# 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
启动:
[root@127 conf]# cd ../bin/
[root@127 bin]# ls
README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zkServer.sh zkSnapShotToolkit.sh zkTxnLogToolkit.sh
zkCleanup.sh zkCli.sh zkEnv.sh zkServer-initialize.sh zkSnapShotToolkit.cmd zkTxnLogToolkit.cmd
[root@127 bin]# sh zkServer.sh start zoo.cfg
查看当前是否启动
sh zkServer.sh status zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/src/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
一台主机搭建集群:
在zookeeper的根目录下创建目录zkCluster和其子目录
mkdir zkCluster
创建其子目录以及myid(搭建3台所以是1-3)
for i in `seq 1 3`; do mkdir -p zk${i}/{data,log}; touch zk${i}/data/myid; echo ${i}>zk${i}/data/myid;done
进入conf文件夹
将zoo.cfg复制三份
分别为123
[root@127 conf]# ls
configuration.xsl log4j.properties zoo1.cfg zoo2.cfg zoo3.cfg zoo.cfg zoo_sample.cfg
[root@127 conf]#
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=/usr/local/src/zookeeper/zkCluster/zk1/data
dataLogDir=/usr/local/src/zookeeper/zkCluster/zk1/log
# the port at which the clients will connect
#每个配置文件端口修改不一样
clientPort=2181
##这个地方三个配置文件都一样
server.1=192.168.126.129:2887:3887
server.2=192.168.126.129:2888:3888
server.3=192.168.126.129:2889:3889
# 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
然后启动
sh zkServe.sh start zoo1.cfg
sh zkServe.sh start zoo2.cfg
sh zkServe.sh start zoo3.cfg
查看状态
sh zkServe.sh status zoo1.cfg
sh zkServe.sh status zoo2.cfg
sh zkServe.sh status zoo3.cfg
[root@127 bin]# sh zkServer.sh status zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/src/zookeeper/bin/../conf/zoo2.cfg
Client port found: 2182. Client address: localhost.
Mode: leader
[root@127 bin]# sh zkServer.sh status zoo1.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/src/zookeeper/bin/../conf/zoo1.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
[root@127 bin]# sh zkServer.sh status zoo3.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/src/zookeeper/bin/../conf/zoo1.cfg
Client port found: 2183. Client address: localhost.
Mode: follower
选举策略
myid值大的优先,超过半数投票则当选,并结束选举
举例:
1 启动 集群还未工作
2启动 2>3/2 集群启动 2>1 选举2当主机 并结束选举
3启动 2已经当选 3当丛机