當我們的項目在不知不覺中做大了之後,各種問題就出來了,真jb頭疼,比如性能,業務系統的並行計算的一致性協調問題,比如分散式架構的事務問題, 我們需要多台機器共同commit事務,經典的案例當然是銀行轉賬,支付寶轉賬這種,如果是一臺機器的話,這個還是很方便的,windows中自帶了一個事務協 調器ms ...
當我們的項目在不知不覺中做大了之後,各種問題就出來了,真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的最新版本,比如下麵這樣:
好了,大概就說這麼多,希望對你有幫助~~~