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