public EtcdFileConfigurationRepository( Cache.IOcelotCache <FileConfiguration> cache, IInternalConfigurationRepository repo, IEtcdClientFactory factory, IOcelotLoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger <EtcdFileConfigurationRepository>(); _cache = cache; var internalConfig = repo.Get(); _configurationKey = "InternalConfiguration"; string token = null; if (!internalConfig.IsError) { token = internalConfig.Data.ServiceProviderConfiguration.Token; _configurationKey = !string.IsNullOrEmpty(internalConfig.Data.ServiceProviderConfiguration.ConfigurationKey) ? internalConfig.Data.ServiceProviderConfiguration.ConfigurationKey : _configurationKey; } var config = new EtcdRegistryConfiguration( internalConfig.Data.ServiceProviderConfiguration.Host, internalConfig.Data.ServiceProviderConfiguration.Port, _configurationKey); _etcdClient = factory.Get(config); }
/// <summary> /// ocelot每次请求都会调用 /// </summary> /// <returns></returns> public async Task <List <Service> > Get() { //Ocelot/Services/srvname/srvid if (Services.Count == 0) { EtcdClient client = etcdClientFactory.Get(this.config); MonitorKeys(); var queryResult = await client.GetRangeAsync($"/Ocelot/Services/{config.KeyOfServiceInEtcd}"); foreach (var dic in queryResult.Kvs) { var srvs = Util.FromGoogleString(dic.Key).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (srvs.Length == 4) { var serviceEntry = JsonConvert.DeserializeObject <ServiceEntry>(Util.FromGoogleString(dic.Value)); serviceEntry.Name = srvs[2]; serviceEntry.Id = srvs[3]; if (this.IsValid(serviceEntry)) { this.Services.Add(BuildService(serviceEntry)); } else { this.logger.LogWarning($"Unable to use service Address: {serviceEntry.Host} and Port: {serviceEntry.Port} as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"); } } } } return(new List <Service>(Services)); }
public Etcd(EtcdRegistryConfiguration config, IOcelotLoggerFactory factory, IEtcdClientFactory clientFactory) { _logger = factory.CreateLogger <Etcd>(); _config = config; _etcdClient = clientFactory.Get(_config); }