Exemplo n.º 1
0
        /// <inheritdoc/>
        public async Task <object> GetStatusAsync(INacosRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }


            var request = new ListInstancesRequest()
            {
                ServiceName = registration.ServiceId,
                Clusters    = registration.ClusterName
            };
            var result = await _nacosClient.ListInstancesAsync(request);

            //ListInstancesResult result = Task.Run(async () =>
            //{
            //    return
            //}).Result;
            foreach (Host check in result.Hosts)
            {
                if (check.Ip.Equals(registration.Host))
                {
                    if (check.Healthy == false)
                    {
                        return(OUT_OF_SERVICE);
                    }
                }
            }

            return(UP);
        }
        public async Task <List <Host> > GetServerAsync(string serviceName)
        {
            var cached = await _provider.GetAsync(serviceName, async() =>
            {
                var serviceInstances = await _client.ListInstancesAsync(new ListInstancesRequest
                {
                    ServiceName = serviceName,
                    HealthyOnly = true,
                });

                var baseUrl = string.Empty;

                if (serviceInstances != null && serviceInstances.Hosts != null && serviceInstances.Hosts.Any())
                {
                    return(serviceInstances.Hosts.ToList());
                }
                return(null);
            }, TimeSpan.FromSeconds(10));

            if (cached.HasValue)
            {
                return(cached.Value);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        public async Task <string> GetServerAsync(string serviceName)
        {
            var cached = await _provider.GetAsync(serviceName, async() =>
            {
                var serviceInstances = await _client.ListInstancesAsync(new ListInstancesRequest
                {
                    ServiceName = serviceName,
                    HealthyOnly = true,
                });

                var baseUrl = string.Empty;

                if (serviceInstances != null && serviceInstances.Hosts != null && serviceInstances.Hosts.Any())
                {
                    var list = serviceInstances.Hosts.Select(x => new NacosServer
                    {
                        // it seems that nacos don't return the scheme
                        // so here use http only.
                        Url    = $"http://{x.Ip}:{x.Port}",
                        Weight = x.Weight
                    }).ToList();

                    return(list);
                }

                return(null);
            }, TimeSpan.FromSeconds(10));

            if (cached.HasValue)
            {
                var list     = cached.Value;
                var instance = _strategy.GetInstance(list);
                return(instance);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 4
0
        public IList <IServiceInstance> GetInstances(string serviceId)
        {
            var request = new ListInstancesRequest()
            {
                ServiceName = serviceId
            };

            ListInstancesResult result = Task.Run(async() =>
            {
                return(await _client.ListInstancesAsync(request));
            }).Result;
            IList <IServiceInstance> instances = new List <IServiceInstance>();

            if (result == null && result.Hosts != null)
            {
                foreach (var host in result.Hosts)
                {
                    instances.Add(new NacosServiceInstance(host));
                }
            }

            return(instances);
        }
Exemplo n.º 5
0
        private async Task <List <Host> > GetServerListInnerAsync(string serviceName, string groupName, string clusters, string namespaceId)
        {
            var cachedKey = $"{serviceName}-{groupName}-{clusters}-{namespaceId}";

            var cached = await _provider.GetAsync(cachedKey, async() =>
            {
                var serviceInstances = await _client.ListInstancesAsync(new ListInstancesRequest
                {
                    ServiceName = serviceName,
                    GroupName   = groupName,
                    Clusters    = clusters,
                    NamespaceId = namespaceId,
                    HealthyOnly = true,
                }).ConfigureAwait(false);

                if (serviceInstances?.Hosts == null || !serviceInstances.Hosts.Any())
                {
                    return(null);
                }
                return(serviceInstances.Hosts.ToList());
            }, TimeSpan.FromSeconds(8)).ConfigureAwait(false);

            return(cached.HasValue ? cached.Value : null);
        }
Exemplo n.º 6
0
        public async Task <string> GetServerAsync(string serviceName)
        {
            var cached = await _provider.GetAsync(serviceName, async() =>
            {
                var serviceInstances = await _client.ListInstancesAsync(new ListInstancesRequest
                {
                    ServiceName = serviceName,
                    HealthyOnly = true,
                });

                var baseUrl = string.Empty;

                if (serviceInstances != null && serviceInstances.Hosts != null && serviceInstances.Hosts.Any())
                {
                    var list = serviceInstances.Hosts.Select(x => new NacosServer
                    {
                        Url = $"http://{x.Ip}:{x.Port}"
                    }).ToList();

                    return(list);
                }

                return(null);
            }, TimeSpan.FromSeconds(10));

            if (cached.HasValue)
            {
                var list  = cached.Value;
                var index = new Random().Next(0, list.Count);
                return(list[index].Url);
            }
            else
            {
                return(null);
            }
        }