分散式架構中一致性解決方案——Zookeeper叢集搭建

一線碼農發表於2016-07-08

  當我們的專案在不知不覺中做大了之後,各種問題就出來了,真jb頭疼,比如效能,業務系統的平行計算的一致性協調問題,比如分散式架構的事務問題,

我們需要多臺機器共同commit事務,經典的案例當然是銀行轉賬,支付寶轉賬這種,如果是一臺機器的話,這個還是很方便的,windows中自帶了一個事務協

調器mstsc,但是呢,你那種很大很牛逼的專案不可能全是windows伺服器,對吧,有些人為了解決這個問題,會採用2pc,3pc這種演算法,或者是paxos的思

想進行分散式下的一致性處理,當然在這個世界上,真的不需要你自己去開發這種協調性,因為現在已經有了專門解決這種問題的解決方案,比如zookeeper。

 

一:zookeeper叢集搭建

  有些人應該明白,zookeeper正是google的chubby的開源實現,使用zookeeper之前,我們先來搭建一個叢集。

1. 下載

   從官網上,我們可以看到,zookeeper的最新版本是3.4.8,下載地址是:http://apache.fayea.com/zookeeper/zookeeper-3.4.8/,可以下載一下:

 

2. 資料夾配置

接下來我們解壓一下,根目錄為zkcluster,下面使用clientport(3000,3001,3002)這樣的埠作為資料夾名稱,裡面就是zookeeper解壓包,如下面這樣:

 

3. 配置zoo.cfg

   現在我們有三個資料夾,也就是3個zookeeper程式,在3001/conf/下面有一個zoo_sample.cfg檔案,現在我們改成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=/root/zkcluster/3001/data
dataLogDir=/root/zkcluster/3001/logs
# the port at which the clients will connect
clientPort=3001
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=6
#
# 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

server.1=192.168.161.134:2888:3888
server.2=192.168.161.134:2889:3889
server.3=192.168.161.134:2890:3890

這裡我們要注意的是,紅色的部分分別就是:指定zookeeper的data和log資料夾,指定clientport訪問的埠和servers的列表。

 

4. 生成pid檔案

    我們在servers列表中,可以看到有server.1 ,server.2, server.3 三個字串,生成pid檔案的內容就取決如此,比如server.1的地址,

我們的pid檔案裡面就是1,不過要知道的是,pid檔案要在data目錄下,比如下面這樣:

 

ok,同樣的道理,3002和3003的資料夾同3001就可以了,比如他們的zoo.cfg如下:

 

--------  3002 --------------

# 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=/root/zkcluster/3002/data
dataLogDir=/root/zkcluster/3002/logs
# the port at which the clients will connect
clientPort=3002
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=6
#
# 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

server.1=192.168.161.134:2888:3888
server.2=192.168.161.134:2889:3889
server.3=192.168.161.134:2890:3890

 

--------  3003 --------------

# 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=/root/zkcluster/3003/data
dataLogDir=/root/zkcluster/3003/logs
# the port at which the clients will connect
clientPort=3003
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=6
#
# 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

server.1=192.168.161.134:2888:3888
server.2=192.168.161.134:2889:3889
server.3=192.168.161.134:2890:3890

 

5. 啟動各自伺服器

    到現在為止,我們各個zookeeper程式的配置都結束了,接下來我們到各自目錄的bin目錄下,通過zkServer.sh來進行啟動,比如下面這樣:

ok,接下來我們來開始啟動,通過如下命令即可:

./zkServer.sh start-foreground

 

現在我們都啟動了,接下來我們可以用命令看下哪個server是leader,哪些是follower。。。

[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zkcluster/3001/bin/../conf/zoo.cfg
Mode: follower
[root@localhost bin]# 


[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zkcluster/3002/bin/../conf/zoo.cfg
Mode: leader
[root@localhost bin]# 

[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zkcluster/3003/bin/../conf/zoo.cfg
Mode: follower
[root@localhost bin]# 

到目前為止,我們的服務端操作都ok啦,,,是不是好吊。。。

 

二:驅動下載

1.  java的驅動就方便了,直接在原始碼中就提供了,直接copy一下lib資料夾中的jar包就ok了,真是tmd的方便。

 

2. 用C#驅動的也不要太煩,要使用也是不難的,我們可以通過nuget下載一下就可以了,轉換過來的版本也是3.4.8的最新版本,比如下面這樣:

 

好了,大概就說這麼多,希望對你有幫助~~~

 

相關文章