分布式环境中大多数服务是允许部分失败,也允许数据不一致,但有些最基础的服务是需要高可靠性,高一致性的,这些服务是其他分布式服务运转的基础,比如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
- # The number of milliseconds of each tick
- tickTime=2000
- # The number of ticks that the initial
- # synchronization phase can take
- initLimit=10
- # The number of ticks that can pass between
- # sending a request and getting an acknowledgement
- syncLimit=5
- # the directory where the snapshot is stored.
- dataDir=/home/admin/zookeeper-3.2.2/data
- # the port at which the clients will connect
- clientPort=2181
- server.1=zoo1:2888:3888
- server.2=zoo2:2888:3888
- public static void main(String[] args) {
- try {
- ZooKeeper zk = new ZooKeeper("10.20.147.35:2181", 30000, null);
- String name = zk.create("/company", "alibaba".getBytes(),
- Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
- Stat stat = new Stat();
- System.out.println(new String(zk.getData(name, null, stat)));
- zk.setData(name, "taobao".getBytes(), stat.getVersion(), null, null);
- System.out.println(new String(zk.getData(name, null, stat)));
- stat = zk.exists(name, null);
- zk.delete(name, stat.getVersion(), null, null);
- System.out.println(new String(zk.getData(name, null, stat)));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Removes the head of the queue and returns it, blocks until it succeeds.
- * @return The former head of the queue
- * @throws NoSuchElementException
- * @throws KeeperException
- * @throws InterruptedException
- */
- public byte[] take() throws KeeperException, InterruptedException {
- TreeMap<Long,String> orderedChildren;
- // Same as for element. Should refactor this.
- while(true){
- LatchChildWatcher childWatcher = new LatchChildWatcher();
- try{
- orderedChildren = orderedChildren(childWatcher);
- }catch(KeeperException.NoNodeException e){
- zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);
- continue;
- }
- if(orderedChildren.size() == 0){
- childWatcher.await();
- continue;
- }
- for(String headNode : orderedChildren.values()){
- String path = dir +"/"+headNode;
- try{
- byte[] data = zookeeper.getData(path, false, null);
- zookeeper.delete(path, -1);
- return data;
- }catch(KeeperException.NoNodeException e){
- // Another client deleted the node first.
- }
- }
- }
- }
- /**
- * Inserts data into queue.
- * @param data
- * @return true if data was successfully added
- */
- public boolean offer(byte[] data) throws KeeperException, InterruptedException{
- for(;;){
- try{
- zookeeper.create(dir+"/"+prefix, data, acl, CreateMode.PERSISTENT_SEQUENTIAL);
- return true;
- }catch(KeeperException.NoNodeException e){
- zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);
- }
- }
- }
原标题:分布式设计与开发(三)
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。