Curator客戶端基本使用
阿新 • • 發佈:2018-12-17
Curator是Netflix公司開源的一套zookeeper客戶端框架,解決了很多Zookeeper客戶端非常底層的細節開發工作,包括連線重連、反覆註冊Watcher和NodeExistsException異常等。
在編寫程式碼之前,你的環境需要有安裝並啟動zookeeper服務, 接下來正式開始程式設計。
- 新增Maven相關的依賴
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency>
- 編寫zookeeper的客戶端
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.data.Stat; import java.util.ArrayList; import java.util.List; public class CuratorClient { private CuratorFramework client; public CuratorClient(String zkAddress, int sessionTimeoutMs, int connectionTimeoutMs) { this.client = connectionZookeeper(zkAddress, sessionTimeoutMs, connectionTimeoutMs); } public CuratorClient(String zkAddress) { this.client = connectionZookeeper(zkAddress, 12000, 12000); } private CuratorFramework connectionZookeeper(String zkAddress, int sessionTimeMs, int connectionTimeoutMs) { ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 5); CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(zkAddress) .sessionTimeoutMs(sessionTimeMs) .connectionTimeoutMs(connectionTimeoutMs) .retryPolicy(retryPolicy) .build(); client.start(); return client; } public CuratorFramework getClient() { return this.client; } /** * 建立一個節點 */ public void createNode(String nodePath, byte[] nodeData, CreateMode createMode) { try { client.create() .creatingParentContainersIfNeeded() .withMode(createMode) .forPath(nodePath, nodeData); } catch (Exception e) { System.out.println("Create znode failed, znode : " + nodePath); e.printStackTrace(); } } /** * 刪除一個子節點 */ public void deleteChildNode(String childNodePath) { try { client.delete().forPath(childNodePath); } catch (Exception e) { System.out.println("Delete childNode failed, znode : " + childNodePath); e.printStackTrace(); } } /** * 刪除一個父節點 */ public void deleteParentNode(String parentNodePath) { try { client.delete() .deletingChildrenIfNeeded() .forPath(parentNodePath); } catch (Exception e) { System.out.println("Delete parentNode failed, znode : " + parentNodePath); e.printStackTrace(); } } /** * 更新一個子節點 */ public void setNodeDate(String nodePath, byte[] nodeData) { try { client.setData().forPath(nodePath, nodeData); } catch (Exception e) { System.out.println("Update data failed, znode : " + nodePath); e.printStackTrace(); } } /** * 獲取一個子節點資料 */ public byte[] getNodeData(String nodePath) { byte[] data = null; try { data = client.getData().forPath(nodePath); } catch (Exception e) { System.out.println("Get data failed, znode : " + nodePath); e.printStackTrace(); } return data; } /** * 獲取一個父節點下所有子節點路徑 */ public List<String> getParentNodePath(String parentNodePath) { List<String> list = new ArrayList<String>(); try { list = client.getChildren().forPath(parentNodePath); } catch (Exception e) { System.out.println("Get children nodePath failed, znode : " + parentNodePath); e.printStackTrace(); } return list; } /** * 判斷當前節點是否存在 */ public boolean nodePathExists(String nodePath) { try { Stat stat = client.checkExists().forPath(nodePath); if (stat != null) { return true; } } catch (Exception e) { System.out.println("Get nodePath stat failed, znode : " + nodePath); e.printStackTrace(); } return false; } }
- 客戶端的使用測試
import org.apache.commons.lang.StringUtils; import org.apache.zookeeper.CreateMode; import java.io.Serializable; import java.util.List; public class WorkerRegister implements Serializable { public static void main(String[] args) { String zkAddress = "localhost:2181"; CuratorClient zkClient = new CuratorClient(zkAddress); //註冊當前節點資訊 String path = "/test/node1"; String data = "info:hello"; zkClient.createNode(path, data.getBytes(), CreateMode.EPHEMERAL); //獲取當前節點資訊 byte[] nodeData = zkClient.getNodeData(path); System.out.println("nodeData : " + new String(nodeData)); //更新當前節點資訊 data = "info:world"; zkClient.setNodeDate(path, data.getBytes()); //獲取父節點下所有子節點路徑 List<String> nodePaths = zkClient.getParentNodePath("/test"); System.out.println(StringUtils.join(nodePaths, ",")); } }
測試結果如下所示:
nodeData : info:hello node1