apache curator怎麼操作zookeeper?
(學習影片分享:程式設計影片)
首先來簡單介紹下apache curator。
Apache Curator是Apache ZooKeeper的Java / JVM客戶端函式庫,Apache ZooKeeper是一種分散式協調服務。它包括一個高級API框架和實用程序,使Apache ZooKeeper更容易和更可靠。它還包括常見用例和擴充功能(如服務發現和Java 8非同步DSL)的配方。
官網:http://curator.apache.org/index.html
Curator專案元件(下載官方原始碼就可以看到以下元件)
Maven依賴(網址:https://search.maven.org/search?q=org.apache.curator)
##分散式鎖定實現<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.1.0</version> </dependency>
public static void main(String[] args) { String zookeeperConnectionString = "localhost:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); try { //创建分布式锁, 锁空间的根节点路径为/curator/lock InterProcessMutex lock = new InterProcessMutex(client, "/curator/lock"); if ( lock.acquire(1000, TimeUnit.SECONDS) ) { try { // do some work inside of the critical section here System.out.println("do some work inside of the critical section here"); } finally { //完成业务流程, 释放锁 lock.release(); } } } catch (Exception e) { e.printStackTrace(); } }相關推薦:
以上是apache curator怎麼操作zookeeper的詳細內容。更多資訊請關注PHP中文網其他相關文章!