Mycat学习实战-Mycat的zookeeper集群模式
[TOC]
1. ZooKeeper简介
ZooKeeper 是一个面向分布式系统的构建块。当设计一个分布式系统时,一般需要设计和开发一些协调服务:
名称服务— 名称服务是将一个名称映射到与该名称有关联的一些信息的服务。电话目录是将人的名字映射到其电话号码的一个名称服务。同样,DNS 服务也是一个名称服务,它将一个域名映射到一个 IP 地址。在分布式系统中,您可能想跟踪哪些服务器或服务在运行,并通过名称查看其状态。ZooKeeper 暴露了一个简单的接口来完成此工作。也可以将名称服务扩展到组成员服务,这样就可以获得与正在查找其名称的实体有关联的组的信息。
锁定— 为了允许在分布式系统中对共享资源进行有序的访问,可能需要实现分布式互斥(distributed mutexes)。ZooKeeper 提供一种简单的方式来实现它们。
同步— 与互斥同时出现的是同步访问共享资源的需求。无论是实现一个生产者-消费者队列,还是实现一个障碍,ZooKeeper 都提供一个简单的接口来实现该操作。
配置管理— 您可以使用 ZooKeeper 集中存储和管理分布式系统的配置。这意味着,所有新加入的节点都将在加入系统后就可以立即使用来自 ZooKeeper 的最新集中式配置。这还允许您通过其中一个 ZooKeeper 客户端更改集中式配置,集中地更改分布式系统的状态。
领导者选举— 分布式系统可能必须处理节点停机的问题,您可能想实现一个自动故障转移策略。ZooKeeper 通过领导者选举对此提供现成的支持。
2. ZooKeeper角色和端口
3. ZooKeeper部署简介
4. ZooKeeper部署管理Mycat
4.1 环境
系统:CentOS7.3
jdk版本:1.7
zookeeper版本:3.4.10
mycat版本:1.6
hostname
IP
myid
testA
192.168.33.11
1
testB
192.168.33.12
2
testC
192.168.33.13
3
4.2 环境准备
为了测试方便,这里关闭系统防火墙和禁用selinux,生产环境防火墙则需要开放zookeeper相关端口,2181、2888、3888。
# 关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
# 关闭SELINUX
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
setenforce 0
1
2
3
4
5
6
7
8
# 关闭防火墙
systemctlstopfirewalld.service
systemctldisablefirewalld.service
# 关闭SELINUX
sed-i's/SELINUX=enforcing/SELINUX=disabled/g'/etc/sysconfig/selinux
setenforce0
4.3 安装zookeeper
先在testA节点安装,再同步到其它2个节点。
4.3.1 下载解压
mkdir -p /data/packages/
cd /data/packages/
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
cd /usr/local/
tar -zxvf /data/packages/zookeeper-3.4.10.tar.gz
ln -s zookeeper-3.4.10 zookeeper
cd zookeeper
mkdir data log
cd conf/
cp zoo_sample.cfg zoo.cfg
1
2
3
4
5
6
7
8
9
10
11
mkdir-p/data/packages/
cd/data/packages/
wgethttp://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
cd/usr/local/
tar-zxvf/data/packages/zookeeper-3.4.10.tar.gz
ln-szookeeper-3.4.10zookeeper
cdzookeeper
mkdirdatalog
cdconf/
cpzoo_sample.cfgzoo.cfg
4.3.2 修改配置文件
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/zookeeper/data
dataLogDir=/usr/local/zookeeper/log
# the port at which the clients will connect
clientPort=2181
server.1=192.168.33.11:2888:3888
server.2=192.168.33.12:2888:3888
server.3=192.168.33.13:2888:3888
# 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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/data
dataLogDir=/usr/local/zookeeper/log
# the port at which the clients will connect
clientPort=2181
server.1=192.168.33.11:2888:3888
server.2=192.168.33.12:2888:3888
server.3=192.168.33.13:2888:3888
# 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
4.3.3 添加myid文件
vim /usr/local/zookeeper/data/myid写入 1
4.3.4 同步zookeeper目录到其它2个节点,修改myid
rsync -avzP /usr/local/zookeeper/ root@192.168.33.12:/usr/local/zookeeper/
rsync -avzP /usr/local/zookeeper/ root@192.168.33.13:/usr/local/zookeeper/
1
2
3
rsync-avzP/usr/local/zookeeper/root@192.168.33.12:/usr/local/zookeeper/
rsync-avzP/usr/local/zookeeper/root@192.168.33.13:/usr/local/zookeeper/
并在2个节点分别修改对应的myid
4.3.5 启动zookeeper
cd /usr/local/zookeeper/bin/
./zkServer.sh start
1
2
3
cd/usr/local/zookeeper/bin/
./zkServer.shstart
4.4 安装Mycat
同样,3个节点都需要安装mycat。
cd /data/packages/
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
cd /usr/local/
tar -zxvf /data/packages/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
1
2
3
4
5
cd/data/packages/
wgethttp://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
cd/usr/local/
tar-zxvf/data/packages/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
4.6 执行mycat初始化数据
3个节点Mycat都执行如下脚本
sh /usr/local/mycat/bin/init_zk_data.sh
1
2
sh/usr/local/mycat/bin/init_zk_data.sh
4.7 配置mycat支持zookeeper
vim /usr/local/mycat/conf/myid.properties
loadZk=true
# zk集群地址,多个用","隔开
zkURL=127.0.0.1:2181
# zk集群内Mycat集群ID
clusterId=mycat-cluster-1
# Mycat集群内本实例ID,禁止重复
myid=mycat_fz_01
# Mycat集群内节点个数
clusterSize=3
clusterNodes=mycat_fz_01,mycat_fz_02,mycat_fz_03
#server booster ; booster install on db same server,will reset all minCon to 1
type=server
boosterDataHosts=dataHost1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
loadZk=true
# zk集群地址,多个用","隔开
zkURL=127.0.0.1:2181
# zk集群内Mycat集群ID
clusterId=mycat-cluster-1
# Mycat集群内本实例ID,禁止重复
myid=mycat_fz_01
# Mycat集群内节点个数
clusterSize=3
clusterNodes=mycat_fz_01,mycat_fz_02,mycat_fz_03
#server booster ; booster install on db same server,will reset all minCon to 1
type=server
boosterDataHosts=dataHost1
4.8 用zookeeper配置mycat
使用ZooInspector工具管理ZooKeeper,以下是连接zookeeper方法。
连接成功后,可看到mycat集群的配置
修改相应配置
启动mycat后,mycat配置文件会自动更新。
注意事项:
* loadZk必须改为true才生效
* zkURL的地址是多个中间用“,”隔开
* clusterId,同一个zk内的集群ID必须唯一
* Myid:本实例的id在当前的mycat集群内ID唯一
* 配置完zk并启动mycat后,会更新本地conf下的相关配置文件。