public async Task EnusreInit()
        {
            if (_zk != null)
            {
                return;
            }

            await _semaphoreSlim.WaitAsync();

            try
            {
                if (_zk == null)
                {
                    var client = new ZookeeperClient(_sessionTimeout);
                    _watcher = new LeaderWatcher();
                    _zk      = await client.CreateClient(_hostPort);

                    await EnsureExists(ROOT);      //确保root已存在
                    await EnsureLocalNodeExists(); //确保创建当前节点

                    _watcher.NodeDeletedEvent += () =>
                    {
                        return(ElectionLeader());
                    };
                }
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Esempio n. 2
0
        public void Initialize()
        {
            var address = Configurations.ZooKeeperAddress;

            _zk       = ZooKeeperFactory.Instance.Connect(address);
            _election = new LeaderElection(_zk, "/ElectionTest", (_watcher = new LeaderWatcher()), new byte[0]);
        }
        public LeaderWatcher GetLeaderWatcher(string keyForLeadership)
        {
            LeaderWatcher returnValue;

            lock (_leaderWatchers)
            {
                if (!_leaderWatchers.TryGetValue(keyForLeadership, out returnValue))
                {
                    returnValue = new LeaderWatcher(_serviceManager, keyForLeadership);
                    _leaderWatchers[keyForLeadership] = returnValue;
                }
            }
            return(returnValue);
        }