public static IConveyBuilder AddConsul(this IConveyBuilder builder, ConsulOptions options, HttpClientOptions httpClientOptions) { builder.Services.AddSingleton(options); if (!options.Enabled || !builder.TryRegister(RegistryName)) { return(builder); } if (httpClientOptions.Type?.ToLowerInvariant() == "consul") { builder.Services.AddTransient <ConsulServiceDiscoveryMessageHandler>(); builder.Services.AddHttpClient <IConsulHttpClient, ConsulHttpClient>("consul-http") .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>(); builder.RemoveHttpClient(); builder.Services.AddHttpClient <IHttpClient, ConsulHttpClient>("consul") .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>(); } builder.Services.AddTransient <IConsulServicesRegistry, ConsulServicesRegistry>(); var registration = builder.CreateConsulAgentRegistration(options); if (registration is null) { return(builder); } builder.Services.AddSingleton(registration); return(builder); }
public static IConveyBuilder AddConsul(this IConveyBuilder builder, ConsulOptions options, HttpClientOptions httpClientOptions) { builder.Services.AddSingleton(options); if (!options.Enabled || !builder.TryRegister(RegistryName)) { return(builder); } if (httpClientOptions.Type?.ToLowerInvariant() == "consul") { builder.Services.AddTransient <ConsulServiceDiscoveryMessageHandler>(); builder.Services.AddHttpClient <IConsulHttpClient, ConsulHttpClient>("consul-http") .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>(); builder.RemoveHttpClient(); builder.Services.AddHttpClient <IHttpClient, ConsulHttpClient>("consul") .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>(); } builder.Services.AddTransient <IConsulServicesRegistry, ConsulServicesRegistry>(); builder.Services.AddSingleton <IConsulClient>(c => new ConsulClient(cfg => { if (!string.IsNullOrEmpty(options.Url)) { cfg.Address = new Uri(options.Url); } })); var registration = builder.CreateConsulAgentRegistration(options); if (registration is null) { return(builder); } builder.Services.AddSingleton(registration); builder.AddBuildAction(sp => { var consulRegistration = sp.GetService <AgentServiceRegistration>(); var client = sp.GetService <IConsulClient>(); client.Agent.ServiceRegister(consulRegistration); }); return(builder); }
private static AgentServiceRegistration CreateConsulAgentRegistration(this IConveyBuilder builder, ConsulOptions options) { var enabled = options.Enabled; var consulEnabled = Environment.GetEnvironmentVariable("CONSUL_ENABLED")?.ToLowerInvariant(); if (!string.IsNullOrWhiteSpace(consulEnabled)) { enabled = consulEnabled == "true" || consulEnabled == "1"; } if (!enabled) { return(null); } if (string.IsNullOrWhiteSpace(options.Address)) { throw new ArgumentException("Consul address can not be empty.", nameof(options.PingEndpoint)); } var uniqueId = string.Empty; using (var serviceProvider = builder.Services.BuildServiceProvider()) { uniqueId = serviceProvider.GetRequiredService <IServiceId>().Id; } var pingInterval = options.PingInterval <= 0 ? 5 : options.PingInterval; var removeAfterInterval = options.RemoveAfterInterval <= 0 ? 10 : options.RemoveAfterInterval; var registration = new AgentServiceRegistration { Name = options.Service, ID = $"{options.Service}:{uniqueId}", Address = options.Address, Port = options.Port }; if (!options.PingEnabled) { return(registration); } var scheme = options.Address.StartsWith("http", StringComparison.InvariantCultureIgnoreCase) ? string.Empty : "http://"; var check = new AgentServiceCheck { Interval = TimeSpan.FromSeconds(pingInterval), DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(removeAfterInterval), HTTP = $"{scheme}{options.Address}{(options.Port > 0 ? $":{options.Port}" : string.Empty)}/" + $"{options.PingEndpoint}" }; registration.Checks = new[] { check }; return(registration); }
private static ServiceRegistration CreateConsulAgentRegistration(this IConveyBuilder builder, ConsulOptions options) { var enabled = options.Enabled; var consulEnabled = Environment.GetEnvironmentVariable("CONSUL_ENABLED")?.ToLowerInvariant(); if (!string.IsNullOrWhiteSpace(consulEnabled)) { enabled = consulEnabled == "true" || consulEnabled == "1"; } if (!enabled) { return(null); } if (string.IsNullOrWhiteSpace(options.Address)) { throw new ArgumentException("Consul address can not be empty.", nameof(options.PingEndpoint)); } builder.Services.AddHttpClient <IConsulService, ConsulService>(c => c.BaseAddress = new Uri(options.Url)); builder.Services.AddHostedService <ConsulHostedService>(); var serviceId = string.Empty; using (var serviceProvider = builder.Services.BuildServiceProvider()) { serviceId = serviceProvider.GetRequiredService <IServiceId>().Id; } var registration = new ServiceRegistration { Name = options.Service, Id = $"{options.Service}:{serviceId}", Address = options.Address, Port = options.Port, Tags = options.Tags, Meta = options.Meta, EnableTagOverride = options.EnableTagOverride, Connect = options.Connect?.Enabled == true ? new Connect() : null }; if (!options.PingEnabled) { return(registration); } var pingEndpoint = string.IsNullOrWhiteSpace(options.PingEndpoint) ? string.Empty : options.PingEndpoint.StartsWith("/") ? options.PingEndpoint : $"/{options.PingEndpoint}"; if (pingEndpoint.EndsWith("/")) { pingEndpoint = pingEndpoint.Substring(0, pingEndpoint.Length - 1); } var scheme = options.Address.StartsWith("http", StringComparison.InvariantCultureIgnoreCase) ? string.Empty : "http://"; var check = new ServiceCheck { Interval = ParseTime(options.PingInterval), DeregisterCriticalServiceAfter = ParseTime(options.RemoveAfterInterval), Http = $"{scheme}{options.Address}{(options.Port > 0 ? $":{options.Port}" : string.Empty)}" + $"{pingEndpoint}" }; registration.Checks = new[] { check }; return(registration); }
private static ServiceRegistration CreateConsulAgentRegistration(this IConveyBuilder builder, ConsulOptions options) { var enabled = options.Enabled; var consulEnabled = Environment.GetEnvironmentVariable("CONSUL_ENABLED")?.ToLowerInvariant(); if (!string.IsNullOrWhiteSpace(consulEnabled)) { enabled = consulEnabled == "true" || consulEnabled == "1"; } if (!enabled) { return(null); } builder.Services.AddHttpClient <IConsulService, ConsulService>(c => c.BaseAddress = new Uri(options.Url)); builder.Services.AddHostedService <ConsulHostedService>(); var serviceId = string.Empty; using (var serviceProvider = builder.Services.BuildServiceProvider()) { serviceId = serviceProvider.GetRequiredService <IServiceId>().Id; } var registration = new ServiceRegistration { Name = options.Service, Id = $"{options.Service}:{serviceId}", Address = options.PreferIpAddress ? options.IpAddress : options.HostName, Port = options.Port, Tags = options.Tags, Meta = options.Meta, EnableTagOverride = options.EnableTagOverride, Connect = options.Connect?.Enabled == true ? new Connect() : null }; if (!options.HealthCheck.Enabled) { return(registration); } var pingEndpoint = string.IsNullOrWhiteSpace(options.HealthCheck.HealthCheckPath) ? string.Empty : options.HealthCheck.HealthCheckPath.StartsWith("/") ? options.HealthCheck.HealthCheckPath : $"/{options.HealthCheck.HealthCheckPath}"; if (pingEndpoint.EndsWith("/")) { pingEndpoint = pingEndpoint.Substring(0, pingEndpoint.Length - 1); } var check = new ServiceCheck { Interval = $"{options.HealthCheck.HealthCheckInterval}s", DeregisterCriticalServiceAfter = $"{options.HealthCheck.HealthCheckCriticalTimeout}m", Timeout = $"{options.HealthCheck.HealthCheckTimeout}s", Http = $"{options.GetServiceAddress()}{pingEndpoint}", TLSSkipVerify = options.HealthCheck.HealthCheckTlsSkipVerify, Method = options.HealthCheck.HealthCheckMethod.ToUpper() }; registration.Checks = new[] { check }; return(registration); }