這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)利用zookeeper怎么實(shí)現(xiàn)分布式鎖,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站主營涪陵網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App定制開發(fā),涪陵h5微信小程序搭建,涪陵網(wǎng)站營銷推廣歡迎涪陵等地區(qū)企業(yè)咨詢
pom文件
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.1</version> </dependency>
Spring集成
<!-- 重連策略 --> <bean id="retryPolicy" class="org.apache.curator.retry.ExponentialBackoffRetry"> <!-- 間隔時(shí)間基數(shù) --> <constructor-arg index="0" value="1000" /> <!-- 重連策略 --> <constructor-arg index="1" value="3" /> </bean> <bean id="curatorFramework" class="org.apache.curator.framework.CuratorFrameworkFactory" factory-method="newClient" init-method="start"> <constructor-arg index="0" value="#{globalConfig.zk_address}" /> <!-- sessionTimeoutMs會(huì)話超時(shí)時(shí)間,單位為毫秒。默認(rèn)是60000ms --> <constructor-arg index="1" value="5000" /> <!-- connectionTimeoutMs連接創(chuàng)建超時(shí)時(shí)間,單位毫秒,默認(rèn)15000ms --> <constructor-arg index="2" value="3000" /> <constructor-arg index="3" ref="retryPolicy" /> </bean>
@Autowired
private CuratorFramework curatorFramework;
//定義鎖路徑
String path = "/lock/discount/" + discountId;
InterProcessMutex lock = new InterProcessMutex(curatorFramework, path);
//獲取鎖
if (lock.acquire(3000, TimeUnit.MILLISECONDS)) {
//do something
}
//釋放鎖
lock.release();
上述就是小編為大家分享的利用zookeeper怎么實(shí)現(xiàn)分布式鎖了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:利用zookeeper怎么實(shí)現(xiàn)分布式鎖
標(biāo)題網(wǎng)址:http://chinadenli.net/article2/jgjcic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、全網(wǎng)營銷推廣、網(wǎng)站制作、服務(wù)器托管、云服務(wù)器、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)