6.nuget安装C#Driver驱动ZooKeeperNet

一: C# 的Drivers
 
  1. nuget上下载 zookeeper.Net
  
 
  IWatcher是什么?:
    client 连接到 server 后,会在server上面注册一个watcher[handler]
    如果server connection完成之后,反向通知client。(sendresponse)
    client会收到这个 handler
    client会利用这个handler找到这个注册的watcher,执行回调函数
     
 IWatcher接口的实现
    public class ZookeeperWatcher : IWatcher
    {
        public static CountdownEvent countdownEvent = new CountdownEvent(1);
        public void Process(WatchedEvent @event)
        {
            Console.WriteLine("path={0},state={1},type={2}", @event.Path, @event.State, @event.Type);
            countdownEvent.Signal(); 
        }
    }
    可以把IWatcher理解成一个 Observer 观察者,注册到 zookeeper中
 
    zookeeper初始化是一个异步的过程。
     【sendthread,eventthread】
       sendthread =》 zookeeper server
       eventthread =》 专门处理server返回的watcher通知
 
  2. Refletor 反编译DLL
 
  3. 【长连接 + watcher 模式】
    心跳: timeout/3
 
  4. 配置log4net.config
  注册:
  
  log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
           
  使用:
  private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
 
  5. 客户端和server交互(心跳检测)
    client < = > server 【返回sessionid】
    syncConnect 同步成功 【网络正常】
    disconnect 失去连接等待重连 【网络断线】
    expired 断网后重连, Server会返回session过期 (一直保持重连,知道网络正常)
 
    为什么重连后,server会把sessionid抹掉?
      【临时节点概念,当网络断线后,datamip会被清空】
      你的当前zkCli.sh节点是属于那个sessionid的
 
 
  6. zookeeper连接初始化指定根路径
    连接到自己需要的路径。
     
  ZooKeeperNet.ZooKeeper zookeeper = new ZooKeeperNet.ZooKeeper("192.168.1.120:2181/datamip",
                TimeSpan.FromSeconds(5),
                new ZookeeperWatcher());

            //通过wait函数,等待ZookeeperWatcher 回调函数执行成功
            ZookeeperWatcher.countdownEvent.Wait();
            var list = zookeeper.GetChildren("/", false);
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/dragon-L/p/8563382.html