集成ZooKeeper
前期回顾:
- Canal:部署Canal与Canal Admin
搭建ZooKeeper可以参考下面这几篇博客:
- ZooKeeper :Shell脚本搭建单机版ZooKeeper
- ZooKeeper :搭建ZooKeeper集群
- ZooKeeper :Nginx基于TCP协议代理ZooKeeper集群
启动ZooKeeper并且关闭防火墙。
[root@localhost ~]# cd /usr/local/apache-zookeeper-3.6.3-bin/
[root@localhost apache-zookeeper-3.6.3-bin]# sh bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost apache-zookeeper-3.6.3-bin]# sh bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg
Client port found: 9000. Client address: localhost. Client SSL: false.
Mode: standalone
[root@localhost apache-zookeeper-3.6.3-bin]# systemctl stop firewalld
在Canal Admin上添加集群,集群名称为zookeeper。

修改zookeeper集群的主配置。

主要是添加ZooKeeper服务地址。

在zookeeper集群下创建server(博主把之前创建的instance和server都删除了,因为server不能修改所属集群配置,只能删除再重新创建)。

创建kaven server成功,状态为启动。

在zookeeper集群下创建instance。

由下图所示,itkaven instance由zookeeper集群下的kaven server运行,状态也是启动。

查看itkaven instance的日志,很显然启动成功了。

因为zookeeper集群下只有一个server,即kaven server,因此itkaven instance肯定是在kaven server上运行的。查看Canal项目(zookeeper集群下唯一的server)的日志文件和配置文件即可发现itkaven instance的相关文件:
[root@localhost canal-server]# ll logs
总用量 0
drwxr-xr-x. 2 root root 47 12月 14 15:27 canal
drwxr-xr-x. 2 root root 25 12月 14 15:20 example
drwxr-xr-x. 2 root root 25 12月 14 16:26 itkaven
drwxr-xr-x. 2 root root 23 12月 14 15:34 other
[root@localhost canal-server]# ll conf
总用量 16
-rwxrwxrwx. 1 root root 319 4月 19 2021 canal_local.properties
-rwxrwxrwx. 1 root root 6277 4月 19 2021 canal.properties
drwxrwxrwx. 2 root root 65 12月 14 16:05 example
drwxr-xr-x. 2 root root 38 12月 14 16:27 itkaven
-rwxrwxrwx. 1 root root 3437 4月 19 2021 logback.xml
drwxrwxrwx. 2 root root 39 12月 13 23:02 metrics
drwxr-xr-x. 2 root root 38 12月 14 15:36 other
drwxrwxrwx. 3 root root 149 12月 13 23:02 spring
ZooKeeper中也会保存一些数据(比如集群下的所有server和instance信息,以及instance在哪个server上运行)。
[root@localhost apache-zookeeper-3.6.3-bin]# sh bin/zkCli.sh -timeout 5000 -server 127.0.0.1:9000
[zk: 127.0.0.1:9000(CONNECTED) 0] ls -R /otter
/otter
/otter/canal
/otter/canal/cluster
/otter/canal/destinations
/otter/canal/cluster/192.168.1.199:11111
/otter/canal/destinations/itkaven
/otter/canal/destinations/itkaven/cluster
/otter/canal/destinations/itkaven/running
/otter/canal/destinations/itkaven/cluster/192.168.1.199:11111
[zk: 127.0.0.1:9000(CONNECTED) 1] get /otter/canal/destinations/itkaven/running
{"active":true,"address":"192.168.1.199:11111"}
HA机制设计
canal的HA分为两部分,canal server和canal client分别有对应的HA实现:
-
canal server: 为了减少对mysql dump的请求,不同server上的instance要求同一时间只能有一个处于running,其他的处于standby状态。 -
canal client: 为了保证有序性,一个instance同一时间只能由一个canal client进行get/ack/rollback操作,否则客户端接收无法保证有序。
整个HA机制的控制主要是依赖了ZooKeeper的几个特性,watcher和EPHEMERAL节点(和session生命周期绑定)。
- ZooKeeper :重要概念
大致步骤:
-
canal server要启动某个canal instance时,都先向ZooKeeper进行一次尝试启动判断 (创建EPHEMERAL节点,谁创建成功就允许谁启动)。 - 创建
ZooKeeper节点成功后,该canal server就启动对应的canal instance,没有创建成功的canal instance就会处于standby状态。 - 一旦
ZooKeeper发现canal server 创建的节点消失后,立即通知其他的canal server再次进行步骤1的操作,重新选出一个canal server启动canal instance。 -
canal client每次进行connect时,会首先向ZooKeeper询问当前是谁启动了canal instance,然后和其建立连接,一旦连接不可用,会重新尝试connect。
canal client的方式和canal server的方式类似,也是利用ZooKeeper抢占EPHEMERAL节点的方式进行控制。
使用ZooKeeper进行集群管理就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。










