public bool GetNodeValue(string nodePath, ref string nodeValue, ref string message) { nodeValue = string.Empty; try { using (ZooKeeper zk = new ZooKeeper(AppSettingsHelper.ZKServer, TimeSpan.FromSeconds(AppSettingsHelper.ZKSessionTimeOut), null)) { ZooKeeper.WaitUntilConnected(zk); Stat stat = zk.Exists(nodePath, true); if (stat == null) { message = string.Format("该节点【{0}】已被别人删除。", nodePath); return(false); } byte[] _nodeValue = zk.GetData(nodePath, false, stat); if (_nodeValue != null && _nodeValue.Length > 0) { nodeValue = Encoding.UTF8.GetString(_nodeValue); } } } catch (Exception ex) { message = string.Format("JinRi.Fx.Manage:{0}GetNodeValue()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); log.Error(message); message = string.Format("GetNodeValue()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); return(false); } return(true); }
public bool AddNode(string nodePath, string nodeValue, ref string message) { try { using (ZooKeeper zk = new ZooKeeper(AppSettingsHelper.ZKServer, TimeSpan.FromSeconds(AppSettingsHelper.ZKSessionTimeOut), null)) { ZooKeeper.WaitUntilConnected(zk); if (zk.Exists(nodePath, false) == null) { zk.Create(nodePath, Encoding.UTF8.GetBytes(nodeValue), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); return(true); } else { zk.Exists(nodePath, true); message = string.Format("该节点【{0}】已被别人新增。", nodePath); return(false); } } } catch (Exception ex) { message = string.Format("JinRi.Fx.Manage:{0}AddNode()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); log.Error(message); message = string.Format("AddNode()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); return(false); } }
private void DoConnect() { ConnectSettingWindow win = new ConnectSettingWindow(); bool?result = win.ShowDialog(); if (result.HasValue && result.Value) { var vm = win.DataContext as ConnectSettingWindowVM; try { this.Watcher = new ZookeeperWatcher(this); _zk = new ZooKeeper(vm.ConnectionString, TimeSpan.FromMilliseconds(double.Parse(vm.Timeout)), this.Watcher); ZooKeeper.WaitUntilConnected(_zk); //added by Yang Li this.ConnectionString = vm.ConnectionString; this.GetZookeeperNodes(); } catch (Exception ex) { this.AddLog(LogType.Fatal, ex.Message); this.DoDisconnect(); } finally { this.RaiseToolBarCanExecuteChanged(); } } }
public bool DeleteNode(string nodePath, ref string message) { try { using (ZooKeeper zk = new ZooKeeper(AppSettingsHelper.ZKServer, TimeSpan.FromSeconds(AppSettingsHelper.ZKSessionTimeOut), null)) { ZooKeeper.WaitUntilConnected(zk); Stat stat = zk.Exists(nodePath, false); if (stat != null) { zk.Delete(nodePath, stat.Version); } } } catch (Exception ex) { message = string.Format("JinRi.Fx.Manage:{0}DeleteNode()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); log.Error(message); message = string.Format("DeleteNode()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); return(false); } return(true); }
public ConfigServiceClient(string serviceAddress, TimeSpan timeout) { try { this.ZK = new ZooKeeper(serviceAddress, timeout, new ConfigServiceWatcher(this)); ZooKeeper.WaitUntilConnected(this.ZK); } catch { this.Close(); } }
static void Main(string[] args) { using (ZooKeeper zk = new ZooKeeper(ConfigServiceHelper.ZKServer, TimeSpan.FromSeconds(ConfigServiceHelper.ZKSessionTimeOut), null)) { ZooKeeper.WaitUntilConnected(zk); string cookieNameKeyPath = string.Format("{0}/{1}", ConfigServiceHelper.ZKRootPathWithAppID, "CookieKeyName"); if (zk.Exists(cookieNameKeyPath, false) != null) { byte[] _nodeValue = zk.GetData(cookieNameKeyPath, false, null); Console.WriteLine(Encoding.UTF8.GetString(_nodeValue)); } } Console.ReadLine(); }
public bool UpdateNodeValue(string nodePath, string newNodeValue, ref string message) { try { byte[] newData = Encoding.UTF8.GetBytes(newNodeValue); using (ZooKeeper zk = new ZooKeeper(AppSettingsHelper.ZKServer, TimeSpan.FromSeconds(AppSettingsHelper.ZKSessionTimeOut), null)) { ZooKeeper.WaitUntilConnected(zk); Stat stat = zk.Exists(nodePath, false); if (stat == null) { message = string.Format("该节点【{0}】已被别人删除。", nodePath); return(false); } byte[] oldValue = zk.GetData(nodePath, false, stat); if (oldValue == null || oldValue.Length < 1) { if (string.IsNullOrWhiteSpace(newNodeValue)) { message = "和原来的值一样,所以无需更新。"; return(false); } } string oldValueStr = string.Empty; if (oldValue != null && oldValue.Length > 0) { oldValueStr = Encoding.UTF8.GetString(oldValue); } if (newNodeValue.Equals(oldValueStr)) { message = "和原来的值一样,所以无需更新。"; return(false); } Stat newStat = zk.SetData(nodePath, newData, stat.Version); return(true); } } catch (Exception ex) { message = string.Format("JinRi.Fx.Manage:{0}UpdateNodeValue()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); log.Error(message); message = string.Format("UpdateNodeValue()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); return(false); } }
public void Init() { try { zk = new ZooKeeper(ZKServerInfo.ZKServer, TimeSpan.FromSeconds(ZKServerInfo.ZKSessionTimeOut), new LogCenterWatcher(this)); ZooKeeper.WaitUntilConnected(zk); if (zk.Exists(ZKServerInfo.ZKLogCenterRootPath, false) != null) { zk.GetChildren(ZKServerInfo.ZKLogCenterRootPath, true); } } catch (Exception ex) { m_localLog.Error("连接Zookeeper服务失败", ex); } }
public bool GetNodeList(TreeZNode root, List <TreeZNode> nodeList, ref string message) { try { using (ZooKeeper zk = new ZooKeeper(AppSettingsHelper.ZKServer, TimeSpan.FromSeconds(AppSettingsHelper.ZKSessionTimeOut), null)) { ZooKeeper.WaitUntilConnected(zk); GetNodeListLoop(zk, root, nodeList); } } catch (Exception ex) { message = string.Format("JinRi.Fx.Manage:{0}GetNodeList()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); log.Error(message); message = string.Format("GetNodeList()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); return(false); } return(true); }
/// <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 bool ConnectZookeeper(string connectionString, TimeSpan timeout) { try { if (_zk == null) { _zk = new ZooKeeper(connectionString, timeout, new ZookeeperWatcher(this)); ZooKeeper.WaitUntilConnected(_zk); } return(true); } catch (Exception ex) { this.AddLog(LogType.Fatal, ex.Message); if (_zk != null) { _zk.Dispose(); } _zk = null; } return(false); }