private static async Task <(bool, long)> ElectLeader(string serverName) { Console.WriteLine("Electing a leader."); var leaseResponse = await EtcdClient.LeaseGrantAsync(new LeaseGrantRequest { TTL = Ttl }); var put = await TryPut(LeaderKey, serverName, leaseResponse.ID); return(put, leaseResponse.ID); }
public async Task ImHere() { var leaseGrantRequest = new LeaseGrantRequest { TTL = LeaseTtl }; var leaseGrantResponse = await _client.LeaseGrantAsync(leaseGrantRequest); _leaseId = leaseGrantResponse.ID; await _client.PutAsync(new PutRequest { Lease = leaseGrantResponse.ID, Key = _key, Value = _broker }); _timer = new Timer(LeaseKeepAlive, null, TimeSpan.Zero, TimeSpan.FromSeconds(5)); }
/// <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"); }
/// <summary> /// 注册服务 /// </summary> /// <param name="client">客户端</param> /// <param name="systemName">系统名称(默认:Agent)</param> /// <param name="registration">注册信息</param> /// <returns></returns> public static async Task <bool> RegisterAsync(this EtcdClient client, AgentServiceRegistration registration, string systemName = "Agent") { try { // /系统/Services/srvname/srvid ServiceEntry entry = new ServiceEntry() { Host = registration.Address, Port = registration.Port, Name = registration.Name, Id = registration.ID, Tags = registration.Tags, Version = registration.Version }; if (registration.Checks == null || registration.Checks.Length == 0) { registration.Checks = new AgentServiceCheck[1]; registration.Checks[0] = new AgentServiceCheck(); registration.Checks[0].Interval = new TimeSpan(0, 0, 5); } var val = JsonConvert.SerializeObject(entry); string key = "/" + systemName + "/Services/" + entry.Name + "/" + entry.Id; CancellationToken token = new CancellationToken(); if (!string.IsNullOrEmpty(Utiletcd.Sinlgeton.Password) || !string.IsNullOrEmpty(Utiletcd.Sinlgeton.Username)) { await client.AuthenticateAsync(new AuthenticateRequest() { Name = Utiletcd.Sinlgeton.Username, Password = Utiletcd.Sinlgeton.Password }); } //申请TTL的ID var lease = await client.LeaseGrantAsync(new LeaseGrantRequest() { ID = 0, TTL = (long)registration.Checks[0].Interval.TotalSeconds }); //加入节点 var rsp = await client.PutAsync(new PutRequest() { Key = key.ToGoogleString(), Value = val.ToGoogleString(), Lease = lease.ID }); //保持激活 await client.LeaseKeepAlive(new LeaseKeepAliveRequest() { ID = lease.ID }, Watch, token); //添加配置 await AddConfigAsync(client, systemName, registration); //服务加入更新列表 await Utiletcd.Sinlgeton.AddKeepAliveAsync(client, key, (long)registration.Checks[0].Interval.TotalSeconds, lease.ID); Init(client, key); return(true); }catch (Exception ex) { return(false); } }