Beispiel #1
0
        /// <summary>
        /// 获取path的数据
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="watcher">监听path节点的watcher</param>
        /// <returns>返回数据</returns>
        public static string GetNodeData(string path, Watcher watcher = null)
        {
            int tryNum = 0;

gotoLable:
            logger.LogInformation($"GetNodeData获取数据:path={path},watcher={watcher?.GetType()?.FullName}");
            try
            {
                var result = watcher == null
                    ? currentZookeeper.getDataAsync(path, true)
                    : currentZookeeper.getDataAsync(path, watcher);

                result.Wait();
                byte[] rst = result.Result.Data;
                if (rst == null || rst.Length < 1)
                {
                    return(null);
                }

                return(Encoding.UTF8.GetString(rst));
            }
            catch (KeeperException.NoNodeException nodeEx) // 这样的异常不用重连
            {
                logger.LogError(nodeEx, $"GetNodeData 获取数据节点不存在 NoNodeException:{path}");
                return(null);
            }
            catch (Exception e)
            {
                logger.LogError(e, $"GetNodeData获取数据异常:path={path},watcher={watcher?.GetType()?.FullName}");
                tryNum += 1;
                if (tryNum < 3)
                {
                    reConnection(e);
                    Thread.Sleep(500);
                    goto gotoLable;
                }
            }

            return(null);
        }
Beispiel #2
0
        public void Register()
        {
            var watches = GetWatches();

            HashSet <Watcher> watchers;

            watches.TryGetValue(clientPath, out watchers);
            if (watchers == null)
            {
                watchers            = new HashSet <Watcher>();
                watches[clientPath] = watchers;
            }
            if (!watchers.Any(p => p.GetType() == watcher.GetType()))
            {
                watchers.Add(watcher);
            }
        }