dont use dynamic routing unless service discovery provider explictly set and log that we are going to use dynamic info (#437)

* #428 dont use dynamic routing unless service discovery provider explictly set and log that we are going to use dynamic info

* #428 fixed tests that were failing due to not maintaining backwards compat with config for service discovery
This commit is contained in:
Tom Pallister
2018-06-27 18:15:04 +01:00
committed by GitHub
parent 9db4273f18
commit 0f7aaa097d
5 changed files with 128 additions and 92 deletions

View File

@ -8,13 +8,16 @@ namespace Ocelot.Configuration.Creator
public ServiceProviderConfiguration Create(FileGlobalConfiguration globalConfiguration)
{
var port = globalConfiguration?.ServiceDiscoveryProvider?.Port ?? 0;
var host = globalConfiguration?.ServiceDiscoveryProvider?.Host ?? "consul";
var host = globalConfiguration?.ServiceDiscoveryProvider?.Host ?? "localhost";
var type = !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Type)
? globalConfiguration?.ServiceDiscoveryProvider?.Type
: "consul";
var pollingInterval = globalConfiguration?.ServiceDiscoveryProvider?.PollingInterval ?? 0;
return new ServiceProviderConfigurationBuilder()
.WithHost(host)
.WithPort(port)
.WithType(globalConfiguration?.ServiceDiscoveryProvider?.Type)
.WithType(type)
.WithToken(globalConfiguration?.ServiceDiscoveryProvider?.Token)
.WithConfigurationKey(globalConfiguration?.ServiceDiscoveryProvider?.ConfigurationKey)
.WithPollingInterval(pollingInterval)

View File

@ -5,13 +5,16 @@
using System.Linq;
using Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Logging;
public class DownstreamRouteProviderFactory : IDownstreamRouteProviderFactory
{
private readonly Dictionary<string, IDownstreamRouteProvider> _providers;
private IOcelotLogger _logger;
public DownstreamRouteProviderFactory(IServiceProvider provider)
public DownstreamRouteProviderFactory(IServiceProvider provider, IOcelotLoggerFactory factory)
{
_logger = factory.CreateLogger<DownstreamRouteProviderFactory>();
_providers = provider.GetServices<IDownstreamRouteProvider>().ToDictionary(x => x.GetType().Name);
}
@ -19,6 +22,7 @@
{
if(!config.ReRoutes.Any() && IsServiceDiscovery(config.ServiceProviderConfiguration))
{
_logger.LogInformation($"Selected {nameof(DownstreamRouteCreator)} as DownstreamRouteProvider for this request");
return _providers[nameof(DownstreamRouteCreator)];
}
@ -27,7 +31,7 @@
private bool IsServiceDiscovery(ServiceProviderConfiguration config)
{
if(!string.IsNullOrEmpty(config?.Host) || config?.Port > 0)
if(!string.IsNullOrEmpty(config?.Host) && config?.Port > 0 && !string.IsNullOrEmpty(config.Type))
{
return true;
}