這篇文章主要介紹“Curator怎么實(shí)現(xiàn)分布式數(shù)字”,在日常操作中,相信很多人在Curator怎么實(shí)現(xiàn)分布式數(shù)字問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Curator怎么實(shí)現(xiàn)分布式數(shù)字”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元登封做網(wǎng)站,已為上家服務(wù),為登封各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
Curator有2個實(shí)現(xiàn):
第一個:
package curator.counters; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.shared.SharedCount; import org.apache.curator.framework.recipes.shared.SharedCountListener; import org.apache.curator.framework.recipes.shared.SharedCountReader; import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.retry.ExponentialBackoffRetry; public class SharedCounterDemo { public static void main(String[] args) throws Exception { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); final CuratorFramework client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181").sessionTimeoutMs(5000).connectionTimeoutMs(10000).retryPolicy(retryPolicy).namespace("test").build(); client.start(); SharedCount count = new SharedCount(client,"/cut",0); count.start(); count.addListener(new SharedCountListener(){ @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { } @Override public void countHasChanged(SharedCountReader sharedCount, int newCount) throws Exception { System.err.println("sharedCount.getCount : "+sharedCount.getCount()); System.err.println(newCount); }}); Thread.currentThread().sleep(5000); count.setCount(5); System.in.read(); } }
第二個:
package curator.counters; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.atomic.AtomicValue; import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.retry.RetryNTimes; public class DistributedAtomicIntegerDemo { public static void main(String[] args) throws Exception { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); final CuratorFramework client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181").sessionTimeoutMs(5000).connectionTimeoutMs(10000).retryPolicy(retryPolicy).namespace("test").build(); client.start(); DistributedAtomicInteger atomicInteger = new DistributedAtomicInteger(client,"/autlog",new RetryNTimes(32,1000)); AtomicValue<Integer> rc = atomicInteger.add(8); System.out.println("success:" + rc.succeeded() + ";before:" + rc.preValue() + ";after:" + rc.postValue()); System.in.read(); } }
到此,關(guān)于“Curator怎么實(shí)現(xiàn)分布式數(shù)字”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)頁名稱:Curator怎么實(shí)現(xiàn)分布式數(shù)字
當(dāng)前網(wǎng)址:http://aaarwkj.com/article38/jjgipp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站改版、定制網(wǎng)站、外貿(mào)建站、網(wǎng)站導(dǎo)航、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)