您好,登錄后才能下訂單哦!
下面,我們將通過一個簡單的示例演示curator最基本的crud功能:
maven依賴:
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
測試類:
package per.ym.zookeeper;
import java.util.List;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.BoundedExponentialBackoffRetry;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryForever;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.retry.RetryUntilElapsed;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CuratorBasicAPI {
private CuratorFramework client;
private String connectString = "192.168.61.131:2184";
private int sessionTimeoutMs = 60000;
private int connectionTimeoutMs = 60000;
private String path = "/test";
private String rootPath = "/";
private byte[] data = "data".getBytes();
private byte[] newData = "newData".getBytes();
private int baseSleepTimeMs = 1000;
private int maxRetries = 3;
@Before
public void connect() {
//baseSleepTimeMs:基礎睡眠時間;maxRetries:最大重試次數;maxSleepMs:兩次重試之間最大睡眠時間
//其睡眠時間計算公式為:sleepMs = baseSleepTimeMs * Math.max(1, random.nextInt(1 << (retryCount + 1)));
RetryPolicy retryPolicy1 = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
//ExponentialBackoffRetry的子類,重寫了父類的getSleepTimeMs方法
//其睡眠時間計算公式為:Math.min(maxSleepTimeMs, super.getSleepTimeMs(retryCount, elapsedTimeMs));
RetryPolicy retryPolicy2 = new BoundedExponentialBackoffRetry(baseSleepTimeMs, 50000, maxRetries);
//最大重試3次,每次相隔時間為5000毫秒
RetryPolicy retryPolicy3 = new RetryNTimes(3, 5000);
//只重試一次,RetryNTimes的子類,就是將次數指定為1
RetryPolicy retryPolicy4 = new RetryOneTime(3000);
//一直重試,重試間隔為3000毫秒
RetryPolicy retryPolicy5 = new RetryForever(3000);
//最大重試時間為20000毫秒,若超過就不重試了,每次間隔3000毫秒
RetryPolicy retryPolicy6 = new RetryUntilElapsed(20000, 3000);
//client = CuratorFrameworkFactory.newClient(connectString, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
//流式
client = CuratorFrameworkFactory.builder()
.connectString(connectString) //服務器列表,格式host1:port1,host2:port2,…
.sessionTimeoutMs(sessionTimeoutMs) //會話超時時間,單位毫秒,默認60000ms
.connectionTimeoutMs(connectionTimeoutMs) //連接創建超時時間,單位毫秒,默認60000ms
.retryPolicy(retryPolicy1)
.build();
//啟動
client.start();
}
@Test
public void showBasicAPI() throws Exception {
exist();
create();
exist();
getData();
setData();
getData();
getChildren();
delete();
getChildren();
}
@After
public void close() {
//關閉連接
client.close();
}
private void create() throws Exception {
//創建一個節點
//client.create().forPath(path);
//創建一個節點附帶數據
client.create().forPath(path, data);
System.out.println("創建節點:" + path);
//創建一個臨時節點,默認不指定是持久的,另外還有持久帶序列和臨時帶序列PERSISTENT_SEQUENTIAL、EPHEMERAL_SEQUENTIAL
//client.create().withMode(CreateMode.EPHEMERAL).forPath(path);
//如果沒有父目錄則一起創建
//client.create().creatingParentsIfNeeded().forPath(path);
//后天模式,還可以傳入自定義CallBack和線程池
//client.create().inBackground().forPath(path);
//帶權限的
//List<ACL> aclList = new ArrayList<ACL>();
//acls.add(new ACL(Perms.ALL, new Id("digest", "520:1314")));
//client.create().withACL(aclList).forPath(path);
//當然你也這可這樣
//client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).withACL(aclList).inBackground().forPath(path, data);
}
private void exist() throws Exception {
Stat stat = client.checkExists().forPath(path);
if (stat == null) {
System.out.println("節點" + path + "不存在。");
} else {
System.out.println("節點" + path + "存在。");
}
}
private void getData() throws Exception {
byte[] byteData = client.getData().forPath(path);
//client.getData().inBackground().forPath(path);
//Stat stat = new Stat();
//client.getData().storingStatIn(stat).forPath("path");
System.out.println("節點" + path + "的數據為:" + new String(byteData));
}
private void setData() throws Exception {
client.setData().forPath(path, newData);
System.out.println("設置節點" + path + "數據為:" + new String(newData));
//client.setData().inBackground().forPath(path, newData);
//指定版本,若版本錯誤將拋出異常bad version
//client.setData().withVersion(520).forPath(path,newData);
}
private void delete() throws Exception {
client.delete().forPath(path);
System.out.println("刪除節點:" + path);
//client.delete().withVersion(1314).forPath(path);
//只要會話有效將一直嘗試刪除,直到刪除成功
//client.delete().guaranteed().forPath(path);
//同
//client.delete().deletingChildrenIfNeeded().forPath(path);
//client.delete().inBackground().forPath(path);
//client.delete().guaranteed().deletingChildrenIfNeeded().withVersion(1314).inBackground().forPath(path);
}
private void getChildren() throws Exception {
List<String> names = client.getChildren().forPath(rootPath);
System.out.println("根目錄下有節點:" + names);
//client.getChildren().inBackground().forPath(rootPath);
}
}
運行結果:
節點/test不存在。
創建節點:/test
節點/test存在。
節點/test的數據為:data
設置節點/test數據為:newData
節點/test的數據為:newData
根目錄下有節點:[zookeeper, test]
刪除節點:/test
根目錄下有節點:[zookeeper]
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。