示例#1
0
        public async Task <ActionResult <IEnumerable <KeyValuePair <string, string> > > > GetRange([FromRoute, Required] string keyPrefix)
        {
            var values = await _etcdClient.GetRangeAsync(GetKey(keyPrefix));

            var list = values.Kvs.Select(x => new KeyValuePair <string, string>(x.Key.ToStringUtf8(), x.Value.ToStringUtf8()));

            return(Ok(list));
        }
示例#2
0
        private async Task <(bool[] partitionsCorrectlyPlaced, int[] brokerPartitionCount)> GetPartitionsToRepartition(
            string producerTablePrefixKey, BrokersObject brokersObject, int wantedPartitionCountPrBroker,
            int wantedPartitionCount)
        {
            var rangeResponseTopic = await _client.GetRangeAsync(producerTablePrefixKey);

            var partitionsCorrectlyPlaced = new bool[wantedPartitionCount];
            var brokerPartitionCount      = PopulatePartitionsToRepartition(rangeResponseTopic, producerTablePrefixKey, brokersObject, wantedPartitionCountPrBroker, ref partitionsCorrectlyPlaced);

            return(partitionsCorrectlyPlaced, brokerPartitionCount);
        }
示例#3
0
        /// <summary>
        /// ocelot每次请求都会调用
        /// </summary>
        /// <returns></returns>
        public async Task <List <Service> > Get()
        {
            //Ocelot/Services/srvname/srvid
            if (Services.Count == 0)
            {
                EtcdClient client = etcdClientFactory.Get(this.config);
                MonitorKeys();
                var queryResult = await client.GetRangeAsync($"/Ocelot/Services/{config.KeyOfServiceInEtcd}");

                foreach (var dic in queryResult.Kvs)
                {
                    var srvs = Util.FromGoogleString(dic.Key).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    if (srvs.Length == 4)
                    {
                        var serviceEntry = JsonConvert.DeserializeObject <ServiceEntry>(Util.FromGoogleString(dic.Value));
                        serviceEntry.Name = srvs[2];
                        serviceEntry.Id   = srvs[3];
                        if (this.IsValid(serviceEntry))
                        {
                            this.Services.Add(BuildService(serviceEntry));
                        }
                        else
                        {
                            this.logger.LogWarning($"Unable to use service Address: {serviceEntry.Host} and Port: {serviceEntry.Port} as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0");
                        }
                    }
                }
            }
            return(new List <Service>(Services));
        }
示例#4
0
        public async Task SetupTopicListWatch()
        {
            _client.WatchRange(Prefix, HandleTopicListWatch);
            var rangeResponse = await _client.GetRangeAsync(Prefix);

            HandleTopicListGet(rangeResponse);
        }
示例#5
0
        public async Task <List <Service> > Get()
        {
            // /Ocelot/Services/srvname/srvid
            var queryResult = await _etcdClient.GetRangeAsync($"/Ocelot/Services/{_config.KeyOfServiceInEtcd}");

            var services = new List <Service>();

            foreach (var dic in queryResult)
            {
                var srvs = dic.Key.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (srvs.Length == 4)
                {
                    var serviceEntry = JsonConvert.DeserializeObject <ServiceEntry>(dic.Value);
                    serviceEntry.Name = srvs[2];
                    serviceEntry.Id   = srvs[3];
                    if (IsValid(serviceEntry))
                    {
                        services.Add(BuildService(serviceEntry));
                    }
                    else
                    {
                        _logger.LogWarning($"Unable to use service Address: {serviceEntry.Host} and Port: {serviceEntry.Port} as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0");
                    }
                }
            }

            return(services.ToList());
        }
示例#6
0
        /// <summary>
        /// 输出在线状态
        /// </summary>
        /// <returns></returns>
        private static async Task OutputOnlineState()
        {
            try
            {
                if (m_client != null)
                {
                    var childs = await m_client.GetRangeAsync("Client/");

                    foreach (var child in childs.Kvs)
                    {
                        var key   = child.Key.ToStringUtf8().Replace("Client/", "");
                        var value = child.Value.ToStringUtf8();

                        string result = $"{key}状态是{value}";
                        Console.WriteLine(result);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
示例#7
0
        public async Task <List <Address> > GetClusterMembers()
        {
            var response = await _client.GetRangeAsync(SEEDS);

            if (response.Count == 0)
            {
                return(new List <Address>());
            }

            var value = response.Kvs.Select(kv => kv.Value).First();

            var str     = Encoding.UTF8.GetString(value.ToByteArray());
            var members = JsonConvert.DeserializeObject <List <Address> >(str);

            return(members);
        }
        /// <summary>
        /// 获取系统服务
        /// </summary>
        /// <param name="client"></param>
        /// <param name="sysName"></param>
        /// <returns></returns>
        public static async Task <List <ServiceEntry> > GetServicesAsync(this EtcdClient client, string sysName)
        {
            string key = "/" + sysName + "/Services/";
            var    rsp = await client.GetRangeAsync(key);

            List <ServiceEntry> lst = new List <ServiceEntry>();

            foreach (var s in rsp.Kvs)
            {
                string   serverKey = s.Key.FromGoogleString();
                string   serverV   = s.Value.FromGoogleString();
                string[] entityKey = serverKey.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (entityKey.Length == 4)
                {
                    var ser = JsonConvert.DeserializeObject <ServiceEntry>(serverV);
                    lst.Add(ser);
                }
            }
            return(lst);
        }