コード例 #1
0
ファイル: CommonMethods.cs プロジェクト: ywscr/Uragano
        internal static ConsulRegisterServiceConfiguration ReadRegisterServiceConfiguration(IConfigurationSection configurationSection)
        {
            var service = new ConsulRegisterServiceConfiguration();

            var idSection = configurationSection.GetSection("id");

            if (idSection.Exists())
            {
                service.Id = idSection.Value;
            }
            var nameSection = configurationSection.GetSection("name");

            if (nameSection.Exists())
            {
                service.Name = nameSection.Value;
            }
            var hcSection = configurationSection.GetSection("HealthCheckInterval");

            if (hcSection.Exists())
            {
                service.HealthCheckInterval =
                    TimeSpan.FromSeconds(configurationSection.GetValue <int>("HealthCheckInterval"));
            }

            service.EnableTagOverride = configurationSection.GetValue <bool>("EnableTagOverride");
            service.Meta = configurationSection.GetValue <Dictionary <string, string> >("meta");
            service.Tags = configurationSection.GetValue <string[]>("tags");
            return(service);
        }
コード例 #2
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;
        }
コード例 #3
0
 public static void AddConsul(this IUraganoBuilder builder, ConsulClientConfigure consulClientConfiguration, ConsulRegisterServiceConfiguration consulAgentServiceConfiguration)
 {
     if (string.IsNullOrWhiteSpace(consulAgentServiceConfiguration.Id))
     {
         consulAgentServiceConfiguration.Id = Guid.NewGuid().ToString("N");
     }
     builder.AddServiceDiscovery <ConsulServiceDiscovery>(consulClientConfiguration, consulAgentServiceConfiguration);
 }
コード例 #4
0
 public static void AddConsul(this IUraganoBuilder builder, ConsulClientConfigure consulClientConfiguration, ConsulRegisterServiceConfiguration consulAgentServiceConfiguration)
 {
     builder.AddHostedService <ServiceStatusManageService>();
     builder.AddServiceDiscovery <ConsulServiceDiscovery>(consulClientConfiguration, consulAgentServiceConfiguration);
 }
コード例 #5
0
        public async Task <bool> RegisterAsync(CancellationToken cancellationToken = default)
        {
            if (ConsulRegisterServiceConfiguration == null)
            {
                ConsulRegisterServiceConfiguration = new ConsulRegisterServiceConfiguration();
            }

            if (string.IsNullOrWhiteSpace(ConsulRegisterServiceConfiguration.Id))
            {
                ConsulRegisterServiceConfiguration.Id = ServerSettings.ToString();
            }

            if (string.IsNullOrWhiteSpace(ConsulRegisterServiceConfiguration.Name))
            {
                ConsulRegisterServiceConfiguration.Name = ServerSettings.ToString();
            }

            if (string.IsNullOrWhiteSpace(ConsulRegisterServiceConfiguration.Name))
            {
                throw new ArgumentNullException(nameof(ConsulRegisterServiceConfiguration.Name), "Service name value cannot be null.");
            }

            Logger.LogTrace("Start registering with consul[{0}]...", ConsulClientConfigure.Address);

            try
            {
                using (var consul = new ConsulClient(conf =>
                {
                    conf.Address = ConsulClientConfigure.Address;
                    conf.Datacenter = ConsulClientConfigure.Datacenter;
                    conf.Token = ConsulClientConfigure.Token;
                    conf.WaitTime = ConsulClientConfigure.WaitTime;
                }))
                {
                    ConsulRegisterServiceConfiguration.Meta = new Dictionary <string, string> {
                        { "X-Tls", (ServerSettings.X509Certificate2 != null).ToString() }
                    };
                    if (ServerSettings.Weight.HasValue)
                    {
                        ConsulRegisterServiceConfiguration.Meta.Add("X-Weight", ServerSettings.Weight.ToString());
                    }

                    //Register service to consul agent
                    var result = await consul.Agent.ServiceRegister(new AgentServiceRegistration
                    {
                        Address           = ServerSettings.Address,
                        Port              = ServerSettings.Port,
                        ID                = ConsulRegisterServiceConfiguration.Id,
                        Name              = ConsulRegisterServiceConfiguration.Name,
                        EnableTagOverride = ConsulRegisterServiceConfiguration.EnableTagOverride,
                        Meta              = ConsulRegisterServiceConfiguration.Meta,
                        Tags              = ConsulRegisterServiceConfiguration.Tags,
                        Check             = new AgentServiceCheck
                        {
                            TCP = ServerSettings.ToString(),
                            DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(20),
                            Timeout  = TimeSpan.FromSeconds(3),
                            Interval = ConsulRegisterServiceConfiguration.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);
                }
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Registration service failed:{0}", e.Message);
                return(false);
            }
        }