自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ZK客戶端Curator使用詳解

開源
zookeeper不是為高可用性設(shè)計的,但它使用ZAB協(xié)議達到了極高的一致性,所以是個CP系統(tǒng)。所以它經(jīng)常被選作注冊中心、配置中心、分布式鎖等場景。

 zookeeper不是為高可用性設(shè)計的,但它使用ZAB協(xié)議達到了極高的一致性,所以是個CP系統(tǒng)。所以它經(jīng)常被選作注冊中心、配置中心、分布式鎖等場景。

[[323374]]

它的性能是非常有限的,而且API并不是那么好用。xjjdog傾向于使用基于Raft協(xié)議的Etcd或者Consul,它們更加輕量級一些。

Curator是netflix公司開源的一套zookeeper客戶端,目前是Apache的頂級項目。與Zookeeper提供的原生客戶端相比,Curator的抽象層次更高,簡化了Zookeeper客戶端的開發(fā)量。Curator解決了很多zookeeper客戶端非常底層的細節(jié)開發(fā)工作,包括連接重連、反復(fù)注冊wathcer和NodeExistsException 異常等。

Curator由一系列的模塊構(gòu)成,對于一般開發(fā)者而言,常用的是curator-framework和curator-recipes,下面對此依次介紹。

1.maven依賴

最新版本的curator 4.3.0支持zookeeper 3.4.x和3.5,但是需要注意curator傳遞進來的依賴,需要和實際服務(wù)器端使用的版本相符,以我們目前使用的zookeeper 3.4.6為例。

  1. <dependency> 
  2.     <groupId>org.apache.curator</groupId> 
  3.     <artifactId>curator-framework</artifactId> 
  4.     <version>4.3.0</version> 
  5.     <exclusions> 
  6.         <exclusion> 
  7.             <groupId>org.apache.zookeeper</groupId> 
  8.             <artifactId>zookeeper</artifactId> 
  9.         </exclusion> 
  10.     </exclusions> 
  11. </dependency> 
  12. <dependency> 
  13.     <groupId>org.apache.curator</groupId> 
  14.     <artifactId>curator-recipes</artifactId> 
  15.     <version>4.3.0</version> 
  16.     <exclusions> 
  17.         <exclusion> 
  18.             <groupId>org.apache.zookeeper</groupId> 
  19.             <artifactId>zookeeper</artifactId> 
  20.         </exclusion> 
  21.     </exclusions> 
  22. </dependency> 
  23. <dependency> 
  24.     <groupId>org.apache.zookeeper</groupId> 
  25.     <artifactId>zookeeper</artifactId> 
  26.     <version>3.4.6</version> 
  27. </dependency> 

2.curator-framework

下面是一些常見的zk相關(guān)的操作。

  1. public static CuratorFramework getClient() { 
  2.     return CuratorFrameworkFactory.builder() 
  3.             .connectString("127.0.0.1:2181"
  4.             .retryPolicy(new ExponentialBackoffRetry(1000, 3)) 
  5.             .connectionTimeoutMs(15 * 1000) //連接超時時間,默認15秒 
  6.             .sessionTimeoutMs(60 * 1000) //會話超時時間,默認60秒 
  7.             .namespace("arch") //設(shè)置命名空間 
  8.             .build(); 
  9.   
  10. public static void create(final CuratorFramework client, final String path, final byte[] payload) throws Exception { 
  11.     client.create().creatingParentsIfNeeded().forPath(path, payload); 
  12.   
  13. public static void createEphemeral(final CuratorFramework client, final String path, final byte[] payload) throws Exception { 
  14.     client.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload); 
  15.   
  16. public static String createEphemeralSequential(final CuratorFramework client, final String path, final byte[] payload) throws Exception { 
  17.     return client.create().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path, payload); 
  18.   
  19. public static void setData(final CuratorFramework client, final String path, final byte[] payload) throws Exception { 
  20.     client.setData().forPath(path, payload); 
  21.   
  22. public static void delete(final CuratorFramework client, final String path) throws Exception { 
  23.     client.delete().deletingChildrenIfNeeded().forPath(path); 
  24.   
  25. public static void guaranteedDelete(final CuratorFramework client, final String path) throws Exception { 
  26.     client.delete().guaranteed().forPath(path); 
  27.   
  28. public static String getData(final CuratorFramework client, final String path) throws Exception { 
  29.     return new String(client.getData().forPath(path)); 
  30.   
  31. public static List<String> getChildren(final CuratorFramework client, final String path) throws Exception { 
  32.     return client.getChildren().forPath(path); 

3.curator-recipescurator-recipes

提供了一些zk的典型使用場景的參考。下面主要介紹一下開發(fā)中常用的組件。

事件監(jiān)聽

zookeeper原生支持通過注冊watcher來進行事件監(jiān)聽,但是其使用不是特別方便,需要開發(fā)人員自己反復(fù)注冊watcher,比較繁瑣。

Curator引入Cache來實現(xiàn)對zookeeper服務(wù)端事務(wù)的監(jiān)聽。Cache是Curator中對事件監(jiān)聽的包裝,其對事件的監(jiān)聽其實可以近似看作是一個本地緩存視圖和遠程Zookeeper視圖的對比過程。同時,Curator能夠自動為開發(fā)人員處理反復(fù)注冊監(jiān)聽,從而大大簡化原生api開發(fā)的繁瑣過程。

1)Node Cache

  1. public static void nodeCache() throws Exception { 
  2.     final String path = "/nodeCache"
  3.     final CuratorFramework client = getClient(); 
  4.     client.start(); 
  5.   
  6.     delete(client, path); 
  7.     create(client, path, "cache".getBytes()); 
  8.   
  9.     final NodeCache nodeCache = new NodeCache(client, path); 
  10.     nodeCache.start(true); 
  11.     nodeCache.getListenable() 
  12.             .addListener(() -> System.out.println("node data change, new data is " + new String(nodeCache.getCurrentData().getData()))); 
  13.   
  14.     setData(client, path, "cache1".getBytes()); 
  15.     setData(client, path, "cache2".getBytes()); 
  16.   
  17.     Thread.sleep(1000); 
  18.   
  19.     client.close(); 

NodeCache可以監(jiān)聽指定的節(jié)點,注冊監(jiān)聽器后,節(jié)點的變化會通知相應(yīng)的監(jiān)聽器

2)Path Cache

Path Cache 用來監(jiān)聽ZNode的子節(jié)點事件,包括added、updateed、removed,Path Cache會同步子節(jié)點的狀態(tài),產(chǎn)生的事件會傳遞給注冊的PathChildrenCacheListener。

  1. public static void pathChildrenCache() throws Exception { 
  2.         final String path = "/pathChildrenCache"
  3.         final CuratorFramework client = getClient(); 
  4.         client.start(); 
  5.   
  6.         final PathChildrenCache cache = new PathChildrenCache(client, path, true); 
  7.         cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); 
  8.     cache.getListenable().addListener((client1, event) -> { 
  9.             switch (event.getType()) { 
  10.                 case CHILD_ADDED: 
  11.                     System.out.println("CHILD_ADDED:" + event.getData().getPath()); 
  12.                     break; 
  13.                 case CHILD_REMOVED: 
  14.                     System.out.println("CHILD_REMOVED:" + event.getData().getPath()); 
  15.                     break; 
  16.                 case CHILD_UPDATED: 
  17.                     System.out.println("CHILD_UPDATED:" + event.getData().getPath()); 
  18.                     break; 
  19.                 case CONNECTION_LOST: 
  20.                     System.out.println("CONNECTION_LOST:" + event.getData().getPath()); 
  21.                     break; 
  22.                 case CONNECTION_RECONNECTED: 
  23.                     System.out.println("CONNECTION_RECONNECTED:" + event.getData().getPath()); 
  24.                     break; 
  25.                 case CONNECTION_SUSPENDED: 
  26.                     System.out.println("CONNECTION_SUSPENDED:" + event.getData().getPath()); 
  27.                     break; 
  28.                 case INITIALIZED: 
  29.                     System.out.println("INITIALIZED:" + event.getData().getPath()); 
  30.                     break; 
  31.                 default
  32.                     break; 
  33.             } 
  34.         }); 
  35.   
  36. //        client.create().withMode(CreateMode.PERSISTENT).forPath(path); 
  37.         Thread.sleep(1000); 
  38.   
  39.         client.create().withMode(CreateMode.PERSISTENT).forPath(path + "/c1"); 
  40.         Thread.sleep(1000); 
  41.   
  42.         client.delete().forPath(path + "/c1"); 
  43.         Thread.sleep(1000); 
  44.   
  45.         client.delete().forPath(path); //監(jiān)聽節(jié)點本身的變化不會通知 
  46.         Thread.sleep(1000); 
  47.   
  48.         client.close(); 
  49.     } 

3)Tree Cache

Path Cache和Node Cache的“合體”,監(jiān)視路徑下的創(chuàng)建、更新、刪除事件,并緩存路徑下所有孩子結(jié)點的數(shù)據(jù)。

  1. public static void treeCache() throws Exception { 
  2.     final String path = "/treeChildrenCache"
  3.     final CuratorFramework client = getClient(); 
  4.     client.start(); 
  5.   
  6.     final TreeCache cache = new TreeCache(client, path); 
  7.     cache.start(); 
  8.   
  9.     cache.getListenable().addListener((client1, event) -> { 
  10.         switch (event.getType()){ 
  11.             case NODE_ADDED: 
  12.                 System.out.println("NODE_ADDED:" + event.getData().getPath()); 
  13.                 break; 
  14.             case NODE_REMOVED: 
  15.                 System.out.println("NODE_REMOVED:" + event.getData().getPath()); 
  16.                 break; 
  17.             case NODE_UPDATED: 
  18.                 System.out.println("NODE_UPDATED:" + event.getData().getPath()); 
  19.                 break; 
  20.             case CONNECTION_LOST: 
  21.                 System.out.println("CONNECTION_LOST:" + event.getData().getPath()); 
  22.                 break; 
  23.             case CONNECTION_RECONNECTED: 
  24.                 System.out.println("CONNECTION_RECONNECTED:" + event.getData().getPath()); 
  25.                 break; 
  26.             case CONNECTION_SUSPENDED: 
  27.                 System.out.println("CONNECTION_SUSPENDED:" + event.getData().getPath()); 
  28.                 break; 
  29.             case INITIALIZED: 
  30.                 System.out.println("INITIALIZED:" + event.getData().getPath()); 
  31.                 break; 
  32.             default
  33.                 break; 
  34.         } 
  35.     }); 
  36.   
  37.     client.create().withMode(CreateMode.PERSISTENT).forPath(path); 
  38.     Thread.sleep(1000); 
  39.   
  40.     client.create().withMode(CreateMode.PERSISTENT).forPath(path + "/c1"); 
  41.     Thread.sleep(1000); 
  42.   
  43.     setData(client, path, "test".getBytes()); 
  44.     Thread.sleep(1000); 
  45.   
  46.     client.delete().forPath(path + "/c1"); 
  47.     Thread.sleep(1000); 
  48.   
  49.     client.delete().forPath(path); 
  50.     Thread.sleep(1000); 
  51.   
  52.     client.close(); 

選舉

curator提供了兩種方式,分別是Leader Latch和Leader Election。

1)Leader Latch

隨機從候選著中選出一臺作為leader,選中之后除非調(diào)用close()釋放leadship,否則其他的后選擇無法成為leader

  1. public class LeaderLatchTest { 
  2.   
  3.     private static final String PATH = "/demo/leader"
  4.   
  5.     public static void main(String[] args) { 
  6.         List<LeaderLatch> latchList = new ArrayList<>(); 
  7.         List<CuratorFramework> clients = new ArrayList<>(); 
  8.         try { 
  9.             for (int i = 0; i < 10; i++) { 
  10.                 CuratorFramework client = getClient(); 
  11.                 client.start(); 
  12.                 clients.add(client); 
  13.   
  14.                 final LeaderLatch leaderLatch = new LeaderLatch(client, PATH, "client#" + i); 
  15.                 leaderLatch.addListener(new LeaderLatchListener() { 
  16.                     @Override 
  17.                     public void isLeader() { 
  18.                         System.out.println(leaderLatch.getId() + ":I am leader. I am doing jobs!"); 
  19.                     } 
  20.   
  21.                     @Override 
  22.                     public void notLeader() { 
  23.                         System.out.println(leaderLatch.getId() + ":I am not leader. I will do nothing!"); 
  24.                     } 
  25.                 }); 
  26.                 latchList.add(leaderLatch); 
  27.                 leaderLatch.start(); 
  28.             } 
  29.             Thread.sleep(1000 * 60); 
  30.         } catch (Exception e) { 
  31.             e.printStackTrace(); 
  32.         } finally { 
  33.             for (CuratorFramework client : clients) { 
  34.                 CloseableUtils.closeQuietly(client); 
  35.             } 
  36.   
  37.             for (LeaderLatch leaderLatch : latchList) { 
  38.                 CloseableUtils.closeQuietly(leaderLatch); 
  39.             } 
  40.         } 
  41.     } 
  42.   
  43.     public static CuratorFramework getClient() { 
  44.         return CuratorFrameworkFactory.builder() 
  45.                 .connectString("127.0.0.1:2181"
  46.                 .retryPolicy(new ExponentialBackoffRetry(1000, 3)) 
  47.                 .connectionTimeoutMs(15 * 1000) //連接超時時間,默認15秒 
  48.                 .sessionTimeoutMs(60 * 1000) //會話超時時間,默認60秒 
  49.                 .namespace("arch") //設(shè)置命名空間 
  50.                 .build(); 
  51.     } 
  52.   

2)Leader Election

通過LeaderSelectorListener可以對領(lǐng)導(dǎo)權(quán)進行控制, 在適當(dāng)?shù)臅r候釋放領(lǐng)導(dǎo)權(quán),這樣每個節(jié)點都有可能獲得領(lǐng)導(dǎo)權(quán)。而LeaderLatch則一直持有l(wèi)eadership, 除非調(diào)用close方法,否則它不會釋放領(lǐng)導(dǎo)權(quán)。

  1. public class LeaderSelectorTest { 
  2.     private static final String PATH = "/demo/leader"
  3.   
  4.     public static void main(String[] args) { 
  5.         List<LeaderSelector> selectors = new ArrayList<>(); 
  6.         List<CuratorFramework> clients = new ArrayList<>(); 
  7.         try { 
  8.             for (int i = 0; i < 10; i++) { 
  9.                 CuratorFramework client = getClient(); 
  10.                 client.start(); 
  11.                 clients.add(client); 
  12.   
  13.                 final String name = "client#" + i; 
  14.                 LeaderSelector leaderSelector = new LeaderSelector(client, PATH, new LeaderSelectorListenerAdapter() { 
  15.                     @Override 
  16.                     public void takeLeadership(CuratorFramework client) throws Exception { 
  17.                         System.out.println(name + ":I am leader."); 
  18.                         Thread.sleep(2000); 
  19.                     } 
  20.                 }); 
  21.   
  22.                 leaderSelector.autoRequeue(); 
  23.                 leaderSelector.start(); 
  24.                 selectors.add(leaderSelector); 
  25.             } 
  26.             Thread.sleep(Integer.MAX_VALUE); 
  27.         } catch (Exception e) { 
  28.             e.printStackTrace(); 
  29.         } finally { 
  30.             for (CuratorFramework client : clients) { 
  31.                 CloseableUtils.closeQuietly(client); 
  32.             } 
  33.   
  34.             for (LeaderSelector selector : selectors) { 
  35.                 CloseableUtils.closeQuietly(selector); 
  36.             } 
  37.   
  38.         } 
  39.     } 
  40.   
  41.     public static CuratorFramework getClient() { 
  42.         return CuratorFrameworkFactory.builder() 
  43.                 .connectString("127.0.0.1:2181"
  44.                 .retryPolicy(new ExponentialBackoffRetry(1000, 3)) 
  45.                 .connectionTimeoutMs(15 * 1000) //連接超時時間,默認15秒 
  46.                 .sessionTimeoutMs(60 * 1000) //會話超時時間,默認60秒 
  47.                 .namespace("arch") //設(shè)置命名空間 
  48.                 .build(); 
  49.     } 
  50.   

分布式鎖

1)可重入鎖Shared Reentrant Lock

Shared意味著鎖是全局可見的, 客戶端都可以請求鎖。Reentrant和JDK的ReentrantLock類似, 意味著同一個客戶端在擁有鎖的同時,可以多次獲取,不會被阻塞。它是由類InterProcessMutex來實現(xiàn)。它的構(gòu)造函數(shù)為:

  1. public InterProcessMutex(CuratorFramework client, String path) 

通過acquire獲得鎖,并提供超時機制:

  1. /** 
  2. * Acquire the mutex - blocking until it's available. Note: the same thread can call acquire 
  3. * re-entrantly. Each call to acquire must be balanced by a call to release() 
  4. */ 
  5. public void acquire(); 
  6.   
  7. /** 
  8. * Acquire the mutex - blocks until it's available or the given time expires. Note: the same thread can 
  9. * call acquire re-entrantly. Each call to acquire that returns true must be balanced by a call to release() 
  10. * Parameters: 
  11. time - time to wait 
  12. * unit - time unit 
  13. Returns
  14. true if the mutex was acquired, false if not 
  15. */ 
  16. public boolean acquire(long time, TimeUnit unit); 

通過release()方法釋放鎖。InterProcessMutex 實例可以重用。Revoking ZooKeeper recipes wiki定義了可協(xié)商的撤銷機制。為了撤銷mutex, 調(diào)用下面的方法:

  1. /** 
  2. * 將鎖設(shè)為可撤銷的. 當(dāng)別的進程或線程想讓你釋放鎖時Listener會被調(diào)用。 
  3. * Parameters: 
  4. * listener - the listener 
  5. */ 
  6. public void makeRevocable(RevocationListener<T> listener) 

2)不可重入鎖Shared Lock

使用InterProcessSemaphoreMutex,調(diào)用方法類似,區(qū)別在于該鎖是不可重入的,在同一個線程中不可重入

3)可重入讀寫鎖Shared Reentrant Read Write Lock

類似JDK的ReentrantReadWriteLock. 一個讀寫鎖管理一對相關(guān)的鎖。一個負責(zé)讀操作,另外一個負責(zé)寫操作。讀操作在寫鎖沒被使用時可同時由多個進程使用,而寫鎖使用時不允許讀 (阻塞)。此鎖是可重入的。一個擁有寫鎖的線程可重入讀鎖,但是讀鎖卻不能進入寫鎖。這也意味著寫鎖可以降級成讀鎖, 比如請求寫鎖 —>讀鎖 —->釋放寫鎖。從讀鎖升級成寫鎖是不成的。主要由兩個類實現(xiàn):

  1. InterProcessReadWriteLock 
  2. InterProcessLock 

4)信號量Shared Semaphore

一個計數(shù)的信號量類似JDK的Semaphore。JDK中Semaphore維護的一組許可(permits),而Cubator中稱之為租約(Lease)。注意,所有的實例必須使用相同的numberOfLeases值。調(diào)用acquire會返回一個租約對象??蛻舳吮仨氃趂inally中close這些租約對象,否則這些租約會丟失掉。但是, 但是,如果客戶端session由于某種原因比如crash丟掉, 那么這些客戶端持有的租約會自動close, 這樣其它客戶端可以繼續(xù)使用這些租約。租約還可以通過下面的方式返還:

  1. public void returnAll(Collection<Lease> leases) 
  2. public void returnLease(Lease lease) 

注意一次你可以請求多個租約,如果Semaphore當(dāng)前的租約不夠,則請求線程會被阻塞。同時還提供了超時的重載方法:

  1. public Lease acquire() 
  2. public Collection<Lease> acquire(int qty) 
  3. public Lease acquire(long time, TimeUnit unit) 
  4. public Collection<Lease> acquire(int qty, long time, TimeUnit unit) 

主要類有:

  1. InterProcessSemaphoreV2 
  2. Lease 
  3. SharedCountReader 

5)多鎖對象Multi Shared Lock

Multi Shared Lock是一個鎖的容器。當(dāng)調(diào)用acquire, 所有的鎖都會被acquire,如果請求失敗,所有的鎖都會被release。同樣調(diào)用release時所有的鎖都被release(失敗被忽略)。基本上,它就是組鎖的代表,在它上面的請求釋放操作都會傳遞給它包含的所有的鎖。主要涉及兩個類:

  1. InterProcessMultiLock 
  2. InterProcessLock 

它的構(gòu)造函數(shù)需要包含的鎖的集合,或者一組ZooKeeper的path。

  1. public InterProcessMultiLock(List<InterProcessLock> locks) 
  2. public InterProcessMultiLock(CuratorFramework client, List<String> paths) 

柵欄

barrier1)DistributedBarrier構(gòu)造函數(shù)中barrierPath參數(shù)用來確定一個柵欄,只要barrierPath參數(shù)相同(路徑相同)就是同一個柵欄。通常情況下柵欄的使用如下:

1.主導(dǎo)client設(shè)置一個柵欄

2.其他客戶端就會調(diào)用waitOnBarrier()等待柵欄移除,程序處理線程阻塞

3.主導(dǎo)client移除柵欄,其他客戶端的處理程序就會同時繼續(xù)運行。

DistributedBarrier類的主要方法如下:

setBarrier() - 設(shè)置柵欄

waitOnBarrier() - 等待柵欄移除

removeBarrier() - 移除柵欄

2)雙柵欄Double Barrier

雙柵欄允許客戶端在計算的開始和結(jié)束時同步。當(dāng)足夠的進程加入到雙柵欄時,進程開始計算,當(dāng)計算完成時,離開柵欄。雙柵欄類是DistributedDoubleBarrier DistributedDoubleBarrier類實現(xiàn)了雙柵欄的功能。它的構(gòu)造函數(shù)如下:

  1. // client - the client 
  2. // barrierPath - path to use 
  3. // memberQty - the number of members in the barrier 
  4. public DistributedDoubleBarrier(CuratorFramework client, String barrierPath, int memberQty) 

memberQty是成員數(shù)量,當(dāng)enter方法被調(diào)用時,成員被阻塞,直到所有的成員都調(diào)用了enter。當(dāng)leave方法被調(diào)用時,它也阻塞調(diào)用線程,直到所有的成員都調(diào)用了leave。

注意:參數(shù)memberQty的值只是一個閾值,而不是一個限制值。當(dāng)?shù)却龞艡诘臄?shù)量大于或等于這個值柵欄就會打開!

與柵欄(DistributedBarrier)一樣,雙柵欄的barrierPath參數(shù)也是用來確定是否是同一個柵欄的,雙柵欄的使用情況如下:

1.從多個客戶端在同一個路徑上創(chuàng)建雙柵欄(DistributedDoubleBarrier),然后調(diào)用enter()方法,等待柵欄數(shù)量達到memberQty時就可以進入柵欄。

2.柵欄數(shù)量達到memberQty,多個客戶端同時停止阻塞繼續(xù)運行,直到執(zhí)行l(wèi)eave()方法,等待memberQty個數(shù)量的柵欄同時阻塞到leave()方法中。

3.memberQty個數(shù)量的柵欄同時阻塞到leave()方法中,多個客戶端的leave()方法停止阻塞,繼續(xù)運行。

DistributedDoubleBarrier類的主要方法如下:enter()、enter(long maxWait, TimeUnit unit) - 等待同時進入柵欄

leave()、leave(long maxWait, TimeUnit unit) - 等待同時離開柵欄

異常處理:DistributedDoubleBarrier會監(jiān)控連接狀態(tài),當(dāng)連接斷掉時enter()和leave方法會拋出異常。

計數(shù)器

Counters利用ZooKeeper可以實現(xiàn)一個集群共享的計數(shù)器。只要使用相同的path就可以得到最新的計數(shù)器值, 這是由ZooKeeper的一致性保證的。Curator有兩個計數(shù)器, 一個是用int來計數(shù),一個用long來計數(shù)。

1)SharedCount

這個類使用int類型來計數(shù)。主要涉及三個類。

  1. * SharedCount 
  2. * SharedCountReader 
  3. * SharedCountListener 

SharedCount代表計數(shù)器, 可以為它增加一個SharedCountListener,當(dāng)計數(shù)器改變時此Listener可以監(jiān)聽到改變的事件,而SharedCountReader可以讀取到最新的值, 包括字面值和帶版本信息的值VersionedValue。

2)DistributedAtomicLong

除了計數(shù)的范圍比SharedCount大了之外, 它首先嘗試使用樂觀鎖的方式設(shè)置計數(shù)器, 如果不成功(比如期間計數(shù)器已經(jīng)被其它client更新了), 它使用InterProcessMutex方式來更新計數(shù)值。此計數(shù)器有一系列的操作:

  • get(): 獲取當(dāng)前值
  • increment():加一
  • decrement(): 減一
  • add():增加特定的值
  • subtract(): 減去特定的值
  • trySet(): 嘗試設(shè)置計數(shù)值
  • forceSet(): 強制設(shè)置計數(shù)值

你必須檢查返回結(jié)果的succeeded(), 它代表此操作是否成功。如果操作成功, preValue()代表操作前的值, postValue()代表操作后的值。

End

Curator抽象和簡化了很多復(fù)雜的zookeeper操作,是zk使用者的福音。而要徹底的幸福,那就是不再使用它。

我不知道其他人把zk放在一個什么位置,但在我接觸paxos協(xié)議之后,就很難對它產(chǎn)生濃厚的興趣。一般在技術(shù)選型的時候,它會躺在我的備選列表最后,我甚至根本無法掌握源代碼里那些晦澀難懂的邏輯。

但工程建設(shè)從來不以我們的喜好來進行衡量。從來如此。

作者簡介:小姐姐味道 (xjjdog),一個不允許程序員走彎路的公眾號。聚焦基礎(chǔ)架構(gòu)和Linux。十年架構(gòu),日百億流量,與你探討高并發(fā)世界,給你不一樣的味道

責(zé)任編輯:武曉燕 來源: 小姐姐味道
相關(guān)推薦

2010-05-12 15:46:51

Subversion客

2010-06-01 14:11:11

TortoiseSVN

2011-08-17 10:10:59

2009-03-04 10:27:50

客戶端組件桌面虛擬化Xendesktop

2010-04-08 15:35:13

Oracle 簡易客戶

2012-10-11 17:02:02

IBMdw

2011-03-21 14:53:36

Nagios監(jiān)控Linux

2011-04-06 14:24:20

Nagios監(jiān)控Linux

2010-03-18 16:49:43

Java Socket

2013-06-08 09:59:15

VMwarevSphere Web

2009-07-24 17:31:56

ASP.NET AJA

2010-05-31 15:55:42

2012-01-13 10:29:37

ibmdw

2011-04-06 14:24:27

Nagios監(jiān)控Linux

2010-06-01 13:54:42

TortoiseSVN

2010-05-26 09:26:43

Cassandra

2009-08-21 16:14:52

服務(wù)端與客戶端通信

2010-03-18 15:44:22

VSTS 2010VS 2010

2021-04-30 08:19:32

SpringCloud客戶端負載Ribbo

2010-05-14 16:11:52

Subversion命
點贊
收藏

51CTO技術(shù)棧公眾號