/// <summary> /// 设置某个路径的值 /// </summary> /// <param name="path">路径</param> /// <param name="value">值</param> public void SetData(string path, string value) { ActionRetryHelper.Retry(() => { Stat stat = ZooKeeper.Exists(path, false); byte[] data = value == null ? null : value.GetBytes(); if (stat == null) { ZooKeeper.Create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } else { ZooKeeper.SetData(path, data, stat.Version); } }, 3, new TimeSpan(10), () => { //exceptionAction }, (ex) => { //errorHandle LogManager.GetLogger().Error(string.Format("DisconfClient.ZooKeeperClient.SetData(path={0},value={1}),ex:{2}", path, value, ex)); }, () => { //retryExceptionAction LogManager.GetLogger().Error(string.Format("DisconfClient.ZooKeeperClient.SetData(path={0},value={1}),error", path, value)); }); }
/// <summary> /// 检查某个路径在Zookeeper中是否已存在 /// </summary> /// <param name="path">路径</param> /// <returns></returns> public bool Exists(string path) { return(ActionRetryHelper.Retry <bool>(() => { Stat stat = ZooKeeper.Exists(path, false); return stat != null; }, 3, new TimeSpan(10), () => { }, (ex) => { //errorHandle LogManager.GetLogger() .Error(string.Format("DisconfClient.ZooKeeperClient.Exists(path={0}),ex:{1}", path, ex)); })); }
/// <summary> /// 重新连接ZooKeeper /// </summary> public void ReConnect() { string host = _webApi.GetZooKeeperHost(); ActionRetryHelper.Retry(() => { LogManager.GetLogger().Info(string.Format("DisconfClient.ConnectionWatcher.Connect,host:{0},Start ReConnect.", host)); Close(); Connect(); NodeWatcher.ReRegisterAllWatcher(); }, 3, new TimeSpan(10), () => { //exceptionAction }, (ex) => { //errorHandle LogManager.GetLogger().Error(string.Format("DisconfClient.ConnectionWatcher.Connect,host:{0},ReConnect,{1}.", host, ex)); }, () => { //retryExceptionAction }); }
/// <summary> /// 创建一个路径节点 /// </summary> /// <param name="path">路径</param> /// <param name="value">值</param> /// <param name="createMode">创建模式</param> /// <returns></returns> public string CreateNode(string path, string value, CreateMode createMode) { return(ActionRetryHelper.Retry <string>(() => { Stat stat = ZooKeeper.Exists(path, false); byte[] data = value == null ? null : value.GetBytes(); if (stat == null) { return ZooKeeper.Create(path, data, Ids.OPEN_ACL_UNSAFE, createMode); } else { ZooKeeper.SetData(path, data, -1); } return path; }, 3, new TimeSpan(10), () => { }, (ex) => { //errorHandle LogManager.GetLogger() .Error(string.Format("DisconfClient.ZooKeeperClient.CreateNode(path={0},value={1},createMode={2}),ex:{3}", path, value, createMode, ex)); })); }