示例#1
0
 /// <summary>
 /// 初始化一个新的Leader选举管理器
 /// </summary>
 /// <param name="serviceName">服务名,参与Leader选举的多个程序应该使用相同的服务名</param>
 /// <param name="serviceId">服务Id,参与Leader选举的每个程序应该有唯一的服务Id</param>
 /// <param name="options"></param>
 public LeaderElectionManager(string serviceName, string serviceId, LeaderElectionOptions options)
 {
     _defaultOfflineConfirmNumber = options.LeaderOfflineConfirmNumber;
     _offlineConfirmNumber        = _defaultOfflineConfirmNumber;
     _currentServiceId            = serviceId;
     _election = new LeaderElectionFactory().Create(options);
     _election.Register(serviceName, serviceId, options);
 }
        /// <summary>
        /// 创建一个Leader选举实现类
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public ILeaderElection Create(LeaderElectionOptions options)
        {
            if (options == null || options.LeaderElectionType == EnumLeaderElectionType.Consul)
            {
                return(new ConsulLeaderElection());
            }

            if (options == null || options.LeaderElectionType == EnumLeaderElectionType.ZooKeeper)
            {
                return(new ZkLeaderElection());
            }

            throw new NotImplementedException("未实现指定的Leader选举类型");
        }
示例#3
0
        /// <summary>
        /// 注册到Leader选举
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="serviceId"></param>
        /// <param name="options"></param>
        public void Register(string serviceName, string serviceId, LeaderElectionOptions options)
        {
            _options = (ZkElectionOptions)options;

            var watcher = new ZkElectionClientWatcher(ProcessZkConnectEvent);

            _zkElectionClient = new ZkElectionClient(_options.ConnectionString, _options.SessionTimeout, watcher);

            _leaderOfflineConfirmInterval = _options.LeaderOfflineConfirmInterval;
            _electionRootPath             = string.Format("/leader-election/{0}", serviceName);
            _electionFlag             = string.Concat(new string[] { _electionRootPath, "/", "flag" });
            _electionLeader           = string.Concat(new string[] { _electionRootPath, "/", "leader" });
            _serviceId                = serviceId;
            _electionDataChangedEvent = new AutoResetEvent(false);
            CreateElectionRootPath(_electionRootPath);
        }
示例#4
0
        /// <summary>
        /// 注册到Leader选举
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="serviceId"></param>
        /// <param name="options"></param>
        public void Register(string serviceName, string serviceId, LeaderElectionOptions options)
        {
            _options = (ConsulElectionOptions)options;
            _leaderOfflineConfirmInterval = options.LeaderOfflineConfirmInterval;

            if (options.LeaderOfflineConfirmInterval.TotalSeconds > 0 &&
                options.LeaderOfflineConfirmInterval.TotalSeconds < 10)
            {
                options.LeaderOfflineConfirmInterval = TimeSpan.FromSeconds(10);
            }

            _consulElectionClient = new ConsulElectionClient(_options.ConsulClient);

            _electionKey    = string.Format("leader-election/{0}/leader", serviceName);
            _serviceId      = serviceId;
            _sessionCheckId = _consulElectionClient.RegisterService(_leaderElectionServicePrefix + serviceId, _leaderElectionServicePrefix + serviceName, 10);
        }