private void Conn(ZookeeperOptions zookeeperOptions) { bool isReadOnly = default(Boolean); Wathcer = new WaitConnWatch(autoResetEvent[0], _logger); if (isReadOnly != zookeeperOptions.IsReadOnly) { isReadOnly = zookeeperOptions.IsReadOnly; } byte[] pwd = new byte[0]; //如果没有密码和sessionId if (string.IsNullOrEmpty(zookeeperOptions.SessionPwd) && _zookeeperOptions.SessionId == default(int)) { _zookeeper = new ZooKeeper(zookeeperOptions.Connectstring, zookeeperOptions.SessionTimeout, Wathcer, isReadOnly); } else if (!string.IsNullOrEmpty(zookeeperOptions.SessionPwd)) { pwd = System.Text.Encoding.Default.GetBytes(zookeeperOptions.SessionPwd); _zookeeper = new ZooKeeper(zookeeperOptions.Connectstring, zookeeperOptions.SessionTimeout, Wathcer, 0, pwd, isReadOnly); } else { _zookeeper = new ZooKeeper(zookeeperOptions.Connectstring , zookeeperOptions.SessionTimeout, Wathcer, zookeeperOptions.SessionId, pwd, isReadOnly); } if (_zookeeper.getState() == States.CONNECTING) { _logger.LogDebug("当前状态:CONNECTING。阻塞等待"); autoResetEvent[0].WaitOne(); } }
public ZookeeperService(IOptionsMonitor <ZookeeperOptions> zookeeperOptions , ILoggerFactory loggerFac) { LoggerFac = loggerFac; _logger = LoggerFac.CreateLogger <ZookeeperService>(); _zookeeperOptions = zookeeperOptions.CurrentValue; _logger.LogDebug("配置参数:{0}", JsonConvert.SerializeObject(_zookeeperOptions)); zookeeperOptions.OnChange((zookopt, s) => { _zookeeperOptions = zookopt; }); _logger.LogDebug("开始连接"); Conn(_zookeeperOptions); }