public ConsulFileConfigurationRepository( Cache.IOcelotCache <FileConfiguration> cache, IInternalConfigurationRepository repo, IConsulClientFactory factory, IOcelotLoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger <ConsulFileConfigurationRepository>(); _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 ConsulRegistryConfiguration(internalConfig.Data.ServiceProviderConfiguration.Host, internalConfig.Data.ServiceProviderConfiguration.Port, _configurationKey, token); _consul = factory.Get(config); }
public void should_use_token() { var token = "test token"; var config = new ConsulRegistryConfiguration(_consulScheme, _consulHost, _port, _serviceName, token); _provider = new Consul(config, _factory.Object, _clientFactory); var serviceEntryOne = new ServiceEntry() { Service = new AgentService() { Service = _serviceName, Address = "localhost", Port = 50881, ID = Guid.NewGuid().ToString(), Tags = new string[0], }, }; this.Given(_ => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName)) .And(_ => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne)) .When(_ => WhenIGetTheServices()) .Then(_ => ThenTheCountIs(1)) .And(_ => ThenTheTokenIs(token)) .BDDfy(); }
public ConsulFileConfigurationRepository( Cache.IOcelotCache <FileConfiguration> cache, IInternalConfigurationRepository repo, IConsulClientFactory factory, IOcelotLoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger <ConsulFileConfigurationRepository>(); _cache = cache; var internalConfig = repo.Get(); var consulHost = "localhost"; var consulPort = 8500; string token = null; if (!internalConfig.IsError) { consulHost = string.IsNullOrEmpty(internalConfig.Data.ServiceProviderConfiguration?.Host) ? consulHost : internalConfig.Data.ServiceProviderConfiguration?.Host; consulPort = internalConfig.Data.ServiceProviderConfiguration?.Port ?? consulPort; token = internalConfig.Data.ServiceProviderConfiguration?.Token; } var config = new ConsulRegistryConfiguration(consulHost, consulPort, OcelotConfiguration, token); _consul = factory.Get(config); }
static void Main(string[] args) { var consulHost = "localhost"; var consulPort = 8500; var token = ""; var config = new ConsulRegistryConfiguration(consulHost, consulPort, "Adou.ConfigCenter.ClientTest", token); ConfigContainer.Init(config) .Monitor <TestConfig>(); Task.Run(async() => { do { var testConfig = ConfigContainer.Current.Get <TestConfig>().Result; Console.WriteLine(JsonConvert.SerializeObject(testConfig)); Thread.Sleep(5000); } while (true); }); do { Console.ReadKey(); } while (true); }
public ConsulServiceDiscoveryProvider(ConsulRegistryConfiguration config, IOcelotLoggerFactory factory, IConsulClientFactory clientFactory) { ; _logger = factory.CreateLogger <ConsulServiceDiscoveryProvider>(); _config = config; _consul = clientFactory.Get(_config); }
public async Task <List <Ocelot.Values.Service> > GetEndpoints(string serviceName) { var config = new ConsulRegistryConfiguration(_serviceConfig.SERVICE_REGISTRY_ADDRESS, int.Parse(_serviceConfig.SERVICE_REGISTRY_PORT), serviceName); IServiceDiscoveryProvider discoveryProvider = new ConsulServiceDiscoveryProvider(config); var list = await discoveryProvider.Get(); return(list); }
public ConsulOcelotConfigurationRepository(Cache.IOcelotCache <IOcelotConfiguration> cache, ServiceProviderConfiguration serviceProviderConfig) { var consulHost = string.IsNullOrEmpty(serviceProviderConfig?.ServiceProviderHost) ? "localhost" : serviceProviderConfig?.ServiceProviderHost; var consulPort = serviceProviderConfig?.ServiceProviderPort ?? 8500; var configuration = new ConsulRegistryConfiguration(consulHost, consulPort, _ocelotConfiguration); _cache = cache; _consul = new ConsulClient(c => { c.Address = new Uri($"http://{configuration.HostName}:{configuration.Port}"); }); }
public ConsulFileConfigurationRepository( Cache.IOcelotCache <FileConfiguration> cache, ServiceProviderConfiguration serviceProviderConfig, IConsulClientFactory factory) { var consulHost = string.IsNullOrEmpty(serviceProviderConfig?.Host) ? "localhost" : serviceProviderConfig?.Host; var consulPort = serviceProviderConfig?.Port ?? 8500; var config = new ConsulRegistryConfiguration(consulHost, consulPort, OcelotConfiguration, serviceProviderConfig?.Token); _cache = cache; _consul = factory.Get(config); }
public ConsulOcelotConfigurationRepository(ConsulRegistryConfiguration consulRegistryConfiguration, Cache.IOcelotCache <IOcelotConfiguration> cache) { var consulHost = string.IsNullOrEmpty(consulRegistryConfiguration?.HostName) ? "localhost" : consulRegistryConfiguration.HostName; var consulPort = consulRegistryConfiguration?.Port ?? 8500; _configuration = new ConsulRegistryConfiguration(consulHost, consulPort, consulRegistryConfiguration?.ServiceName); _cache = cache; _consul = new ConsulClient(config => { config.Address = new Uri($"http://{_configuration.HostName}:{_configuration.Port}"); }); }
/// <inheritdoc /> public IConsulClient Get(ConsulRegistryConfiguration config) { return(new ConsulClient(option => { option.Address = new Uri($"http://{config.Host}:{config.Port}"); if (!string.IsNullOrEmpty(config?.Token)) { option.Token = config.Token; } })); }
public ConsulServiceDiscoveryProvider(ConsulRegistryConfiguration config, IOcelotLoggerFactory factory, IConsulClientFactory clientFactory) { ; _logger = factory.CreateLogger <ConsulServiceDiscoveryProvider>(); var consulHost = string.IsNullOrEmpty(config?.Host) ? "localhost" : config.Host; var consulPort = config?.Port ?? 8500; _config = new ConsulRegistryConfiguration(consulHost, consulPort, config?.KeyOfServiceInConsul, config?.Token); _consul = clientFactory.Get(_config); }
public void GivenOcelotIsRunningUsingConsulToStoreConfig(ConsulRegistryConfiguration consulConfig) { _webHostBuilder = new WebHostBuilder(); _webHostBuilder.ConfigureServices(s => { s.AddSingleton(_webHostBuilder); s.AddOcelotStoreConfigurationInConsul(consulConfig); }); _ocelotServer = new TestServer(_webHostBuilder .UseStartup <Startup>()); _ocelotClient = _ocelotServer.CreateClient(); }
public ConsulServiceDiscoveryProvider(ConsulRegistryConfiguration consulRegistryConfiguration, IOcelotLoggerFactory factory) { ; _logger = factory.CreateLogger <ConsulServiceDiscoveryProvider>(); var consulHost = string.IsNullOrEmpty(consulRegistryConfiguration?.HostName) ? "localhost" : consulRegistryConfiguration.HostName; var consulPort = consulRegistryConfiguration?.Port ?? 8500; _consulConfig = new ConsulRegistryConfiguration(consulHost, consulPort, consulRegistryConfiguration?.KeyOfServiceInConsul); _consul = new ConsulClient(config => { config.Address = new Uri($"http://{_consulConfig.HostName}:{_consulConfig.Port}"); }); }
public ConsulServiceDiscoveryProviderTests() { _serviceName = "test"; _port = 8500; _consulHost = "localhost"; _fakeConsulServiceDiscoveryUrl = $"http://{_consulHost}:{_port}"; _serviceEntries = new List <ServiceEntry>(); _factory = new Mock <IOcelotLoggerFactory>(); _logger = new Mock <IOcelotLogger>(); _factory.Setup(x => x.CreateLogger <ConsulServiceDiscoveryProvider>()).Returns(_logger.Object); var config = new ConsulRegistryConfiguration(_consulHost, _port, _serviceName); _provider = new ConsulServiceDiscoveryProvider(config, _factory.Object); }
public void should_return_response_200_with_simple_url() { var configuration = new FileConfiguration { ReRoutes = new List <FileReRoute> { new FileReRoute { DownstreamPathTemplate = "/", DownstreamScheme = "http", DownstreamHost = "localhost", DownstreamPort = 51779, UpstreamPathTemplate = "/", UpstreamHttpMethod = new List <string> { "Get" }, } }, GlobalConfiguration = new FileGlobalConfiguration() { ServiceDiscoveryProvider = new FileServiceDiscoveryProvider() { Provider = "Consul", Host = "localhost", Port = 9500 } } }; var fakeConsulServiceDiscoveryUrl = "http://localhost:9500"; var consulConfig = new ConsulRegistryConfiguration("localhost", 9500, "Ocelot"); this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl)) .And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", 200, "Hello from Laura")) .And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfig(consulConfig)) .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) .BDDfy(); }
/// <summary> /// 启用 Grpc Client 客户端连接 /// </summary> /// <param name="configs"></param> /// <param name="consulConfig">Consul 服务器配置</param> public static void UseGrpcClient(this IModuleConfigurations configs, ConsulRegistryConfiguration consulConfig) { var clientConfig = configs.AbpConfiguration.Get <IGrpcClientConfiguration>(); clientConfig.ConsulRegistryConfiguration = consulConfig; }
public static IServiceCollection AddOcelotStoreConfigurationInConsul(this IServiceCollection services, ConsulRegistryConfiguration consulConfig) { services.AddSingleton <ConsulRegistryConfiguration>(consulConfig); services.AddSingleton <IOcelotConfigurationRepository, ConsulOcelotConfigurationRepository>(); return(services); }