你的位置:首页 > 软件开发 > Java > 分布式设计与开发(三)

分布式设计与开发(三)

发布时间:2015-10-18 22:20:05
分布式环境中大多数服务是允许部分失败,也允许数据不一致,但有些最基础的服务是需要高可靠性,高一致性的,这些服务是其他分布式服务运转的基础,比如naming service、分布式lock等,这些分布式的基础服务有以下要求:高可用性高一致性高性能对于这种有些挑战CAP原则 的服务该 ...

分布式设计与开发(三)

分布式环境中大多数服务是允许部分失败,也允许数据不一致,但有些最基础的服务是需要高可靠性,高一致性的,这些服务是其他分布式服务运转的基础,比如naming service、分布式lock等,这些分布式的基础服务有以下要求:

  • 高可用性
  • 高一致性
  • 高性能

对于这种有些挑战CAP原则 的服务该如何设计,是一个挑战,也是一个不错的研究课题,Apache的ZooKeeper也许给了我们一个不错的答案。ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务, 它暴露了一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等。关于ZooKeeper更多信息可以参见 官方文档

ZooKeeper的基本使用

搭一个分布式的ZooKeeper环境比较简单,基本步骤如下:

1)在各服务器安装 ZooKeeper

下载ZooKeeper后在各服务器上进行解压即可

tar -xzf zookeeper-3.2.2.tar.gz

2)配置集群环境

分别各服务器的zookeeper安装目录下创建名为zoo.cfg的配置文件,内容填写如下:

[xhtml] view plaincopy
  1. # The number of milliseconds of each tick  
  2. tickTime=2000  
  3. # The number of ticks that the initial  
  4. # synchronization phase can take  
  5. initLimit=10  
  6. # The number of ticks that can pass between  
  7. # sending a request and getting an acknowledgement  
  8. syncLimit=5  
  9. # the directory where the snapshot is stored.  
  10. dataDir=/home/admin/zookeeper-3.2.2/data  
  11. # the port at which the clients will connect  
  12. clientPort=2181  
  13. server.1=zoo1:2888:3888  
  14. server.2=zoo2:2888:3888  
  1. public static void main(String[] args) {  
  2.     try {  
  3.         ZooKeeper zk = new ZooKeeper("10.20.147.35:2181", 30000, null);  
  4.         String name = zk.create("/company", "alibaba".getBytes(),  
  5.                 Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);  
  6.         Stat stat = new Stat();  
  7.         System.out.println(new String(zk.getData(name, null, stat)));  
  8.         zk.setData(name, "taobao".getBytes(), stat.getVersion(), null, null);  
  9.         System.out.println(new String(zk.getData(name, null, stat)));  
  10.         stat = zk.exists(name, null);  
  11.         zk.delete(name, stat.getVersion(), null, null);  
  12.         System.out.println(new String(zk.getData(name, null, stat)));  
  13.     } catch (Exception e) {  
  14.         e.printStackTrace();  
  15.     }  
  16. }  
  1. /** 
  2.  * Removes the head of the queue and returns it, blocks until it succeeds. 
  3.  * @return The former head of the queue 
  4.  * @throws NoSuchElementException 
  5.  * @throws KeeperException 
  6.  * @throws InterruptedException 
  7.  */  
  8. public byte[] take() throws KeeperException, InterruptedException {  
  9.     TreeMap<Long,String> orderedChildren;  
  10.     // Same as for element.  Should refactor this.  
  11.     while(true){  
  12.         LatchChildWatcher childWatcher = new LatchChildWatcher();  
  13.         try{  
  14.             orderedChildren = orderedChildren(childWatcher);  
  15.         }catch(KeeperException.NoNodeException e){  
  16.             zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);  
  17.             continue;  
  18.         }  
  19.         if(orderedChildren.size() == 0){  
  20.             childWatcher.await();  
  21.             continue;  
  22.         }  
  23.         for(String headNode : orderedChildren.values()){  
  24.             String path = dir +"/"+headNode;  
  25.             try{  
  26.                 byte[] data = zookeeper.getData(path, false, null);  
  27.                 zookeeper.delete(path, -1);  
  28.                 return data;  
  29.             }catch(KeeperException.NoNodeException e){  
  30.                 // Another client deleted the node first.  
  31.             }  
  32.         }  
  33.     }  
  34. }  
  35. /** 
  36.  * Inserts data into queue. 
  37.  * @param data 
  38.  * @return true if data was successfully added 
  39.  */  
  40. public boolean offer(byte[] data) throws KeeperException, InterruptedException{  
  41.     for(;;){  
  42.         try{  
  43.             zookeeper.create(dir+"/"+prefix, data, acl, CreateMode.PERSISTENT_SEQUENTIAL);  
  44.             return true;  
  45.         }catch(KeeperException.NoNodeException e){  
  46.             zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);  
  47.         }  
  48.     }  
  49. }  

原标题:分布式设计与开发(三)

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录