文档中心 V3.0
Zookeeper

Zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

使用示例

接下来,我们将通过本教程介绍如何使用Zookeeper服务。步骤包括:
1.在您的MoPaaS应用管理界面中创建绑定Zookeeper服务。
2.代码示例

创建Zookeeper服务过程

在应用列表页面,选择某个应用,进入应用控制台。点击“服务详情”可以查看并添加当前支持的所有服务。创建Zookeeper步骤如下:
1、选择Zookeeper–>输入服务名称,选择存储空间大小–>点击创建按钮

Zookeeper-1.png
Zookeeper-2.png

2、创建成功后,将在您的服务列表中显示出Zookeeper。

Zookeeper-3.png

3、点击绑定按钮,使服务Zookeeper与当前应用绑定

Zookeeper-4.png

4、点击管理按钮进入Zookeeper详情页,显示了Zookeeper的任务信息。

Zookeeper-5.png

代码示例

Java:

package com.mydemo.zookeeper.test;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooKeeper.States;
import org.apache.zookeeper.data.Stat;
public class Conf {
public static void waitUntilConnected(ZooKeeper zooKeeper, CountDownLatch connectedLatch) {
if (States.CONNECTING == zooKeeper.getState()) {
try {
connectedLatch.await();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
static class ConnectedWatcher implements Watcher {
private CountDownLatch connectedLatch;
ConnectedWatcher(CountDownLatch connectedLatch) {
this.connectedLatch = connectedLatch;
}
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedLatch.countDown();
}
}
}
static public Conf Instance(){
if(static_ == null){
static_ = new Conf();
}
return static_;
}
public boolean Init(String hostports, int times){
try{
CountDownLatch connectedLatch = new CountDownLatch(1);
Watcher watcher = new ConnectedWatcher(connectedLatch);
zk_ = new ZooKeeper(hostports, times, watcher);
waitUntilConnected(zk_, connectedLatch);
}
catch(Exception e){
System.out.println(e);
return false;
}
return true;
}
public String Get(String keys){
String re = "";
String ppath = "/zookeeper";
int oldpos = -1;
int pos = 0;
while(true){
pos = keys.indexOf(".", oldpos + 1);
if(pos < 0){
ppath += "/";
String str = keys.substring(oldpos + 1);
ppath += str;
break;
}
ppath += "/";
String str = keys.substring(oldpos + 1, pos);
ppath += str;
oldpos = pos;
}
Stat stat = new Stat();
try{
byte[] b = zk_.getData(ppath, false, stat); //获取节点的信息及存储的数据
re = new String(b);
}
catch(Exception e){
System.out.println(e);
}
return re;
}
public void create(){
try {
zk_.addAuthInfo("digest", "super:superpw".getBytes());
zk_.create("/testRootPath4", "testRootData3".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
// 创建一个子目录节点
zk_.create("/testRootPath4/testChildPathOne", "testChildDataOne".getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println(new String(zk_.getData("/testRootPath4",false,null)));
// 取出子目录节点列表
System.out.println(zk_.getChildren("/testRootPath4",true));
// 修改子目录节点数据
zk_.setData("/testRootPath4/testChildPathOne","modifyChildDataOne".getBytes(),-1);
System.out.println("目录节点状态:["+zk_.exists("/testRootPath4",true)+"]");
// 创建另外一个子目录节点
zk_.create("/testRootPath4/testChildPathTwo", "testChildDataTwo".getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println(new String(zk_.getData("/testRootPath4/testChildPathTwo",true,null)));
// 删除子目录节点
zk_.delete("/testRootPath4/testChildPathTwo",-1);
zk_.delete("/testRootPath4/testChildPathOne",-1);
// 删除父目录节点
zk_.delete("/testRootPath4",-1);
// 关闭连接
zk_.close();
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Conf(){
}
private ZooKeeper zk_;
static private Conf static_;
public static void main(String args[]){
String hostports = "10.0.2.15:35076";
Conf.Instance().Init(hostports, 1000);
Conf.Instance().create();
while(true){
try{Thread.sleep(100);}
catch(Exception e){
}
}
}
}