static void Main(string[] args) { //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 using (ZooKeeper zk = new ZooKeeper("121.199.25.195:2181", new TimeSpan(0, 0, 0, 50000), new Watcher())) { var stat = zk.Exists("/root", true); //创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //取得/root节点下的子节点名称,返回List<String> zk.GetChildren("/root", true); //取得/root/childone节点下的数据,返回byte[] zk.GetData("/root/childone", true, null); //修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉 zk.SetData("/root/childone", "childonemodify".GetBytes(), -1); //删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 zk.Delete("/root/childone", -1); } }
/// <summary> /// <![CDATA[Zookeeper使用示例]]> /// </summary> /// <param name="args"></param> static void Main(string[] args) { //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 using (ZooKeeper zk = new ZooKeeper("10.10.10.19:2181", new TimeSpan(0, 0, 0, 50000), new Watcher())) { var stat = zk.Exists("/root", true); ////创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) //zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); ////在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 //zk.Create("/root/childone", "lichaoqiang.com".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //取得/root节点下的子节点名称,返回List<String> var listRootChildren = zk.GetChildren("/dubbo", true); listRootChildren = zk.GetChildren("/dubbo/com.ty.dao.mapper.ebook.EBookNoteMapper/providers", true); //取得/root/childone节点下的数据,返回byte[] //byte[] buffer = zk.GetData("/root/childone", true, null); //string strData = System.Text.Encoding.UTF8.GetString(buffer); //Console.WriteLine(strData); //修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉 //zk.SetData("/root/childone", "childonemodify".GetBytes(), -1); //删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 //zk.Delete("/root/childone", -1); ZooKeeperNet.ClientConnection clientConnection = new ClientConnection("10.10.10.19:2181", new TimeSpan(0, 0, 0, 50000), zk, null); clientConnection.Start(); ZooKeeperNet.ClientConnectionEventConsumer clientConnectionEventConsumer = new ClientConnectionEventConsumer(clientConnection); clientConnectionEventConsumer.Start(); } Console.ReadLine(); }
public void testSequentialNodeNames() { string path = "/SEQUENCE" + Guid.NewGuid(); string file = "TEST"; string filepath = path + "/" + file; using (ZooKeeper zk = CreateClient()) { zk.Create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); var children = zk.GetChildren(path, false); Assert.Equal(1, children.Count()); Assert.Equal(file + "0000000000", children.ElementAt(0)); zk.Create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential); children = zk.GetChildren(path, false); Assert.Equal(2, children.Count()); Assert.True(children.Contains(file + "0000000001"), "contains child 1"); zk.Create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential); children = zk.GetChildren(path, false); Assert.Equal(3, children.Count()); Assert.True(children.Contains(file + "0000000002"), "contains child 2"); // The pattern is holding so far. Let's run the counter a bit // to be sure it continues to spit out the correct answer for (int i = children.Count(); i < 105; i++) { zk.Create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential); } children = zk.GetChildren(path, false); Assert.True(children.Contains(file + "0000000104"), "contains child 104"); } }
/// <summary> ///更新服务列表,整个替换 /// </summary> private static void updateServiceList() { Dictionary <String, RoundRobbin> newDictionary = new Dictionary <string, RoundRobbin>(); try { List <string> rootChildren = (List <string>)_zk.GetChildren("/dubbo", false); foreach (var rootChild in rootChildren) { List <string> children = (List <string>)_zk.GetChildren("/dubbo/" + rootChild + "/providers", false); List <string> serviceHosts = new List <string>(); foreach (var child in children) { string childNode = System.Web.HttpUtility.UrlDecode(child); int index1 = childNode.IndexOf("rest://"); // 查找rest://的位置 if (childNode.Contains("rest://") == true) { int index = childNode.IndexOf("/", 7); // “rest://”有7个字符 string host = childNode.Substring(7, index - 7); serviceHosts.Add(host); } } newDictionary.Add(rootChild, new RoundRobbin(serviceHosts)); } } catch (Exception exp) { // TODO:日志 } _dictionary = newDictionary; }
public void Enter() { Console.WriteLine("[" + owner + "] Barrier.Enter Starting"); zk.GetChildren(synchPath, true); name = zk.Create(synchPath + "/b_" + owner, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); // Create a node for self while (true) { lock (mutex) { var children = zk.GetChildren(synchPath, true); Console.WriteLine("[" + owner + "] Barrier children: " + String.Join(",", children.ToArray()) + " " + children.Count()); if (children.Count() < size) { System.Threading.Monitor.Wait(mutex); } else { Console.WriteLine("[" + owner + "] Barrier.Enter Done"); return; } } } }
public List <String> GetChildren(string path, IWatcher watcher) { if (isDisposed) { return(null); } checkRep(); List <String> lst = null; int tries = retries; while ((tries--) > 0) { try { var children = zk.GetChildren(path, watcher); if (children != null) { lst = children.ToList(); } break; } catch (Exception ex) { if (tries == 0) { Console.WriteLine("GetChildren exception after #" + retries + " retries :\n" + ex.Message); Console.WriteLine("Last retry, throwing exception"); throw ex; } } } return(lst); }
/// <summary> /// Processes the specified watched event. /// </summary> /// <param name="watchedEvent">The watched event.</param> public void Process(WatchedEvent watchedEvent) { try { byte[] _nodeData = null; switch (watchedEvent.Type) { case EventType.None: break; case EventType.NodeCreated: ZooKeeper.GetChildren(watchedEvent.Path, this, null); _nodeData = ZooKeeper.GetData(watchedEvent.Path, this, null); if (OnNodeChangeEvent != null) { OnNodeChangeEvent(watchedEvent, _nodeData); } break; case EventType.NodeDeleted: ZooKeeper.Exists(watchedEvent.Path, this); if (OnNodeChangeEvent != null) { OnNodeChangeEvent(watchedEvent, null); } break; case EventType.NodeChildrenChanged: var _chlidrenNode = ZooKeeper.GetChildren(watchedEvent.Path, this, null); if (OnNodeChildrenChangeEvent != null) { OnNodeChildrenChangeEvent(watchedEvent, _chlidrenNode.ToArray()); } break; default: _nodeData = ZooKeeper.GetData(watchedEvent.Path, this, null); if (OnNodeChangeEvent != null) { OnNodeChangeEvent(watchedEvent, _nodeData); } break; } } catch (NoNodeException ex) { if (OnNodeExceptionEvent != null) { OnNodeExceptionEvent(ex, Path); } } }
public void Process(WatchedEvent @event) { if (@event.State == KeeperState.SyncConnected && _connectedSignal.CurrentCount != 0) { _connectedSignal.Signal(); Status = "Connection Established!"; } AppendLineShow(String.Format("Just hand an event: {0} ", @event.Type)); if (@event.Type == EventType.NodeChildrenChanged) { const string pdfPath = "/PDFConverter"; // List znodes + values try { // Place the watcher again Zk.GetChildren(pdfPath, true); var children = Zk.GetChildren(pdfPath, false); IEnumerable <string> enumerable = children as string[] ?? children.ToArray(); if (enumerable.IsEmpty()) { AppendLineShow("No members in znode: " + pdfPath); } else { AppendLineShow("\n======= Z ======="); foreach (var child in enumerable) { var path = pdfPath + "/" + child; var data = Zk.GetData(path, this, null /*stat*/); AppendLineShow(child + " - " + (data != null ? Encoding.UTF8.GetString(data) : "")); } AppendLineShow("\n"); } } catch (KeeperException.NoNodeException e) { AppendLineShow("Znode does not exist!"); } catch (KeeperException.SessionExpiredException e) { AppendLineShow("The session expired!"); } catch (InvalidOperationException e) { AppendLineShow("Too many '/'!"); } } }
public void Process(WatchedEvent @event) { try { if (@event.Type == EventType.None) { return; } if (@event.Type == EventType.NodeCreated) { _zk.GetChildren(@event.Path, this, null); var nodeData = _zk.GetData(@event.Path, this, null); if (_nodeChangecallback != null) { _nodeChangecallback(@event, nodeData); } } else if (@event.Type == EventType.NodeDeleted) { _zk.Exists(@event.Path, this); if (_nodeChangecallback != null) { _nodeChangecallback(@event, null); } } else if (@event.Type == EventType.NodeChildrenChanged) { var chlidrenNode = _zk.GetChildren(@event.Path, this, null); if (_nodeChildrenChangecallback != null) { _nodeChildrenChangecallback(@event, chlidrenNode.ToArray()); } } else { var nodeData = _zk.GetData(@event.Path, this, null); if (_nodeChangecallback != null) { _nodeChangecallback(@event, nodeData); } } } catch (KeeperException.NoNodeException e) { _zk.Exists(@event.Path, this); Console.WriteLine("ERROR"); } }
static void Main(string[] args) { // 创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 using (var zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 0, 50000), new Watcher())) { var stat = zk.Exists("/root", true); // 创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) // zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); // 在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); // 取得/root节点下的子节点名称,返回List<String> zk.GetChildren("/root", true); // 取得/root/childone节点下的数据,返回byte[] zk.GetData("/root/childone", true, null); // 修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉 zk.SetData("/root/childone", "childonemodify".GetBytes(), -1); // 删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 zk.Delete("/root/childone", -1); } Console.ReadKey(); }
/// <summary> /// Gets all children for a given path /// </summary> /// <param name="path"> /// The given path. /// </param> /// <param name="watch"> /// Indicates whether should reinstall watcher in ZooKeeper. /// </param> /// <returns> /// Children /// </returns> public IEnumerable <string> GetChildren(string path, bool watch) { Guard.NotNullNorEmpty(path, "path"); EnsuresNotDisposedAndNotNull(); return(_zkclient.GetChildren(path, watch)); }
public void testSequentialNodeData() { string queue_handle = "/queue" + Guid.NewGuid(); using (ZooKeeper zk = CreateClient()) { zk.Create(queue_handle, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create(queue_handle + "/element", "0".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); zk.Create(queue_handle + "/element", "1".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); var children = zk.GetChildren(queue_handle, true); Assert.Equal(children.Count(), 2); string child1 = children.ElementAt(0); string child2 = children.ElementAt(1); int compareResult = child1.CompareTo(child2); Assert.Equal(compareResult, 0); if (compareResult < 0) { } else { string temp = child1; child1 = child2; child2 = temp; } string child1data = Encoding.UTF8.GetString(zk.GetData(queue_handle + "/" + child1, false, null)); string child2data = Encoding.UTF8.GetString(zk.GetData(queue_handle + "/" + child2, false, null)); Assert.Equal(child1data, "0"); Assert.Equal(child2data, "1"); } }
private void DeleteChild(ZooKeeper zk, string path) { if (!string.IsNullOrEmpty(path) && !path.Contains("praweda") && !path.Contains("zookeeper")) { var lstChild = zk.GetChildren(path, false); foreach (var child in lstChild) { if (path != "/") { DeleteChild(zk, path + "/" + child); } else { DeleteChild(zk, "/" + child); } } if (path != "/") { try { zk.Delete(path, -1); } catch { } } } }
protected override void ProcessImpl(WatchedEvent watchedEvent) { var path = Path; var watcher = new ChildrenMonitorWatcher(_zooKeeper, path, _action); switch (watchedEvent.Type) { case EventType.NodeCreated: case EventType.NodeChildrenChanged: if (_zooKeeper.Exists(path, watcher) != null) { var childrens = _zooKeeper.GetChildren(path, watcher, new Stat()); _action(childrens); } else { _action(null); } break; case EventType.NodeDeleted: _zooKeeper.Exists(path, watcher); _action(null); break; } }
public List <string> GetChildren(string Path) { List <string> result = new List <string>(); IEnumerable <string> children; children = zk.GetChildren(Path, false); if (Path == "/") { foreach (string item in children) { if (!isZookeeperDefaultNode(item.Trim())) { result.Add(item); } ; } } else { foreach (string item in children) { result.Add(Path + "/" + item); } } return(result); }
public void GetLock() { var parent = OurPath.Substring(0, OurPath.LastIndexOf('/')); var nodes = ZK.GetChildren(parent, false).ToList(); nodes.Sort(); if (OurPath.Equals(parent + "/" + nodes[0])) { HasGetLock = true; monitor.Set(); } else { //注册watch if (ZK.Exists(OurPath, false) != null) { ZK.Register(new Locker(ZK, OurPath)); } var myNode = OurPath.Substring(OurPath.LastIndexOf('/') + 1); var lower = string.Empty; for (int i = 0; i < nodes.Count; i++) { if (nodes[i].Equals(myNode)) { lower = nodes[i - 1]; } } WaitForLock(parent + "/" + lower); HasGetLock = false; } }
private void GetZookeeperNodesLoop(ZookeeperTreeNodeModel node) { foreach (var child in _zk.GetChildren(node.QueryPath, true)) { var childnode = new ZookeeperTreeNodeModel(child, string.Concat(node.JoinPath, "/", child), node); node.Childs.Add(childnode); GetZookeeperNodesLoop(childnode); } }
public void test() { var stat = zk.Exists("/tasks", this); if (stat != null) { var data = zk.GetData("/tasks", true, stat); zk.GetChildren("/tasks", true); } }
/// <summary> /// 清空所有的服务路由。 /// </summary> /// <returns>一个任务。</returns> public Task ClearAsync() { return(Task.Run(() => { if (_logger.IsEnabled(LogLevel.Information)) { _logger.Information("准备清空所有路由配置。"); } var path = _configInfo.RoutePath; var childrens = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var index = 0; while (childrens.Any()) { var nodePath = "/" + string.Join("/", childrens); if (_zooKeeper.Exists(nodePath, false) != null) { foreach (var child in _zooKeeper.GetChildren(nodePath, false)) { var childPath = $"{nodePath}/{child}"; if (_logger.IsEnabled(LogLevel.Debug)) { _logger.Debug($"准备删除:{childPath}。"); } _zooKeeper.Delete(childPath, -1); } if (_logger.IsEnabled(LogLevel.Debug)) { _logger.Debug($"准备删除:{nodePath}。"); } _zooKeeper.Delete(nodePath, -1); } index++; childrens = childrens.Take(childrens.Length - index).ToArray(); } if (_logger.IsEnabled(LogLevel.Information)) { _logger.Information("路由配置清空完成。"); } })); }
public void testChrootedWatchReestablishment() { string chroot = "/" + Guid.NewGuid() + "watchtest"; using (ZooKeeper zk1 = CreateClient()) { Assert.AreEqual(chroot, zk1.Create(chroot, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent)); } ZooKeeper zk = CreateClient(chroot); MyWatcher watcher = new MyWatcher(); string name = "/watchtest"; zk.GetChildren("/", watcher); zk.Create(name, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); WatchedEvent @event; Assert.IsTrue(watcher.events.TryTake(out @event, TimeSpan.FromSeconds(3d))); Assert.AreEqual("/", @event.Path); Assert.AreEqual(EventType.NodeChildrenChanged, @event.Type); Assert.AreEqual(KeeperState.SyncConnected, @event.State); zk.GetChildren("/", watcher); ClientConnection cnxn = (ClientConnection)zk.GetType().GetField("cnxn", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(zk); ClientConnectionRequestProducer producer = (ClientConnectionRequestProducer)cnxn.GetType().GetField("producer", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cnxn); TcpClient client = (TcpClient)producer.GetType().GetField("client", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(producer); client.Close(); Thread.Sleep(100); zk.Delete(name, -1); Assert.IsTrue(watcher.events.TryTake(out @event, TimeSpan.FromSeconds(3d))); Assert.AreEqual("/", @event.Path); Assert.AreEqual(EventType.NodeChildrenChanged, @event.Type); Assert.AreEqual(KeeperState.SyncConnected, @event.State); Assert.AreEqual(0, watcher.events.Count); }
/** * Join barrier * * @return * @throws KeeperException * @throws InterruptedException */ public bool Enter() { _zk.Create(_root + "/" + _nodeName, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential); while (true) { lock (_mutex) { var children = _zk.GetChildren(_root, true); if (children.Count() < _size) { Monitor.Wait(_mutex); } else { return(true); } } } }
public void Process(WatchedEvent @event) { // 全部重新 watch zk.GetData(@event.Path, this, null); zk.Exists(@event.Path, this); zk.GetChildren(@event.Path, this); Console.WriteLine("33receive watch notify:"); Console.WriteLine(" path is : {0}", @event.Path); Console.WriteLine(" state is : {0}", @event.State); Console.WriteLine(" type is : {0}", @event.Type); }
/// <summary> /// 初始化 /// </summary> public void Init() { zk = new ZooKeeper(MasterElectionHelper.ZKServer, TimeSpan.FromSeconds(MasterElectionHelper.ZKSessionTimeOut), new MasterElectionWatcher(this)); ZooKeeper.WaitUntilConnected(zk); if (zk.Exists(MasterElectionHelper.ZKRootPath, false) == null) { zk.Create(MasterElectionHelper.ZKRootPath, string.Empty.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.GetChildren(MasterElectionHelper.ZKRootPath, true); } string ip = MasterElectionHelper.GetHostIP().ToString(); string nodePrefixName = string.Format("{0}_", ip.Replace(".", "-")); string nodePath = string.Format("{0}/{1}", MasterElectionHelper.ZKRootPath, nodePrefixName); if (string.IsNullOrWhiteSpace(this.clientNodePath) || zk.Exists(this.clientNodePath, false) == null) { this.clientNodePath = zk.Create(nodePath, string.Empty.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential); } zk.GetChildren(MasterElectionHelper.ZKRootPath, true); }
public void LoadLiveNodes() { CrawlProxy.Instance.Clear(); var nodes = zookeeper.GetChildren("/live_nodes", false); foreach (var node in nodes) { var ips = GetNodeIp(node); CrawlProxy.Instance.AddCrawlerServer(node, ips.ToList()); } }
//updates remote region servers IP table. private void UpdateRegionServers(WatchedEvent @event) { //obtains a list of remote region server address. just likes: "gs-server-1003,60020,1433640602093" IEnumerable <string> children = _zkClient.GetChildren("/hbase/rs", new ZooKeeperWatcher(UpdateRegionServers)); List <IPEndPoint> regionServers = new List <IPEndPoint>(); foreach (string rs in children) { string[] args = rs.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); regionServers.Add(new IPEndPoint(IPAddress.Parse(_hostMappingManager.GetIPAddressByHostName(args[0])), _tPort)); } Interlocked.Exchange(ref _regionServers, regionServers); }
public void testChild() { string name = "/" + Guid.NewGuid() + "foo"; zk.Create(name, name.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); string childname = name + "/bar"; zk.Create(childname, childname.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); Stat stat = new Stat(); List <string> s = zk.GetChildren(name, false, stat); Assert.AreEqual(stat.Czxid, stat.Mzxid); Assert.AreEqual(stat.Czxid + 1, stat.Pzxid); Assert.AreEqual(stat.Ctime, stat.Mtime); Assert.AreEqual(1, stat.Cversion); Assert.AreEqual(0, stat.Version); Assert.AreEqual(0, stat.Aversion); Assert.AreEqual(0, stat.EphemeralOwner); Assert.AreEqual(name.Length, stat.DataLength); Assert.AreEqual(1, stat.NumChildren); Assert.AreEqual(s.Count, stat.NumChildren); s = zk.GetChildren(childname, false, stat); Assert.AreEqual(stat.Czxid, stat.Mzxid); Assert.AreEqual(stat.Czxid, stat.Pzxid); Assert.AreEqual(stat.Ctime, stat.Mtime); Assert.AreEqual(0, stat.Cversion); Assert.AreEqual(0, stat.Version); Assert.AreEqual(0, stat.Aversion); Assert.AreEqual(zk.SessionId, stat.EphemeralOwner); Assert.AreEqual(childname.Length, stat.DataLength); Assert.AreEqual(0, stat.NumChildren); Assert.AreEqual(s.Count, stat.NumChildren); }
public IServerInfo GetAliveLogServer(string logToServer) { IServerInfo logServer = ServerManager.Get(logToServer); try { if (zk != null && zk.Exists(ZKServerInfo.ZKLogCenterRootPath, false) != null) { IEnumerable <string> aliveServers = zk.GetChildren(ZKServerInfo.ZKLogCenterRootPath, false); if (aliveServers != null && aliveServers.Count() >= 1) { IEnumerable <string> masterServer = aliveServers.Where(t => { byte[] data = zk.GetData(BuildFullPath(t), true, null); return(!"OFF".Equals(Encoding.UTF8.GetString(data).ToUpper())); }); List <string> tempList = masterServer.Count() > 0 ? masterServer.ToList() : aliveServers.ToList(); ShellSort(tempList); string serverIP = tempList.FirstOrDefault(); if (!string.IsNullOrEmpty(serverIP)) { string[] serverArr = serverIP.Replace("-", ".").Split('_'); short port = logServer.Port; short.TryParse(serverArr[1], out port); IServerInfo serverInfo = new ServerInfo("LogCenter_Thrift"); serverInfo.Address = serverArr[0]; serverInfo.Port = port; serverInfo.Timeout = logServer.Timeout; return(serverInfo); } } } } catch (Exception ex) { m_localLog.Error("从Zookeeper获取日志服务器IP失败", ex); } return(logServer); }
private IEnumerable <string> GetChildren(string groupNode) { while (true) { try { return(Execute(() => _zk.GetChildren("/" + groupNode, true))); } catch (Exception) { throw; } } }
/// <summary> /// 获根节点下的子节点 /// </summary> /// <returns></returns> public List <string> GetRootChildren() { List <string> list = new List <string>(); try { list = ZooKeeper.GetChildren("/", false).ToList(); } catch (Exception ex) { LogManager.GetLogger().Error(string.Format("DisconfClient.ZooKeeperClient.GetRootChildren,error:{0}", ex)); } return(list); }
/// <summary> /// 构造函数 /// </summary> /// <param name="zk">ZooKeeper</param> /// <param name="path">节点路径</param> public ZookeeperWatcher(ZooKeeper zk, string path) { Path = path; ZooKeeper = zk; if (zk.Exists(path, false) == null) { zk.Exists(path, this); } else { zk.GetData(path, this, null); zk.GetChildren(path, this, null); } }
internal static int Consume(this ZooKeeper zk, string path) { while (true) { //Get child nodes var nodes = zk.GetChildren(path, true).ToList(); if (nodes.IsEmpty()) { continue; } //Dequeue minimum node return(Dequeue(zk, path, nodes)); } }
private void Delete(ZooKeeper zk, List<string> children) { foreach (var child in children) { List<string> tmpChildren = zk.GetChildren(child, false).ToList(); if (tmpChildren.Count == 0) { zk.Delete(child, -1); } else { List<string> realPath = new List<string>(); foreach (var tmpChild in tmpChildren) { realPath.Add(child + "/" + tmpChild); } Delete(zk, realPath); } } }
static void Main(string[] args) { //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 //using (ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 0, 50000), new Watcher())) //{ // var stat = zk.Exists("/root", true); // zk.GetChildren("/root", true); // ////zk.GetChildren("/root", true); // //var stat = zk.Exists("/root", true); // //zk.Delete("/root", -1); // ////zk.Delete("/root", -1); // //////创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) // //zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); // ////在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 // //zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); // ////取得/root节点下的子节点名称,返回List<String> // //zk.GetChildren("/root", true); // ////取得/root/childone节点下的数据,返回byte[] // //zk.GetData("/root/childone", true, null); // ////修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉 // //zk.SetData("/root/childone", "childonemodify".GetBytes(), -1); // ////删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 // //zk.Delete("/root/childone", -1); // Console.ReadLine(); //} //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 using (ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 0, 50000), new Watcher1())) { //var stat = zk.Exists("/root", true); zk.GetChildren("/root", new Watcher2(zk)); //zk.GetChildren("/root", true); //zk.GetData("/root", new Watcher1(), null); //zk.GetData("/root/childone", true, null); //zk.GetChildren("/root", true); //var stat = zk.Exists("/root", true); //zk.Delete("/root", -1); ////zk.Delete("/root", -1); //////创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) //zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); ////在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 //zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); ////取得/root节点下的子节点名称,返回List<String> //zk.GetChildren("/root", true); ////取得/root/childone节点下的数据,返回byte[] //zk.GetData("/root/childone", true, null); ////修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉 //zk.SetData("/root/childone", "childonemodify".GetBytes(), -1); ////删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 //while (true) { // Console.Write(string.Join(",",zk.GetChildren("/root", true))); // Console.WriteLine(); // Thread.Sleep(10000); //} Console.ReadLine(); } }