zoukankan      html  css  js  c++  java
  • 9.使用GetData,Children实现对ZNode的监控

    一、 zookeeper中的watcher
    1. exists :监控的是【znode】的CUD 的操作
     
    2. getdata 的时候注册的watcher【U,D 监控】update delete
    exists 可以获取到当前的节点,但是无法获取到内容。
    getdata 可以获取到最新的内容。
    异常: 如果znode不存,是无法监控getdata
    官方文档: A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
        
        同样的实现接口IWatcher
        
        public class DataWatcher: IWatcher
        {
            private ZooKeeper _zookeeper = null;
            Action<WatchedEvent, string> _action = null;
    
            public DataWatcher(ZooKeeper zookeeper, Action<WatchedEvent, string> action)
            {
                _zookeeper = zookeeper;
                _action = action;
            }
    
            public void Process(WatchedEvent @event)
            {
                try
                {
                    //Console.WriteLine("path={0},state={1},type={2}", @event.Path, @event.State, @event.Type);
                    //通过getdata获取内容
                    var content = Encoding.UTF8.GetString(_zookeeper.GetData(@event.Path, this, null));
                    if (_action != null)
                    {
                        _action(@event, content);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
      使用:
      
                Console.WriteLine("欢迎来到zookeeper.net 9 ");
                try
                {
                    var bytes = zookeeper.GetData("/google", new DataWatcher(zookeeper, (e, data) =>
                     {
                         //data 是返回的内容
                         Console.WriteLine("path={0},state={1},type={2},data={3}", e.Path, e.State, e.Type, data);
                     }), null); 
                }
                catch (KeeperException ex)
                {
                    throw new Exception(ex.Message);
                }
      3. getchildren 的时候注册的watcher
        子节点的add,remove 都是能够监控的到状态。【Update是监控不到的,需要使用exists】
     
       public class ChildrenWatcher : IWatcher
        {
            private ZooKeeper _zookeeper = null;
            Action<WatchedEvent, IEnumerable<string>> _action = null;
    
            public ChildrenWatcher(ZooKeeper zookeeper, Action<WatchedEvent, IEnumerable<string>> action)
            {
                _zookeeper = zookeeper;
                _action = action;
            }
    
            public void Process(WatchedEvent @event)
            {
                try
                {
                    //通过getdata获取内容
                    var list = _zookeeper.GetChildren(@event.Path, this, null);
                    if (_action != null)
                    {
                        _action(@event, list);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
    
        }
    try
                {
                  var list_t = zookeeper.GetChildren("/google", new ChildrenWatcher(zookeeper, (e, data) =>
                    {
                        Console.WriteLine("path={0},state={1},type={2},data={3}", e.Path, e.State, e.Type, string.Join(",", data));
                    }), null);
    
                }
                catch (KeeperException ex)
                {
                    throw new Exception(ex.Message);
                }

    执行创建,删除后的效果

      

    监听结果
      
     
    应用场景:
      【智能感知上下线】 服务注(provider) 和发现(client)
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    DJANGO
    前端--BootStrap
    前端--JQuery
    前端--JavaScript
    前端--CSS
    前端--HTML
    python中面试题积累
    python中一些小的知识点
    python中字典的增删改查及相关知识点
    python中列表的增删改查以及其它相关方法
  • 原文地址:https://www.cnblogs.com/dragon-L/p/8576629.html
Copyright © 2011-2022 走看看