ZooKeeper学习笔记十一 ZooKeeper典型应用场景——分布式队列

本文学习资源来自《从Paxos到ZooKeeper分布式一致性原理与实践》 电子工业出版社

分布式队列,简单地讲有两大类:一种是常规的先入先出队列,另一种则是要等到队列元素集聚之后才统一安排执行的Barrier模型 (同步队列)。

FIFO:先入先出

先入先出的算法思想,以其简单明了的特点,广泛应用于计算机科学的各个方面。而FIFO队列也是一种非常典型且应用广泛的按序执行的队列模型;先进入队列的请求操作先完成后,才会开始处理后面的请求。

使用ZooKeeper实现FIFO队列,和共享锁的实现非常类似。FIFO队列就类似于一个全写的共享锁模型,大体设计思路:
所有客户端都会到/queue_fifo这个节点下面创建一个临时顺序节点,例如/queue_fifo/192.168.0.1-00000001,
创建完节点之后,根据如下4个步骤来确定执行顺序:

  1. 通过调用getChildren()接口来获取/queue_fifo节点下的所有子节点,即获取队列中所有的元素
  2. 确定自己的节点序号在所有子节点中的顺序
  3. 如果自己不是序号最小的子节点,那么就需要进入等待,同时向比自己序号小的最后一个节点注册Watcher监听。
  4. 接收到Wathcer通知后,重复步骤1

代码示例:
这里写图片描述

DistributedQueue

package zookeeper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.I0Itec.zkclient.ExceptionUtil;
import org.I0Itec.zkclient.exception.ZkNoNodeException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 分布式队列,同步队列的实现
 * 
 * @author linbingwen
 *
 * @param <T>
 */
public class DistributedQueue<T> {
    private static Logger logger = LoggerFactory.getLogger(DistributedQueue.class);

    protected final ZooKeeper zooKeeper;// 用于操作zookeeper集群
    protected final String root;// 代表根节点
    private int queueSize;
    private String startPath = "/queue/start";

    protected static final String Node_NAME = "n_";// 顺序节点的名称

    public DistributedQueue(ZooKeeper zooKeeper, String root, int queueSize) {
        this.zooKeeper = zooKeeper;
        this.root = root;
        this.queueSize = queueSize;
        init();
    }

    /**
     * 初始化根目录
     */
    private void init() {
        try {
            Stat stat = zooKeeper.exists(root, false);// 判断一下根目录是否存在
            if (stat == null) {
                zooKeeper.create(root, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            zooKeeper.delete(startPath, -1); // 删除队列满的标志
        } catch (Exception e) {
            logger.error("create rootPath error", e);
        }
    }

    /**
     * 获取队列的大小
     * 
     * @return
     * @throws Exception
     */
    public int size() throws Exception {
        return zooKeeper.getChildren(root, false).size();
    }

    /**
     * 判断队列是否为空
     * 
     * @return
     * @throws Exception
     */
    public boolean isEmpty() throws Exception {
        return zooKeeper.getChildren(root, false).size() == 0;
    }

    /**
     * bytes 转object
     * 
     * @param bytes
     * @return
     */
    private Object ByteToObject(byte[] bytes) {
        Object obj = null;
        try {
            // bytearray to object
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);

            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
            logger.error("translation" + e.getMessage());
            e.printStackTrace();
        }
        return obj;
    }

    /**
     * Object 转byte
     * 
     * @param obj
     * @return
     */
    private byte[] ObjectToByte(java.lang.Object obj) {
        byte[] bytes = null;
        try {
            // object to bytearray
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);

            bytes = bo.toByteArray();

            bo.close();
            oo.close();
        } catch (Exception e) {
            logger.error("translation" + e.getMessage());
            e.printStackTrace();
        }
        return bytes;
    }

    /**
     * 向队列提供数据,队列满的话会阻塞等待直到start标志位清除
     * 
     * @param element
     * @return
     * @throws Exception
     */
    public boolean offer(T element) throws Exception {
        // 构建数据节点的完整路径
        String nodeFullPath = root.concat("/").concat(Node_NAME);
        try {
            if (queueSize > size()) {
                // 创建持久的节点,写入数据
                zooKeeper.create(nodeFullPath, ObjectToByte(element), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                        CreateMode.PERSISTENT);
                // 再判断一下队列是否满
                if (queueSize > size()) {
                    zooKeeper.delete(startPath, -1); // 确保不存在
                } else {
                    zooKeeper.create(startPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }
            } else {
                // 创建队列满的标记
                if (zooKeeper.exists(startPath, false) != null) {
                    zooKeeper.create(startPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }

                final CountDownLatch latch = new CountDownLatch(1);
                final Watcher previousListener = new Watcher() {
                    public void process(WatchedEvent event) {
                        if (event.getType() == EventType.NodeDeleted) {
                            latch.countDown();
                        }
                    }
                };

                // 如果节点不存在会出现异常
                zooKeeper.exists(startPath, previousListener);
                latch.await();
                offer(element);

            }
        } catch (ZkNoNodeException e) {
            logger.error("", e);
        } catch (Exception e) {
            throw ExceptionUtil.convertToRuntimeException(e);
        }
        return true;
    }

    /**
     * 从队列取数据,当有start标志位时,开始取数据,全部取完数据后才删除start标志
     * 
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public T poll() throws Exception {

        try {
            // 队列还没满
            if (zooKeeper.exists(startPath, false) == null) {
                final CountDownLatch latch = new CountDownLatch(1);
                final Watcher previousListener = new Watcher() {
                    public void process(WatchedEvent event) {
                        if (event.getType() == EventType.NodeCreated) {
                            latch.countDown();
                        }
                    }
                };

                // 如果节点不存在会出现异常
                zooKeeper.exists(startPath, previousListener);

                // 如果节点不存在会出现异常
                latch.await();
            }

            List<String> list = zooKeeper.getChildren(root, false);
            if (list.size() == 0) {
                return null;
            }
            // 将队列按照由小到大的顺序排序
            Collections.sort(list, new Comparator<String>() {
                public int compare(String lhs, String rhs) {
                    return getNodeNumber(lhs, Node_NAME).compareTo(getNodeNumber(rhs, Node_NAME));
                }
            });

            /**
             * 将队列中的元素做循环,然后构建完整的路径,在通过这个路径去读取数据
             */
            for (String nodeName : list) {
                String nodeFullPath = root.concat("/").concat(nodeName);
                try {
                    T node = (T) ByteToObject(zooKeeper.getData(nodeFullPath, false, null));
                    zooKeeper.delete(nodeFullPath, -1);
                    return node;
                } catch (ZkNoNodeException e) {
                    logger.error("", e);
                }
            }
            return null;
        } catch (Exception e) {
            throw ExceptionUtil.convertToRuntimeException(e);
        }

    }

    /**
     * 截取节点的数字的方法
     * 
     * @param str
     * @param nodeName
     * @return
     */
    private String getNodeNumber(String str, String nodeName) {
        int index = str.lastIndexOf(nodeName);
        if (index >= 0) {
            index += Node_NAME.length();
            return index <= str.length() ? str.substring(index) : "";
        }
        return str;

    }

}

DistributedQueueTest

package zookeeper;

import java.io.Serializable;  

import org.I0Itec.zkclient.ZkClient;  
import org.I0Itec.zkclient.serialize.SerializableSerializer;  

public class DistributedQueueTest {  

   public static void main(String[] args) {  
       ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000, 5000, new SerializableSerializer());  
       DistributedSimpleQueue<SendObject> queue = new DistributedSimpleQueue<SendObject>(zkClient, "/Queue");  
       new Thread(new ConsumerThread(queue)).start();  
       new Thread(new ProducerThread(queue)).start();  

   }  

}  

class ConsumerThread implements Runnable {  
   private DistributedSimpleQueue<SendObject> queue;  

   public ConsumerThread(DistributedSimpleQueue<SendObject> queue) {  
       this.queue = queue;  
   }  

   public void run() {  
       for (int i = 0; i < 10000; i++) {  
           try {  
               Thread.sleep((int) (Math.random() * 5000));// 随机睡眠一下  
               SendObject sendObject = (SendObject) queue.poll();  
               System.out.println("消费一条消息成功:" + sendObject);  
           } catch (Exception e) {  
           }  
       }  
   }  
}  

class ProducerThread implements Runnable {  

   private DistributedSimpleQueue<SendObject> queue;  

   public ProducerThread(DistributedSimpleQueue<SendObject> queue) {  
       this.queue = queue;  
   }  

   public void run() {  
       for (int i = 0; i < 10000; i++) {  
           try {  
               Thread.sleep((int) (Math.random() * 5000));// 随机睡眠一下  
               SendObject sendObject = new SendObject(String.valueOf(i), "content" + i);  
               queue.offer(sendObject);  
               System.out.println("发送一条消息成功:" + sendObject);  
           } catch (Exception e) {  
           }  
       }  
   }  

}  

class SendObject implements Serializable {  

   private static final long serialVersionUID = 1L;  

   public SendObject(String id, String content) {  
       this.id = id;  
       this.content = content;  
   }  

   private String id;  

   private String content;  

   public String getId() {  
       return id;  
   }  

   public void setId(String id) {  
       this.id = id;  
   }  

   public String getContent() {  
       return content;  
   }  

   public void setContent(String content) {  
       this.content = content;  
   }  

   @Override  
   public String toString() {  
       return "SendObject [id=" + id + ", content=" + content + "]";  
   }  

}  

DistributedSimpleQueue

package zookeeper;

import java.util.Collections;  
import java.util.Comparator;  
import java.util.List;  
import org.I0Itec.zkclient.ExceptionUtil;  
import org.I0Itec.zkclient.ZkClient;  
import org.I0Itec.zkclient.exception.ZkNoNodeException;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  


/** 
 * 分布式队列,生产者,消费者的实现 
 * @author linbingwen 
 * 
 * @param <T> 
 */  
public class DistributedSimpleQueue<T> {  

    private static Logger logger = LoggerFactory.getLogger(DistributedSimpleQueue.class);  

    protected final ZkClient zkClient;//用于操作zookeeper集群  
    protected final String root;//代表根节点  

    protected static final String Node_NAME = "n_";//顺序节点的名称  



    public DistributedSimpleQueue(ZkClient zkClient, String root) {  
        this.zkClient = zkClient;  
        this.root = root;  
    }  

    //获取队列的大小  
    public int size() {  
        /** 
         * 通过获取根节点下的子节点列表 
         */  
        return zkClient.getChildren(root).size();  
    }  

    //判断队列是否为空  
    public boolean isEmpty() {  
        return zkClient.getChildren(root).size() == 0;  
    }  

    /** 
     * 向队列提供数据 
     * @param element 
     * @return 
     * @throws Exception 
     */  
    public boolean offer(T element) throws Exception{  

        //构建数据节点的完整路径  
        String nodeFullPath = root .concat( "/" ).concat( Node_NAME );  
        try {  
            //创建持久的节点,写入数据  
            zkClient.createPersistentSequential(nodeFullPath , element);  
        }catch (ZkNoNodeException e) {  
            zkClient.createPersistent(root);  
            offer(element);  
        } catch (Exception e) {  
            throw ExceptionUtil.convertToRuntimeException(e);  
        }  
        return true;  
    }  


    //从队列取数据  
    @SuppressWarnings("unchecked")  
    public T poll() throws Exception {  

        try {  

            List<String> list = zkClient.getChildren(root);  
            if (list.size() == 0) {  
                return null;  
            }  
            //将队列按照由小到大的顺序排序  
            Collections.sort(list, new Comparator<String>() {  
                public int compare(String lhs, String rhs) {  
                    return getNodeNumber(lhs, Node_NAME).compareTo(getNodeNumber(rhs, Node_NAME));  
                }  
            });  

            /** 
             * 将队列中的元素做循环,然后构建完整的路径,在通过这个路径去读取数据 
             */  
            for ( String nodeName : list ){  

                String nodeFullPath = root.concat("/").concat(nodeName);      
                try {  
                    T node = (T) zkClient.readData(nodeFullPath);  
                    zkClient.delete(nodeFullPath);  
                    return node;  
                } catch (ZkNoNodeException e) {  
                    logger.error("",e);  
                }  
            }  

            return null;  

        } catch (Exception e) {  
            throw ExceptionUtil.convertToRuntimeException(e);  
        }  

    }  


    private String getNodeNumber(String str, String nodeName) {  
        int index = str.lastIndexOf(nodeName);  
        if (index >= 0) {  
            index += Node_NAME.length();  
            return index <= str.length() ? str.substring(index) : "";  
        }  
        return str;  

    }  

}  

运行结果:

发送一条消息成功:SendObject [id=0, content=content0]
消费一条消息成功:SendObject [id=0, content=content0]
消费一条消息成功:null
发送一条消息成功:SendObject [id=1, content=content1]
消费一条消息成功:SendObject [id=1, content=content1]
发送一条消息成功:SendObject [id=2, content=content2]
消费一条消息成功:SendObject [id=2, content=content2]
发送一条消息成功:SendObject [id=3, content=content3]
发送一条消息成功:SendObject [id=4, content=content4]
消费一条消息成功:SendObject [id=3, content=content3]
消费一条消息成功:SendObject [id=4, content=content4]
发送一条消息成功:SendObject [id=5, content=content5]
消费一条消息成功:SendObject [id=5, content=content5]
消费一条消息成功:null
发送一条消息成功:SendObject [id=6, content=content6]
发送一条消息成功:SendObject [id=7, content=content7]
消费一条消息成功:SendObject [id=6, content=content6]
发送一条消息成功:SendObject [id=8, content=content8]
消费一条消息成功:SendObject [id=7, content=content7]
消费一条消息成功:SendObject [id=8, content=content8]
发送一条消息成功:SendObject [id=9, content=content9]
消费一条消息成功:SendObject [id=9, content=content9]
消费一条消息成功:null
发送一条消息成功:SendObject [id=10, content=content10]
消费一条消息成功:SendObject [id=10, content=content10]

代码来源:
https://blog.csdn.net/evankaka/article/details/70806752

猜你喜欢

转载自blog.csdn.net/xundh/article/details/80192723
今日推荐