Exemplo n.º 1
0
        /// <summary>
        /// 监视路径
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="nodeData">节点值</param>
        /// <param name="disconfNodeType">节点类型</param>
        public void WatchPath(string nodeName, string nodeData, DisconfNodeType disconfNodeType)
        {
            string      monitorPath = CreateMonitorPath(nodeName, nodeData, disconfNodeType);
            NodeWatcher nodeWatcher = new NodeWatcher(_zooKeeperClient, nodeName, nodeData, disconfNodeType, monitorPath);

            nodeWatcher.Monitor();
        }
        /// <summary>
        /// ZooKeeper事件处理
        /// </summary>
        /// <param name="watchedEvent">ZooKeeper事件</param>
        public void Process(WatchedEvent watchedEvent)
        {
            string host = _webApi.GetZooKeeperHost();

            if (watchedEvent.State == KeeperState.SyncConnected)
            {
                LogManager.GetLogger().Info(string.Format("DisconfClient.ConnectionWatcher.Connect,host:{0},KeeperState.SyncConnected.", host));
                NodeWatcher.ReRegisterAllWatcher();
            }
            else if (watchedEvent.State == KeeperState.NoSyncConnected)
            {
                LogManager.GetLogger().Error(string.Format("DisconfClient.ConnectionWatcher.Connect,host:{0},KeeperState.NoSyncConnected.", host));
            }
            else if (watchedEvent.State == KeeperState.Disconnected)
            {
                // 这时收到断开连接的消息,这里其实无能为力,因为这时已经和ZK断开连接了,只能等ZK再次开启了
                LogManager.GetLogger().Error(string.Format("DisconfClient.ConnectionWatcher.Connect,host:{0},KeeperState.Disconnected.", host));
            }
            else if (watchedEvent.State == KeeperState.Expired)
            {
                // 这时收到这个信息,表示,ZK已经重新连接上了,但是会话丢失了,这时需要重新建立会话。
                LogManager.GetLogger().Error(string.Format("DisconfClient.ConnectionWatcher.Connect,host:{0},KeeperState.Expired.", host));
                ReConnect();
            }
            else if (watchedEvent.State == KeeperState.Unknown)
            {
                LogManager.GetLogger().Error(string.Format("DisconfClient.ConnectionWatcher.Connect,host:{0},KeeperState.Unknown.", host));
            }
        }
        /// <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
            });
        }