ZooKeeper--一个具有高可用性的高性能协调服务

ZooKeeper是什么

ZooKeeper是一个具有高可用性的高性能协调服务。

ZooKeeper维护着一个树形层次结构,书中的节点被称为znode。znode可以用来存储数据,并且有一个与之相关联的ACL(权限),znode不能大于1M。

ZooKeeper 的详细介绍:请点这里
ZooKeeper 的下载地址:请点这里

相关阅读

ZooKeeper使用场景

ZooKeeper主要用来解决分布式系统中的“部分失败”问题。部分失败是分布式系统的固有的特征,ZooKeeper不能根除部分失败,也不会隐藏部分失败;但是可以提供一组工具,使你在构建分布式应用时对部分失败进行处理。这主要利用的是观察者模式。

Hadoop内置了ZooKeeper,阿里的开源框架Dubbo、Otter等也都是用ZooKeeper作为协调服务。

安装和运行ZooKeeper

安装

wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz

tar zxvf zookeeper-3.3.6.tar.gz

cd zookeeper-3.3.6

cp conf/zoo_sample.cfg conf/zoo.cfg

运行

bin/zkServer.sh start

测试是否正确运行:echo ruok | nc localhost 2181,如果启动成功,则返回imok

zkCli.sh命令行工具

bin/zkCli.sh -server localhost,连接到ZooKeeper,可以执行各种znode操作。

Java开发的znode操作代码

import java.io.IOException;
import java.util.List;
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;

public class ZkTest implements Watcher
{
 private static final int SESSION_TIMEOUT = 5000;
 private ZooKeeper zk;
 private CountDownLatch connectedSignal = new CountDownLatch(1);

 public void connect(String hosts) throws IOException,InterruptedException
 {
  zk = new ZooKeeper(hosts,SESSION_TIMEOUT,this);
  connectedSignal.await();
 }
 @Override
 public void process(WatchedEvent event)
 {
  if(event.getState() == KeeperState.SyncConnected)
  {
   connectedSignal.countDown();
  }
 }
 //新建表示组的znode
 public void create(String groupName) throws KeeperException,InterruptedException
 {
  String path = "/" + groupName;
  String createdPath = zk.create(path,null/*data*/,Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
  System.out.println("Created " + createdPath);
 }
 //用户将成员加入组中:临时成员
 public void join(String groupName,String memberName) throws KeeperException,InterruptedException
 {
  String path = "/" + groupName + "/" + memberName;
  String createdPath = zk.create(path,null/*data*/,Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
  System.out.println("Joined " + createdPath);
 }
 //列出组成员
 public void list(String groupName) throws KeeperException,InterruptedException
 {
  String path = "/" + groupName;
  try
  {
   List<String> children = zk.getChildren(path,false);
   if(children.isEmpty())
   {
    System.out.printf("No members in group %s\n",groupName);
    System.exit(1);
   }
   for(String child:children)
   {
    System.out.println(child);
   }
  }
  catch (KeeperException.NoNodeException e)
  {
   System.out.printf("No members in group %s\n",groupName);
   System.exit(1);
  }
 }
 //删除一个组及其所有成员
 public void delete(String groupName) throws KeeperException,InterruptedException
 {
  String path = "/" + groupName;
  try
  {
   List<String> children = zk.getChildren(path,false);
   for(String child:children)
   {
    zk.delete(path + "/" + child,-1);
   }
   zk.delete(path,-1);
  }
  catch (KeeperException.NoNodeException e)
  {
   System.out.printf("Group %s does not exist\n",groupName);
   System.exit(1);
  }
 }
 public void close() throws InterruptedException
 {
  zk.close();
 }
 public static void main(String[] args) throws Exception
 {
  ZkTest zkTest = new ZkTest();
  zkTest.connect("192.168.211.230");
  //zkTest.create("longlonggroup");
  //zkTest.join("longlonggroup", "aaa");
  //Thread.sleep(60000); //这里睡60秒是为了让你通过zkCli.sh之类的工具看到/creategroup下的aaa这个临时节点
  //zkTest.list("CMS");
  //zkTest.delete("longlonggroup");
  zkTest.close();
 }
}

相关推荐