1. 程式人生 > >Zookeeper.NET Client(二)【官方驅動 開發入門】

Zookeeper.NET Client(二)【官方驅動 開發入門】

首先專案結構很簡單,如圖:


接下來是Program.cs內容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZooKeeperClient;
using ZooKeeperNet;

namespace ZkTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string _address = "127.0.0.1:2181";
            IZooKeeperFactory _factory=ZooKeeperFactory.Instance;
            
            var zk = _factory.Connect(_address);
 
            if (zk.Exists("/ConnectionTest", true)==null)
            {
                // 建立一個目錄節點,不進行ACL許可權控制,節點為永久性的  
                zk.Create("/ConnectionTest", "testRootData".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

                // 建立一個子目錄節點
                //臨時節點:客戶端會話失效或連線關閉後,該節點會被自動刪除,且不能再臨時節點下面建立子節點,否則報如下錯:org.apache.zookeeper.KeeperException$NoChildrenForEphemeralsException;
                //CreateMode.Ephemeral
                // 臨時順序節點:基本特性與臨時節點一致,建立節點的過程中,zookeeper會在其名字後自動追加一個單調增長的數字字尾,作為新的節點名
                //CreateMode.EphemeralSequential
                //持久節點:節點建立後,會一直存在,不會因客戶端會話失效而刪除
                //CreateMode.Persistent
                //持久順序節點:基本特性與持久節點一致,建立節點的過程中,zookeeper會在其名字後自動追加一個單調增長的數字字尾,作為新的節點名
                //CreateMode.PersistentSequential
                zk.Create("/ConnectionTest/testChildPathOne", "testChildDataOne".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); 
            }
             
            var state = zk.State;
            Thread.Sleep(2000);
            if (state.Equals(ZooKeeper.States.CONNECTED))
            {
                 string conTestData= Encoding.Default.GetString(zk.GetData("/ConnectionTest", true, null));
                 Console.WriteLine("取出根目錄節點資料:"+conTestData);
                // 取出子目錄節點列表
                 zk.GetChildren("/ConnectionTest", true).ToList().ForEach(delegate(String NodeName)
                     {
                         Console.WriteLine("子目錄節點名稱:"+NodeName);
                     }
                     );

                 //取出子目錄節點資料,返回byte[] 
                 string childTestData = Encoding.Default.GetString(zk.GetData("/ConnectionTest/testChildPathOne", true, null));
                 Console.WriteLine("取出子目錄節點資料:"+childTestData);

                //修改節點/root/childone下的資料,第三個引數為版本,如果是-1,那會無視被修改的資料版本,直接改掉  
                 zk.SetData("/ConnectionTest/testChildPathOne", "CHILDGeekModify".GetBytes(), -1);
                 //查詢修改的資料
                 childTestData = Encoding.Default.GetString(zk.GetData("/ConnectionTest/testChildPathOne", true, null));
                 Console.WriteLine("取出子目錄節點修改後資料為:" + childTestData);
                //刪除/ConnectionTest/testChildPathOne這個節點,第二個引數為版本,-1的話直接刪除,無視版本 
                 //zk.Delete("/ConnectionTest/testChildPathOne", -1);  
             
            }
            zk.Dispose();
            Console.WriteLine("---------------------------------------------------");
          
            Console.ReadKey();
        }
    }
}

接下來看Watcher.cs程式碼:

namespace ZooKeeperClient
{
    internal class Watcher : IWatcher
    {
        public void Process(WatchedEvent @event)
        {
            if (@event.Type == EventType.NodeDataChanged)
            {
                Console.WriteLine(@event.Path + "   此路徑節點變化了!");
            }  
        }
    }
}

很重要的ZooKeeperFactory.cs和IZooKeeperFactory.cs:

using System;
using ZooKeeperNet;

namespace ZooKeeperClient
{
    public interface IZooKeeperFactory
    {
        ZooKeeper Connect(string address);
        
        ZooKeeper Connect(string address, TimeSpan timeoutSpan);
        
        ZooKeeper Connect(string address, TimeSpan timeoutSpan, IWatcher watcher);

        ZooKeeper Connect(string address, TimeSpan timeoutSpan, IWatcher watcher, long sessionId, byte[] password);
    }
}
using System;
using ZooKeeperNet;

namespace ZooKeeperClient
{
    public sealed class ZooKeeperFactory : IZooKeeperFactory
    {
        public static IZooKeeperFactory Instance = new ZooKeeperFactory();

        private ZooKeeperFactory() { }



        //建立一個Zookeeper例項,第一個引數為目標伺服器地址和埠,第二個引數為Session超時時間,第三個為節點變化時的回撥方法   
        public ZooKeeper Connect(string address)
        {
            return Connect(address, new TimeSpan(0, 0, 0, 30), new Watcher());
        }

        public ZooKeeper Connect(string address, TimeSpan timeoutSpan)
        {
            return Connect(address, timeoutSpan, new Watcher());
        }

        public ZooKeeper Connect(string address, TimeSpan timeoutSpan, IWatcher watcher)
        {
            return new ZooKeeper(address, timeoutSpan, watcher);
        }

        public ZooKeeper Connect(string address, TimeSpan timeoutSpan, IWatcher watcher, long sessionId, byte[] password)
        {
            return new ZooKeeper(address, timeoutSpan, watcher, sessionId, password);
        }
    }
}

最終結果如圖: