/// <summary> /// 初始化 /// </summary> public async Task Init() { try { //清理无用节点 await this.RetryAsync().ExecuteAsync(async() => await this.ClearDeadNodes()); } catch (Exception e) { var err = new Exception("清理无用节点失败", e); err.AddErrorLog(); } try { //读取节点并添加监视 await this.RetryAsync().ExecuteAsync(async() => await this.WalkNodeAndWatch(this._base_path)); } catch (Exception e) { var err = new Exception("订阅服务节点失败", e); err.AddErrorLog(); } //订阅完成 if (this.OnSubscribeFinishedAsync != null) { await this.OnSubscribeFinishedAsync.Invoke(); } //订阅完成 this._client_ready.Set(); }
public async Task Reg() { try { await this.RetryAsync(async() => await this.RegisterService()); } catch (Exception e) { var err = new Exception("注册服务失败", e); err.AddErrorLog(); } }
protected async Task InitBasePath() { try { var client = this.GetClientManager(); await this.RetryAsync().ExecuteAsync(async() => await client.EnsurePath(this._base_path)); } catch (Exception e) { var err = new Exception("尝试创建服务注册base path失败", e); err.AddErrorLog(); } }
public ZooKeeperClient(string host, TimeSpan?timeout = null) { this._host = host ?? throw new ArgumentNullException(nameof(host)); this._timeout = timeout ?? TimeSpan.FromSeconds(30); //监控zk的状态 this._connection_status_watcher = new ConnectionStatusWatcher(async status => { this._client_lock.Reset(); switch (status) { case Watcher.Event.KeeperState.SyncConnected: case Watcher.Event.KeeperState.ConnectedReadOnly: //服务可用 this._client_lock.Set(); if (this.OnConnectedAsync != null) { await this.OnConnectedAsync.Invoke(); } break; case Watcher.Event.KeeperState.Disconnected: //链接丢失,等待再次连接 if (this.OnUnConnectedAsync != null) { await this.OnUnConnectedAsync.Invoke(); } break; case Watcher.Event.KeeperState.Expired: //session过期,重新创建客户端 if (this.OnSessionExpiredAsync != null) { await this.OnSessionExpiredAsync.Invoke(); } break; case Watcher.Event.KeeperState.AuthFailed: //验证错误没必要再试尝试 this.Dispose(); var e = new Exception("zk auth验证失败,已经销毁所有链接"); e.AddErrorLog(); throw e; } }); //这里注释掉,让用户手动调用 //this.CreateClient(); }