public async Task <string> DiscoverService(DiscoverOptions options) { options.CompleteDiscoverOptions(); try { var res = await _client.Health.Service(options.SearchServiceKey, "", true); if (res == null || res.StatusCode != HttpStatusCode.OK) { throw new Exception(); } _discoveredServices[options.SearchServiceKey] = res.Response.Select(e => new DiscoveredService(e)).Where(e => e.Version != null).ToList(); } catch { _logger.LogError("Error retrieving healthy service instances from Consul."); } var allDiscoveries = _discoveredServices.ContainsKey(options.SearchServiceKey) ? _discoveredServices[options.SearchServiceKey] : new List <DiscoveredService>(); foreach (var discovery in allDiscoveries) { var watchNamespace = $"/environments/{options.Environment}/services/{options.ServiceName}/{discovery.Version}"; if (_gatewayURLs.Where(e => e.Id == watchNamespace).Any() == false) { _logger.LogInformation($"Creating a watch for {watchNamespace}"); var key = "gatewayUrl"; _gatewayURLs.Add(new GatewayURLWatch() { Id = watchNamespace, URL = _config.Get <string>(key) }); _config.Subscribe(key, (string val) => { foreach (var gatewayURL in _gatewayURLs) { if (gatewayURL.Id == watchNamespace) { gatewayURL.URL = val; } } }); } } var service = Common.GetRandomServiceInstance(allDiscoveries, _gatewayURLs, options); return(service ?? ""); }
public static void CompleteDiscoverOptions(this DiscoverOptions options) { if (options.ServiceName == null) { options.ServiceName = ""; } if (string.IsNullOrWhiteSpace(options.Environment)) { options.Environment = "dev"; } if (string.IsNullOrWhiteSpace(options.Version)) { options.Version = ">=0.0.0"; } if (string.IsNullOrWhiteSpace(options.AccessTypeValue)) { options.AccessType = AccessType.Gateway; } }
public async Task <int> Execute(DiscoverOptions options) { WelcomeHelper.PrintWelcomeMessage(); var baseUrl = new Uri(options.Url); var targetUrl = new Uri(baseUrl, "erg/discover"); var response = await HttpClient.GetAsync(targetUrl); if (!response.IsSuccessStatusCode) { Console.WriteLine($"[erg-discover] could not discover runnable endpoints @ {targetUrl}"); Console.WriteLine($"[erg-discover] {response.StatusCode}"); return(-1); } var content = await response.Content.ReadAsStringAsync(); var discoverableEndpoints = JsonConvert.DeserializeObject <List <DiscoverableEndpoint> >(content); Console.WriteLine($"[erg-discover] discovered following endpoints @ {targetUrl}"); Console.WriteLine(""); ConsoleTable.From(discoverableEndpoints).Write(Format.Minimal); Console.WriteLine(); try { ConfigWriter.WriteEndpointMetadata(discoverableEndpoints); Console.WriteLine($"[erg-discover] Successfully saved endpoint data"); } catch (Exception ex) { Console.WriteLine($"[erg-discover] Error while saving endpoint data: {ex.Message}"); return(1); } return(0); }
public static string GetRandomServiceInstance(List <DiscoveredService> discoveredServices, List <GatewayURLWatch> gateways, DiscoverOptions options) { Range desiredVersionRange; try { desiredVersionRange = new Range(options.Version); } catch { desiredVersionRange = null; } if (desiredVersionRange == null) { return(null); } var latestVersion = desiredVersionRange.MaxSatisfying(discoveredServices.Select(e => e.Version)); var validServices = discoveredServices.Where(e => e.Version == latestVersion).ToList(); if (validServices.Count == 0) { return(null); } var randomService = validServices[new Random().Next(validServices.Count)]; var watchNamespace = $"/environments/{options.Environment}/services/{options.ServiceName}/{randomService.Version.ToString()}"; var serviceGatewayURL = gateways.Where(e => e.Id == watchNamespace).Select(e => e.URL).FirstOrDefault(); if (!string.IsNullOrEmpty(serviceGatewayURL) && options.AccessType == AccessType.Gateway) { return(serviceGatewayURL); } else if (!string.IsNullOrEmpty(randomService.DirectURL)) { return(randomService.DirectURL); } else { return(null); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //services.AddKumuluzHealth(options => //{ // options.RegisterHealthCheck("http_health_check", new HttpHealthCheck("https://github.com/")); // options.RegisterHealthCheck("as", new DiskSpaceHealthCheck(500, SpaceUnit.Gigabyte)); //}); services.AddKumuluzConfig(options => { options.SetConfigFilePath(Path.GetFullPath("config.yaml")); options.SetExtensions(Extension.Consul, Extension.Etcd); }); services.AddKumuluzDiscovery(opt => { opt.SetConfigFilePath(Path.GetFullPath("config.yaml")); opt.SetExtension(DiscoveryExtension.Consul); }); //DiscoveryProvider.GetDiscovery().RegisterService(new RegisterOptions() { Singleton = true }); var discoverOptions1 = new DiscoverOptions { ServiceName = "test-service", Environment = "devf", AccessType = AccessType.Direct, Version = ">1.0.2" }; var res1 = DiscoveryProvider.GetDiscovery().DiscoverService(discoverOptions1); var discoverOptions2 = new DiscoverOptions { ServiceName = "test-service", Environment = "devf", AccessType = AccessType.Direct, Version = "<=1.0.0" }; var res2 = DiscoveryProvider.GetDiscovery().DiscoverService(discoverOptions2); var discoverOptions3 = new DiscoverOptions { ServiceName = "test-service", Environment = "devf", AccessType = AccessType.Direct, Version = "^2.0.2" }; var res3 = DiscoveryProvider.GetDiscovery().DiscoverService(discoverOptions3); var discoverOptions4 = new DiscoverOptions { ServiceName = "test-service", Environment = "devf", AccessType = AccessType.Direct, Version = ">1.0.2" }; var res4 = DiscoveryProvider.GetDiscovery().DiscoverService(discoverOptions4); }
public string DiscoverService(DiscoverOptions options) { return(_discoverySource.DiscoverService(options).Result); }