當咱們的項目在不知不覺中作大了以後,各類問題就出來了,真jb頭疼,好比性能,業務系統的並行計算的一致性協調問題,好比分佈式架構的事務問題,html
咱們須要多臺機器共同commit事務,經典的案例固然是銀行轉帳,支付寶轉帳這種,若是是一臺機器的話,這個仍是很方便的,windows中自帶了一個事務協java
調器mstsc,可是呢,你那種很大很牛逼的項目不可能全是windows服務器,對吧,有些人爲了解決這個問題,會採用2pc,3pc這種算法,或者是paxos的思算法
想進行分佈式下的一致性處理,固然在這個世界上,真的不須要你本身去開發這種協調性,由於如今已經有了專門解決這種問題的解決方案,好比zookeeper。apache
一:zookeeper集羣搭建windows
有些人應該明白,zookeeper正是google的chubby的開源實現,使用zookeeper以前,咱們先來搭建一個集羣。服務器
1. 下載架構
從官網上,咱們能夠看到,zookeeper的最新版本是3.4.8,下載地址是:http://apache.fayea.com/zookeeper/zookeeper-3.4.8/,能夠下載一下:分佈式
2. 文件夾配置ide
接下來咱們解壓一下,根目錄爲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的最新版本,好比下面這樣:
好了,大概就說這麼多,但願對你有幫助~~~