天天看點

Canal:ZooKeeper進行叢集管理

內建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​

​。

Canal:ZooKeeper進行叢集管理

修改​

​zookeeper​

​叢集的主配置。

Canal:ZooKeeper進行叢集管理

主要是添加​

​ZooKeeper​

​服務位址。

Canal:ZooKeeper進行叢集管理

在​

​zookeeper​

​​叢集下建立​

​server​

​​(部落客把之前建立的​

​instance​

​​和​

​server​

​​都删除了,因為​

​server​

​不能修改所屬叢集配置,隻能删除再重新建立)。

Canal:ZooKeeper進行叢集管理

建立​

​kaven server​

​成功,狀态為啟動。

Canal:ZooKeeper進行叢集管理

在​

​zookeeper​

​​叢集下建立​

​instance​

​。

Canal:ZooKeeper進行叢集管理

由下圖所示,​

​itkaven instance​

​​由​

​zookeeper​

​​叢集下的​

​kaven server​

​運作,狀态也是啟動。

Canal:ZooKeeper進行叢集管理

檢視​

​itkaven instance​

​的日志,很顯然啟動成功了。

Canal:ZooKeeper進行叢集管理

因為​

​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 :重要概念​​

大緻步驟:

  1. ​canal server​

    ​​要啟動某個​

    ​canal instance​

    ​​時,都先向​

    ​ZooKeeper​

    ​​進行一次嘗試啟動判斷 (建立​

    ​EPHEMERAL​

    ​節點,誰建立成功就允許誰啟動)。
  2. 建立​

    ​ZooKeeper​

    ​​節點成功後,該​

    ​canal server​

    ​​就啟動對應的​

    ​canal instance​

    ​​,沒有建立成功的​

    ​canal instance​

    ​​就會處于​

    ​standby​

    ​狀态。
  3. 一旦​

    ​ZooKeeper​

    ​​發現​

    ​canal server​

    ​​ 建立的節點消失後,立即通知其他的​

    ​canal server​

    ​​再次進行步驟​

    ​1​

    ​​的操作,重新選出一個​

    ​canal server​

    ​​啟動​

    ​canal instance​

    ​。
  4. ​canal client​

    ​​每次進行​

    ​connect​

    ​​時,會首先向​

    ​ZooKeeper​

    ​​詢問目前是誰啟動了​

    ​canal instance​

    ​​,然後和其建立連接配接,一旦連接配接不可用,會重新嘗試​

    ​connect​

    ​。

繼續閱讀