// Inspiration for the leader election have been found here: https://www.sandtable.com/etcd3-leader-election-using-python/ private static async Task LeaderElectionExample(EtcdClient client, string me) { client.Watch(LeaderKey, SetNewElection); while (true) { NewElection = false; var(leader, lease) = await LeaderElection(client, me); if (leader) { Console.WriteLine("I'm the leader!!!"); var count = 0; while (count < 20) { count++; client.LeaseKeepAlive(new LeaseKeepAliveRequest { ID = lease.ID }, Print, CancellationToken.None); Thread.Sleep(500); } Console.WriteLine("I'm no longer the leader"); Thread.Sleep(10000); } else { Console.WriteLine("I'm a follower!!!"); while (!NewElection) { Thread.Sleep(500); } } } }
public void LeaseKeepAlive(long leaseid) { var request = new LeaseKeepAliveRequest() { ID = leaseid }; var req = request.ToProto(); var rsp = client.LeaseKeepAlive(req, new Action <Etcdserverpb.LeaseKeepAliveResponse>(p => { }), CancellationToken.None); }
private static async Task LeadershipGained(long lease) { Console.WriteLine("Press ESC to terminate"); while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)) { Console.WriteLine("Refreshing. Still a leader."); EtcdClient.LeaseKeepAlive(lease, CancellationToken.None); await DoWork(); } await Revoke(lease); }
/// <summary> /// 发送更新 /// </summary> /// <param name="client"></param> /// <param name="id"></param> private bool KeepAlive(EtcdClient client, long id) { try { CancellationToken token = new CancellationToken(); client.LeaseKeepAlive(new LeaseKeepAliveRequest() { ID = id }, Watch, token); return(true); } catch { return(false); } }
private void LeaseKeepAlive(object state) { _client.LeaseKeepAlive(new LeaseKeepAliveRequest { ID = _leaseId }, KeepAliveResponseHandler, CancellationToken.None); }
/// <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); } }