コード例 #1
0
        static void Main(string[] args)
        {
            //Zookeeper连接字符串,采用host:port格式,多个地址之间使用逗号(,)隔开
            string[] address = new string[] { "127.0.0.1:2181" };
            //会话超时时间,单位毫秒
            int sessionTimeOut = 10000;

            ZookeeperHelper zookeeperHelper = new ZookeeperHelper(address, "/");

            zookeeperHelper.SessionTimeout = sessionTimeOut;
            zookeeperHelper.Connect();//发起连接
            while (!zookeeperHelper.Connected)
            {
                Thread.Sleep(1000); //停一秒,等待连接完成
            }

            //创建znode节点
            {
                zookeeperHelper.SetData("/mynode", "hello world", true, false);
                Console.WriteLine("完成创建节点");
            }

            //节点是否存在
            {
                var exists = zookeeperHelper.Exists("/mynode");
                Console.WriteLine("节点是否存在:" + exists);
            }

            //添加监听器
            {
                zookeeperHelper.WatchAsync("/mynode", (e) =>
                {
                    Console.WriteLine($"recieve: Path-{e.Path}     State-{e.State}    Type-{e.Type}");
                }).Wait();
            }

            //获取节点数据
            {
                var value = zookeeperHelper.GetData("/mynode");
                Console.WriteLine("完成读取节点:" + value);
            }

            //设置节点数据
            {
                zookeeperHelper.SetData("/mynode", "hello world again");
                Console.WriteLine("设置节点数据");
            }

            //重新获取节点数据
            {
                var value = zookeeperHelper.GetData("/mynode");
                Console.WriteLine("重新获取节点数据:" + value);
            }

            //移除节点
            {
                zookeeperHelper.Delete("/mynode");
                Console.WriteLine("移除节点");
            }

            Console.WriteLine("完成");
            Console.ReadKey();
        }
コード例 #2
0
 public DefaultWatcher(ZookeeperHelper zookeeperHelper, EventWaitHandle ewh)
 {
     this.ewh             = ewh;
     this.zookeeperHelper = zookeeperHelper;
 }