Java程式操作Zookeeper
1.建立java項目并導入相關jar包
主要jar包在主目錄下
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iMldjY4MTMzM2M4QmN1YmY0gTM0YzMyUjM2U2N1EjM18CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2.API簡單使用
2.1配置Zookeeper對象
// zookeeper的伺服器位址
private String connectString = "192.168.88.121:2181,192.168.88.122:2181,192.168.88.123:2181";
// 連接配接逾時時間
private int sessionTimeout = 2000;
private ZooKeeper zk = null;
/**
* 設定zookeeper對象
* @throws IOException
*/
@Before
public void setZookeeper() throws IOException {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
/**
* 事件觸發的回調方法
*/
@Override
public void process(WatchedEvent event) {
}
});
System.out.println("---"+zk);
}
常用API操作
/**
* create 方法參數
* 第一個參數 路徑
* 第二個參數 值 bytes
* 第三個參數 對節點的通路控制
* 第四個參數 節點的類型 短暫 永久 序号
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void create() throws KeeperException, InterruptedException {
String path = zk.create("/app3", "123".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(path);
}
/**
* 判斷節點是否存在
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void exist() throws KeeperException, InterruptedException{
// 設定為 true 會調用 zk中的監聽器
Stat stat = zk.exists("/app1", true);
if(stat!=null){
System.out.println("存在。。。。"+stat.getDataLength());
}else{
System.out.println("不存在。。。");
}
}
/**
* 擷取子節點
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void getChilren() throws KeeperException, InterruptedException{
List<String> list = zk.getChildren("/", true);
for (String s : list) {
System.out.println(s);
}
}
/**
* 擷取節點的内容
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void getData() throws KeeperException, InterruptedException{
byte[] b = zk.getData("/app1", false, null );
System.out.println(new String(b));
}
/**
* 修改節點内容
* version -1 自動維護
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void setData() throws KeeperException, InterruptedException{
Stat s = zk.setData("/app1", "test".getBytes(), -1);
}
/**
* 删除節點
*
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void deleteNode() throws InterruptedException, KeeperException{
zk.delete("/app1", -1);
}
3.監聽器的使用
@Before
public static void setUpBeforeClass() throws Exception {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
/**
* 監聽器
*/
@Override
public void process(WatchedEvent event) {
System.out.println("事件觸發...");
}
});
}
/**
* 擷取子節點
* getChildren(path,watch?)監聽的事件是:節點下的子節點增減變化事件
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void getChilren() throws KeeperException, InterruptedException{
List<String> list = zk.getChildren("/", new Watcher() {
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
System.out.println("監控節點下的子節點被改變..."+event.getPath());
}
});
for (String s : list) {
System.out.println(s);
}
Thread.sleep(Long.MAX_VALUE);
}
/**
* 擷取節點的内容
* getData(path,watch?)監聽的事件是:節點資料變化事件
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void getData() throws KeeperException, InterruptedException{
byte[] b = zk.getData("/app1", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("--資料改變了--");
}
}, null );
System.out.println(new String(b));
Thread.sleep(Long.MAX_VALUE);
}
~zookeeper的javaAPI操作還是比較簡單的,本文就介紹到這裡