Ejemplo n.º 1
0
 public void AddServiceDiscovery(Type serviceDiscovery,
                                 IServiceDiscoveryClientConfiguration serviceDiscoveryClientConfiguration)
 {
     UraganoSettings.ServiceDiscoveryClientConfiguration = serviceDiscoveryClientConfiguration ?? throw new ArgumentNullException(nameof(serviceDiscoveryClientConfiguration));
     ServiceCollection.AddSingleton(typeof(IServiceDiscovery), serviceDiscovery);
     AddStartUpTask <ServiceDiscoveryStartup>();
 }
Ejemplo n.º 2
0
        public async Task <List <ServiceDiscoveryInfo> > QueryServiceAsync(IServiceDiscoveryClientConfiguration serviceDiscoveryClientConfiguration, string serviceName,
                                                                           ServiceStatus serviceStatus = ServiceStatus.Alive, CancellationToken cancellationToken = default)
        {
            if (!(serviceDiscoveryClientConfiguration is ConsulClientConfigure client))
            {
                throw new ArgumentNullException(nameof(serviceDiscoveryClientConfiguration));
            }
            if (string.IsNullOrWhiteSpace(serviceName))
            {
                throw new ArgumentNullException(nameof(serviceName));
            }
            using (var consul = new ConsulClient(conf =>
            {
                conf.Address = client.Address;
                conf.Datacenter = client.Datacenter;
                conf.Token = client.Token;
                conf.WaitTime = client.WaitTime;
            }))
            {
                try
                {
                    QueryResult <ServiceEntry[]> result;
                    switch (serviceStatus)
                    {
                    case ServiceStatus.Alive:
                        result = await consul.Health.Service(serviceName, "", true, cancellationToken);

                        break;

                    case ServiceStatus.All:
                        result = await consul.Health.Service(serviceName, "", false, cancellationToken);

                        break;

                    default:
                        result = await consul.Health.Service(serviceName, cancellationToken);

                        break;
                    }
                    if (result.StatusCode != HttpStatusCode.OK)
                    {
                        Logger.LogError("Get the service{1} error:{0}", result.StatusCode, serviceName);
                    }

                    return(result.Response.Select(p => new ServiceDiscoveryInfo
                    {
                        ServiceId = p.Service.ID,
                        Address = p.Service.Address,
                        Port = p.Service.Port,
                        Meta = p.Service.Meta,
                        Alive = p.Checks.All(s => s.Status.Equals(HealthStatus.Passing))
                    }).ToList());
                }
                catch (Exception ex)
                {
                    Logger.LogError("Get the service{2} error:{0}\n{1}", ex.Message, ex.StackTrace, serviceName);
                    throw;
                }
            }
        }
Ejemplo n.º 3
0
        public async Task <bool> DeregisterAsync(IServiceDiscoveryClientConfiguration serviceDiscoveryClientConfiguration, string serviceId, CancellationToken cancellationToken = default)
        {
            if (!(serviceDiscoveryClientConfiguration is ConsulClientConfigure client))
            {
                throw new ArgumentNullException(nameof(serviceDiscoveryClientConfiguration));
            }
            if (string.IsNullOrWhiteSpace(serviceId))
            {
                throw new ArgumentNullException(nameof(serviceId));
            }
            Logger.LogTrace("Start deregistration consul...");
            using (var consul = new ConsulClient(conf =>
            {
                conf.Address = client.Address;
                conf.Datacenter = client.Datacenter;
                conf.Token = client.Token;
                conf.WaitTime = client.WaitTime;
            }))
            {
                var result = await consul.Agent.ServiceDeregister(serviceId, cancellationToken);

                if (result.StatusCode != HttpStatusCode.OK)
                {
                    Logger.LogError("--------------->  Deregistration service failed:{0}", result.StatusCode);
                    throw new ConsulRequestException("Deregistration service failed.", result.StatusCode);
                }

                Logger.LogTrace("Deregistration consul has been completed.");
                return(result.StatusCode == HttpStatusCode.OK);
            }
        }
Ejemplo n.º 4
0
        public async Task <bool> RegisterAsync(IServiceDiscoveryClientConfiguration serviceDiscoveryClientConfiguration, IServiceRegisterConfiguration serviceRegisterConfiguration, int?weight = default, CancellationToken cancellationToken = default)
        {
            if (!(serviceDiscoveryClientConfiguration is ConsulClientConfigure client))
            {
                return(false);
            }
            if (!(serviceRegisterConfiguration is ConsulRegisterServiceConfiguration service))
            {
                throw new ArgumentNullException(nameof(UraganoSettings.ServiceRegisterConfiguration));
            }
            Logger.LogTrace("Start registration consul...");
            using (var consul = new ConsulClient(conf =>
            {
                conf.Address = client.Address;
                conf.Datacenter = client.Datacenter;
                conf.Token = client.Token;
                conf.WaitTime = client.WaitTime;
            }))
            {
                if (weight.HasValue)
                {
                    if (service.Meta == null)
                    {
                        service.Meta = new Dictionary <string, string>();
                    }
                    service.Meta.Add("X-Weight", weight.ToString());
                }

                //Register service to consul agent
                var result = await consul.Agent.ServiceRegister(new AgentServiceRegistration
                {
                    Address           = UraganoSettings.ServerSettings.IP.ToString(),
                    Port              = UraganoSettings.ServerSettings.Port,
                    ID                = service.Id,
                    Name              = string.IsNullOrWhiteSpace(service.Name) ? $"{UraganoSettings.ServerSettings.IP}:{UraganoSettings.ServerSettings.Port}" : service.Name,
                    EnableTagOverride = service.EnableTagOverride,
                    Meta              = service.Meta,
                    Tags              = service.Tags,
                    Check             = new AgentServiceCheck
                    {
                        TCP = $"{UraganoSettings.ServerSettings.IP}:{UraganoSettings.ServerSettings.Port}",
                        DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(20),
                        Timeout  = TimeSpan.FromSeconds(3),
                        Interval = service.HealthCheckInterval
                    }
                }, cancellationToken);

                if (result.StatusCode != HttpStatusCode.OK)
                {
                    Logger.LogError("--------------->  Registration service failed:{0}", result.StatusCode);
                    throw new ConsulRequestException("Registration service failed.", result.StatusCode);
                }
                Logger.LogTrace("---------------> Consul service registration completed");
                return(result.StatusCode != HttpStatusCode.OK);
            }
        }
Ejemplo n.º 5
0
 public void AddServiceDiscovery(Type serviceDiscovery,
                                 IServiceDiscoveryClientConfiguration serviceDiscoveryClientConfiguration)
 {
     if (serviceDiscoveryClientConfiguration == null)
     {
         throw new ArgumentNullException(nameof(serviceDiscoveryClientConfiguration));
     }
     ServiceCollection.AddSingleton(serviceDiscoveryClientConfiguration);
     ServiceCollection.AddSingleton(typeof(IServiceDiscovery), serviceDiscovery);
     AddHostedService <ServiceDiscoveryStartup>();
 }
Ejemplo n.º 6
0
        public void AddServiceDiscovery(Type serviceDiscovery,
                                        IServiceDiscoveryClientConfiguration serviceDiscoveryClientConfiguration,
                                        IServiceRegisterConfiguration serviceRegisterConfiguration)
        {
            UraganoSettings.ServiceDiscoveryClientConfiguration = serviceDiscoveryClientConfiguration ?? throw new ArgumentNullException(nameof(serviceDiscoveryClientConfiguration));
            UraganoSettings.ServiceRegisterConfiguration        = serviceRegisterConfiguration ?? throw new ArgumentNullException(nameof(serviceRegisterConfiguration));
            if (string.IsNullOrWhiteSpace(serviceRegisterConfiguration.Name))
            {
                throw new ArgumentNullException(nameof(serviceRegisterConfiguration.Name));
            }

            ServiceCollection.AddSingleton(typeof(IServiceDiscovery), serviceDiscovery);
            AddStartUpTask <ServiceDiscoveryStartup>();
        }
Ejemplo n.º 7
0
 /// <summary>
 /// For server
 /// </summary>
 /// <typeparam name="TServiceDiscovery"></typeparam>
 /// <param name="serviceDiscoveryClientConfiguration"></param>
 /// <param name="serviceRegisterConfiguration"></param>
 public void AddServiceDiscovery <TServiceDiscovery>(IServiceDiscoveryClientConfiguration serviceDiscoveryClientConfiguration,
                                                     IServiceRegisterConfiguration serviceRegisterConfiguration) where TServiceDiscovery : IServiceDiscovery
 {
     AddServiceDiscovery(typeof(TServiceDiscovery), serviceDiscoveryClientConfiguration, serviceRegisterConfiguration);
 }
Ejemplo n.º 8
0
        public ZooKeeperServiceDiscovery(ICodec codec, ILogger <ZooKeeperServiceDiscovery> logger, UraganoSettings uraganoSettings, IServiceDiscoveryClientConfiguration clientConfiguration, IServiceProvider service)
        {
            if (!(clientConfiguration is ZooKeeperClientConfigure client))
            {
                throw new ArgumentNullException(nameof(clientConfiguration));
            }
            ZooKeeperClientConfigure = client;

            var agent = service.GetService <IServiceRegisterConfiguration>();

            if (agent != null)
            {
                if (!(agent is ZooKeeperRegisterServiceConfiguration serviceAgent))
                {
                    throw new ArgumentNullException(nameof(ZooKeeperRegisterServiceConfiguration));
                }
                ZooKeeperRegisterServiceConfiguration = serviceAgent;
            }
            ZooKeeperWatcher           = new UraganoWatcher();
            ZooKeeperWatcher.OnChange += Watcher_OnChange;;
            ServerSettings             = uraganoSettings.ServerSettings;
            Codec  = codec;
            Logger = logger;
            CreateZooKeeperClient();
        }
Ejemplo n.º 9
0
 /// <summary>
 /// For client
 /// </summary>
 /// <typeparam name="TServiceDiscovery"></typeparam>
 /// <param name="serviceDiscoveryClientConfiguration"></param>
 public void AddServiceDiscovery <TServiceDiscovery>(IServiceDiscoveryClientConfiguration serviceDiscoveryClientConfiguration) where TServiceDiscovery : class, IServiceDiscovery
 {
     AddServiceDiscovery(typeof(TServiceDiscovery), serviceDiscoveryClientConfiguration);
 }
Ejemplo n.º 10
0
        public ConsulServiceDiscovery(UraganoSettings uraganoSettings, ILogger <ConsulServiceDiscovery> logger, IServiceDiscoveryClientConfiguration clientConfiguration, IServiceProvider service)
        {
            if (!(clientConfiguration is ConsulClientConfigure client))
            {
                throw new ArgumentNullException(nameof(clientConfiguration));
            }
            var agent = service.GetService <IServiceRegisterConfiguration>();

            if (agent != null)
            {
                if (!(agent is ConsulRegisterServiceConfiguration serviceAgent))
                {
                    throw new ArgumentNullException(nameof(ConsulRegisterServiceConfiguration));
                }
                ConsulRegisterServiceConfiguration = serviceAgent;
            }


            ConsulClientConfigure = client;
            ServerSettings        = uraganoSettings.ServerSettings;
            Logger = logger;
        }