示例#1
0
        public async Task <string> ListAllMembersInCluster()
        {
            MemberListRequest request = new MemberListRequest();

            etcdClient.MemberList(request);

            // Sync
            //MemberListResponse res = etcdClient.MemberList(request);

            // Async
            MemberListResponse res = await etcdClient.MemberListAsync(request);

            JsonMemberList = JsonConvert.SerializeObject(res);



            // Do something with response
            //foreach (var member in res.Members)
            //{
            //    WriteLine($"member.ID: {member.ID} - member.Name: {member.Name} - member.PeerURLs :{member.PeerURLs} - member.ClientURLs: { member.ClientURLs}");
            //}

            return(JsonMemberList);
        }
示例#2
0
        /// <summary>
        /// 保持服务活动
        /// </summary>
        /// <param name="client">客户端</param>
        /// <param name="key">服务Key</param>
        /// <param name="ttl">服务更新时间</param>
        /// <param name="ID">申请ID</param>
        public async Task AddKeepAliveAsync(EtcdClient client, string key, long ttl, long ID)
        {
            //获取集群列表

            MemberListRequest request = new MemberListRequest();
            var rsp = await client.MemberListAsync(request);

            if (minTTL > ttl)
            {
                if (minTTL == DefaultTTL)
                {
                    //第一次修改
                    ExecuteTiks = (DefaultTTL - ttl) * STicks;//需要提前时间
                }
                else
                {
                    //需要提前的时间减去已经运行的时间
                    ExecuteTiks = (minTTL - ttl) * STicks - (LastKeep - DateTime.Now.Ticks);
                }
                FulshTicks = FulshTicks - ExecuteTiks; //针对上次运行任务还剩余的时间
                minTTL     = ttl;
                isUpdate   = false;                    //无法正常允许了
            }
            if (!dic.ContainsKey(rsp.Header.ClusterId))
            {
                Health health = new Health
                {
                    Clients = new List <EtcdClient>(),
                    KeyTTL  = new Dictionary <string, long>(),

                    LastKeep = new Dictionary <string, long>(),
                    LeaseID  = new Dictionary <string, long>()
                };
                foreach (var kv in rsp.Members)
                {
                    EtcdClientUrls urls = new EtcdClientUrls();
                    foreach (var c in kv.ClientURLs)
                    {
                        urls.Urls.Add(c);
                    }
                    health.Urls = urls;
                }
                //
                health.Clients.Add(client);
                health.KeyTTL[key]        = ttl;
                health.LeaseID[key]       = ID;
                health.LastKeep[key]      = DateTime.Now.Ticks;
                dic[rsp.Header.ClusterId] = health;
                Console.WriteLine("加入刷新");
            }
            else
            {
                Health health = dic[rsp.Header.ClusterId];
                lock (health)
                {
                    health.Clients.Add(client);
                    health.KeyTTL[key]   = ttl;
                    health.LastKeep[key] = DateTime.Now.Ticks;
                    health.LeaseID[key]  = ID;
                }
            }
        }