日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

分布式架构中一致性解决方案——Zookeeper集群搭建

發(fā)布時間:2023/12/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分布式架构中一致性解决方案——Zookeeper集群搭建 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  當我們的項目在不知不覺中做大了之后,各種問題就出來了,真jb頭疼,比如性能,業(yè)務(wù)系統(tǒng)的并行計算的一致性協(xié)調(diào)問題,比如分布式架構(gòu)的事務(wù)問題,

我們需要多臺機器共同commit事務(wù),經(jīng)典的案例當然是銀行轉(zhuǎn)賬,支付寶轉(zhuǎn)賬這種,如果是一臺機器的話,這個還是很方便的,windows中自帶了一個事務(wù)協(xié)

調(diào)器mstsc,但是呢,你那種很大很牛逼的項目不可能全是windows服務(wù)器,對吧,有些人為了解決這個問題,會采用2pc,3pc這種算法,或者是paxos的思

想進行分布式下的一致性處理,當然在這個世界上,真的不需要你自己去開發(fā)這種協(xié)調(diào)性,因為現(xiàn)在已經(jīng)有了專門解決這種問題的解決方案,比如zookeeper。

?

一:zookeeper集群搭建

  有些人應(yīng)該明白,zookeeper正是google的chubby的開源實現(xiàn),使用zookeeper之前,我們先來搭建一個集群。

1. 下載

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

?

2. 文件夾配置

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

?

3. 配置zoo.cfg

? ?現(xiàn)在我們有三個文件夾,也就是3個zookeeper程序,在3001/conf/下面有一個zoo_sample.cfg文件,現(xiàn)在我們改成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=1server.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文件的內(nèi)容就取決如此,比如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=1server.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=1server.1=192.168.161.134:2888:3888 server.2=192.168.161.134:2889:3889 server.3=192.168.161.134:2890:3890

?

5. 啟動各自服務(wù)器

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

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

./zkServer.sh start-foreground

?

現(xiàn)在我們都啟動了,接下來我們可以用命令看下哪個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]#

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

?

二:驅(qū)動下載

1. ?java的驅(qū)動就方便了,直接在源代碼中就提供了,直接copy一下lib文件夾中的jar包就ok了,真是tmd的方便。

?

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

?

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

?

總結(jié)

以上是生活随笔為你收集整理的分布式架构中一致性解决方案——Zookeeper集群搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。