1. 程式人生 > 其它 >zookeeper通過JavaAPI相關操作

zookeeper通過JavaAPI相關操作

這裡用的Curator

1、工程專案結構

新建一個curator-zk的乾淨maven專案即可(不要勾選哈皮weapp模板)

2、新增相關依賴

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.31</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.31</version>
</dependency>

</dependencies>

3、log4j.properties檔案

配置log4j

log4j.rootLogger=off,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{yyyy-MM-dd HH/:mm/:ss}]%-5p %c(line/:%L) %x-%m%n

3、新建CuratorTest類

public class CuratorTest {
/**
* 建立連線
*/

private CuratorFramework client;

@Before // 在任何test執行之前先執行before
public void testConnect() {
//第一種方式
/*
connectString – 連線字串 zk server地址和埠 118.31.187.88:2181
sessionTimeoutMs – session timeout 會話超時時間
connectionTimeoutMs – connection timeout 連線超時時間
retryPolicy – retry policy to use 重試策略
*/
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//三秒一次,重試10次
// CuratorFramework client = CuratorFrameworkFactory.newClient("118.31.187.88:2181", 60 * 1000, 15 * 1000, retryPolicy);
// client.start();
//第二種方式
client = CuratorFrameworkFactory.builder()
.connectString("118.31.187.88:2181")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("ztytest")
.build();
client.start();
}

/**
* 建立節點:create 持久 臨時 順序 資料
* 1.基本建立 client.create().forPath("/test1");
* 2.建立節點 帶有資料 client.create().forPath("/test2", "success".getBytes());
* 3.設定節點型別 client.create().withMode(CreateMode.EPHEMERAL).forPath("/test3", "perfect".getBytes());
* 4.建立多級節點 client.create().creatingParentsIfNeeded().forPath("/test4/yasuo/hasaki");
*/

@Test
public void testCreate1() throws Exception {
//1.基本建立
//如果建立節點,沒有指定資料,則預設將當前客戶端的ip作為資料儲存
String path = client.create().forPath("/test1");
System.out.println(path);
}

@Test
public void testCreate2() throws Exception {
//2.建立節點,帶有資料
String path = client.create().forPath("/test2", "success".getBytes());
System.out.println(path);
}

@Test
public void testCreate3() throws Exception {
//2.設定節點型別
// 預設型別:持久化
//這裡設定臨時的
String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/test3", "perfect".getBytes());
System.out.println(path);
//這裡不迴圈的話,就會執行下面的after,臨時節點又會刪除
}

@Test
public void testCreate4() throws Exception {
//2.建立多級節點
//creatingParentsIfNeeded() 如果父級節點不存在,就建立父級節點
//客戶端無法通過 create /test4/yauso/hasaki 節點,因為父級節點為空,會報 not exit 錯誤
String path = client.create().creatingParentsIfNeeded().forPath("/test4/yasuo/hasaki");
System.out.println(path);
//這裡不迴圈的話,就會執行下面的after,臨時節點又會刪除
     while (true) {
     }
}

@After
public void close() {
if (client != null) {
client.close();
}
}

}