private async Task <int> GetWantedPartitionCount(string topic) { var rangeResponseTopicList = await _client.GetAsync(TopicList.Prefix + topic); if (rangeResponseTopicList.Kvs.Count == 0) { // Remove Topic from everywhere! } var wantedPartitionCountString = rangeResponseTopicList.Kvs.First().Value.ToStringUtf8(); int.TryParse(wantedPartitionCountString, out var wantedPartitionCount); return(wantedPartitionCount); }
public async void Watch <T>(string key, Action <T> callback, string oldValue = "", int retryDelay = 0) { if (callback == null) { return; } var cb = new Action <object>(o => callback((T)o)); key = key.Replace('.', '/'); RangeResponse response; try { await Task.Delay(TimeSpan.FromMilliseconds(retryDelay)); response = await client?.GetAsync(nametag + key); } catch { response = null; } if (response == null) { if (retryDelay == 0) { retryDelay = startRetryDelay; } Watch(key, callback, oldValue, Math.Min(2 * retryDelay, maxRetryDelay)); return; } if (response.Count > 0) { var value = response.Kvs[0].Value.ToStringUtf8(); if (value != oldValue) { cb(_converter.ConvertTo <T>(value)); } Watch(key, callback, value, startRetryDelay); return; } Watch(key, callback, oldValue, startRetryDelay); return; }
/// <summary> /// 系统配置注册 /// </summary> /// <param name="client">客户端</param> /// <param name="sysName">系统名称</param> /// <param name="registration">配置信息</param> /// <returns></returns> private static async Task AddConfigAsync(EtcdClient client, string sysName, AgentServiceRegistration registration) { string key = "/" + sysName + "/config"; string timeKey = "/" + sysName + "/Time/" + registration.Name + "/" + registration.ID; var leasersp = await client.LeaseGrantAsync(new LeaseGrantRequest() { ID = 0, TTL = 10 }); var lockrsp = await client.LockAsync(new V3Lockpb.LockRequest() { Lease = leasersp.ID, Name = (key + "1").ToGoogleString() }); var rsp = await client.GetAsync(key); EtcdConfig config = new EtcdConfig(); if (rsp.Kvs.Count == 0) { var lst = new List <AgentServiceRegistration> { registration }; config.Services = lst; } else { var val = JsonConvert.DeserializeObject <EtcdConfig>(rsp.Kvs[0].Value.FromGoogleString()); List <AgentServiceRegistration> lst = new List <AgentServiceRegistration>(val.Services); lst.Add(registration); config.Services = lst; } string cof = JsonConvert.SerializeObject(config); await client.PutAsync(key, cof); await client.PutAsync(timeKey, DateTime.Now.ToString("yyyy-MM-dd mm:HH:ss")); await client.UnlockAsync(key + "1"); }