Zookeeper在.netCore中的简单操作

开发包

在nuget中需要引用包:ZooKeeperNetEx

监控器

先定义俩监控器,后面如果需要使用,可以直接用:

    /// <summary>
    /// 监控器1
    /// </summary>
    public class ConfigServiceWatcher : Watcher
    {
        public override async Task process(WatchedEvent @event)
        {
            //Console.WriteLine("Type:" + @event.GetType() + ",     EventType:" + @event.get_Type() + ",     state:" + @event.getState());
            ////获取当前监控事件类型
            ////Console.WriteLine(@event.get_Type().ToString());
        }
    }


    /// <summary>
    /// 监控器2
    /// </summary>
    public class NodeWatcher : Watcher
    {
        public override async Task process(WatchedEvent @event)
        {
            Console.WriteLine("Type:" + @event.GetType() + ",     EventType:" + @event.get_Type() + ",     state:" + @event.getState());
            //获取当前监控事件类型
            //Console.WriteLine(@event.get_Type().ToString());
            if (@event.get_Type() == EventType.NodeDataChanged)
            {
                //各类业务实现
            }
        }
    }

其他操作

1,链接

      a、创建链接使用监控器

//单节点,开始连接zk,使用监控器
//var zk = new ZooKeeper("127.0.0.1:2181", 990000, new ConfigServiceWatcher());
//多接点,客户端会自动处理谁连上,谁宕机了
var zk = new ZooKeeper("127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002", 990000, new ConfigServiceWatcher());

      b、创建链接,不用监控器

//开始连接zk,不用监控器,多个地址第一个参数形如“127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002”
var zk = new ZooKeeper("127.0.0.1:2181", 990000, null);

      c、根据链接获取session会话信息

//获取sessionid
var sessionId = zk.getSessionId();
//获取session密码
var sessionPwd = zk.getSessionPasswd();

      d,根据session会话信息,重新恢复链接

//获取sessionid
var sessionId = zk.getSessionId();
//获取session密码
var sessionPwd = zk.getSessionPasswd();
//关闭zk连接
await zk.closeAsync();
//根据sessionId和密码,重新连接会话.
zk = new ZooKeeper("127.0.0.1:2181", 990000, _watcher, sessionId, sessionPwd);

      e,断开连接

//断开连接
zk.closeAsync();

 

2、读取

        /// <summary>
        /// 读取操作示例
        /// </summary>
        /// <param name="zk"></param>
        /// <returns></returns>
        public static async Task<DataResult> Read(ZooKeeper zk)
        {
            var stat = await zk.existsAsync("/mypath", true);
            if (stat == null)
            {
                Console.WriteLine("无法读取,结点不存在!");
                return null;
            }
            var dataResult = await zk.getDataAsync("/mypath", true);

            Console.WriteLine(Encoding.UTF8.GetString(dataResult.Data));
            return dataResult;
        }

3、添加

        /// <summary>
        /// 添加操作示例
        /// </summary>
        /// <param name="zk"></param>
        /// <returns></returns>
        public static async Task<string> Add(ZooKeeper zk)
        {
            var isExist = await zk.existsAsync("/mypath", new NodeWatcher());
            if (isExist == null)
            {
                var data = Encoding.Default.GetBytes("小笨熊!");
                var rs = await zk.createAsync("/mypath", data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                return rs;
            }
            else
            {
                Console.WriteLine("结点已经存在!" + "当前状态不为空,跳过创建。");
                return null;
            }

        }

4、更新

        /// <summary>
        /// 更新操作示例
        /// </summary>
        /// <param name="zk"></param>
        /// <returns></returns>
        public static async Task<Stat> Update(ZooKeeper zk)
        {
            var isExist = await zk.existsAsync("/mypath",  new NodeWatcher());
            if (isExist != null)
            {
                var data = Encoding.Default.GetBytes("大笨象!");
                var rs = await zk.setDataAsync("/mypath", data);

                return rs;
            }
            else
            {
                Console.WriteLine("无法更新,结点不存在!");
                return null;
            }
        }

5、删除

        /// <summary>
        /// 删除操作示例
        /// </summary>
        /// <param name="zk"></param>
        /// <returns></returns>
        public static async Task Del(ZooKeeper zk)
        {
            var isExist = await zk.existsAsync("/mypath", new NodeWatcher());
            if (isExist == null)
            {
             Console.WriteLine("删除失败,数据不存在!");
            }
            else
            {
                await zk.deleteAsync("/mypath", -1);
                Console.WriteLine("删除成功!");
            }
        }

 示例代码:ZkTestSlou.rar (595.70 kb)

添加评论

Loading