Exemple #1
0
        public async Task <List <Service> > Get()
        {
            var services = new List <Service>();

            var instances = await _client.GetAllInstances(_serviceName, _groupName, _clusters);

            if (instances != null && instances.Any())
            {
                foreach (var Sitem in instances)
                {
                    string sip   = Sitem.Ip;
                    int    sport = Sitem.Port;
                    if (Sitem.Metadata.ContainsKey("endpoint"))
                    {
                        string[] ipport = Sitem.Metadata["endpoint"].Split(':');
                        sip   = ipport[0];
                        sport = int.Parse(ipport[1]);
                    }
                    services.Add(new Service(Sitem.InstanceId, new ServiceHostAndPort(sip, sport), "", "", new List <string>()));
                }
                // services.AddRange(instances.Select(i => new Service(i.InstanceId, new ServiceHostAndPort(i.Ip, i.Port), "", "", new List<string>())));
            }

            return(await Task.FromResult(services));
        }
Exemple #2
0
        protected virtual async Task Naming_Should_Succeed()
        {
            var serviceName = $"auth-{Guid.NewGuid().ToString()}";
            var instances   = await _namingSvc.GetAllInstances(serviceName, false).ConfigureAwait(false);

            _output.WriteLine($"Naming_Should_Succeed, GetAllInstances, {serviceName}, {instances?.ToJsonString()}");
            Assert.Empty(instances);
        }
        protected virtual async Task DeregisterInstance_Should_Succeed()
        {
            var serviceName = $"dereg-{Guid.NewGuid().ToString()}";
            var ip          = "127.0.0.2";
            var port        = 9999;

            await AssertRegisterSingleInstance(serviceName, ip, port, nameof(DeregisterInstance_Should_Succeed)).ConfigureAwait(false);

            await _namingSvc.DeregisterInstance(serviceName, ip, port).ConfigureAwait(false);

            await Task.Delay(1000).ConfigureAwait(false);

            var instances = await _namingSvc.GetAllInstances(serviceName, false).ConfigureAwait(false);

            _output.WriteLine($"DeregisterInstance_Should_Succeed, GetAllInstances, {serviceName}, {instances?.ToJsonString()}");
            Assert.Empty(instances);
        }
Exemple #4
0
        static async Task GetAllInstances(INacosNamingService svc)
        {
            await Task.Delay(500);

            // 获取全部实例有很多重载,选适合自己的即可。最后一个参数表明要不要订阅这个服务
            // SelectInstances, SelectOneHealthyInstance 是另外的方法可以获取服务信息。
            var list = await svc.GetAllInstances("demo-svc1", Nacos.V2.Common.Constants.DEFAULT_GROUP, false);

            Console.WriteLine($"======================获取实例成功,{Newtonsoft.Json.JsonConvert.SerializeObject(list)}");
        }
Exemple #5
0
        public async Task <List <Service> > Get()
        {
            var services = new List <Service>();

            var instances = await _client.GetAllInstances(_serviceName);

            if (instances != null && instances.Any())
            {
                services.AddRange(instances.Select(i => new Service(i.InstanceId, new ServiceHostAndPort(i.Ip, i.Port), "", "", new List <string>())));
            }

            return(await Task.FromResult(services));
        }
        public async Task <List <Service> > Get()
        {
            var services = new List <Service>();

            var instances = await _service.GetAllInstances(_serviceName, _groupName);

            if (instances != null && instances.Any())
            {
                foreach (var instance in instances)
                {
                    services.Add(BuildService(instance));
                }
            }

            return(await Task.FromResult(services));
        }
        public async Task <MessageModel <string> > GetStatus()
        {
            var data      = new MessageModel <string>();
            var instances = await NacosNamingService.GetAllInstances(JsonConfigSettings.NacosServiceName);

            if (instances == null || instances.Count == 0)
            {
                data.status  = 406;
                data.msg     = "DOWN";
                data.success = false;
                return(data);
            }
            // 获取当前程序IP
            var  currentIp = IpHelper.GetCurrentIp(null);
            bool isUp      = false;

            instances.ForEach(item =>
            {
                if (item.Ip == currentIp)
                {
                    isUp = true;
                }
            });
            // var baseUrl = await NacosNamingService.GetServerStatus();
            if (isUp)
            {
                data.status  = 200;
                data.msg     = "UP";
                data.success = true;
                return(data);
            }
            else
            {
                data.status  = 406;
                data.msg     = "DOWN";
                data.success = false;
                return(data);
            }
        }