mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-17 22:23:21 +08:00
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:
parent
9db4273f18
commit
0f7aaa097d
@ -18,7 +18,8 @@ will be used.
|
|||||||
|
|
||||||
"ServiceDiscoveryProvider": {
|
"ServiceDiscoveryProvider": {
|
||||||
"Host": "localhost",
|
"Host": "localhost",
|
||||||
"Port": 8500
|
"Port": 8500,
|
||||||
|
"Type": "Consul"
|
||||||
}
|
}
|
||||||
|
|
||||||
In the future we can add a feature that allows ReRoute specfic configuration.
|
In the future we can add a feature that allows ReRoute specfic configuration.
|
||||||
@ -136,8 +137,8 @@ The config might look something like
|
|||||||
"RequestIdKey": null,
|
"RequestIdKey": null,
|
||||||
"ServiceDiscoveryProvider": {
|
"ServiceDiscoveryProvider": {
|
||||||
"Host": "localhost",
|
"Host": "localhost",
|
||||||
"Port": 8510,
|
"Port": 8500,
|
||||||
"Type": null,
|
"Type": "Consul",
|
||||||
"Token": null,
|
"Token": null,
|
||||||
"ConfigurationKey": null
|
"ConfigurationKey": null
|
||||||
},
|
},
|
||||||
|
@ -8,13 +8,16 @@ namespace Ocelot.Configuration.Creator
|
|||||||
public ServiceProviderConfiguration Create(FileGlobalConfiguration globalConfiguration)
|
public ServiceProviderConfiguration Create(FileGlobalConfiguration globalConfiguration)
|
||||||
{
|
{
|
||||||
var port = globalConfiguration?.ServiceDiscoveryProvider?.Port ?? 0;
|
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;
|
var pollingInterval = globalConfiguration?.ServiceDiscoveryProvider?.PollingInterval ?? 0;
|
||||||
|
|
||||||
return new ServiceProviderConfigurationBuilder()
|
return new ServiceProviderConfigurationBuilder()
|
||||||
.WithHost(host)
|
.WithHost(host)
|
||||||
.WithPort(port)
|
.WithPort(port)
|
||||||
.WithType(globalConfiguration?.ServiceDiscoveryProvider?.Type)
|
.WithType(type)
|
||||||
.WithToken(globalConfiguration?.ServiceDiscoveryProvider?.Token)
|
.WithToken(globalConfiguration?.ServiceDiscoveryProvider?.Token)
|
||||||
.WithConfigurationKey(globalConfiguration?.ServiceDiscoveryProvider?.ConfigurationKey)
|
.WithConfigurationKey(globalConfiguration?.ServiceDiscoveryProvider?.ConfigurationKey)
|
||||||
.WithPollingInterval(pollingInterval)
|
.WithPollingInterval(pollingInterval)
|
||||||
|
@ -5,13 +5,16 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Configuration;
|
using Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
|
||||||
public class DownstreamRouteProviderFactory : IDownstreamRouteProviderFactory
|
public class DownstreamRouteProviderFactory : IDownstreamRouteProviderFactory
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, IDownstreamRouteProvider> _providers;
|
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);
|
_providers = provider.GetServices<IDownstreamRouteProvider>().ToDictionary(x => x.GetType().Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,6 +22,7 @@
|
|||||||
{
|
{
|
||||||
if(!config.ReRoutes.Any() && IsServiceDiscovery(config.ServiceProviderConfiguration))
|
if(!config.ReRoutes.Any() && IsServiceDiscovery(config.ServiceProviderConfiguration))
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation($"Selected {nameof(DownstreamRouteCreator)} as DownstreamRouteProvider for this request");
|
||||||
return _providers[nameof(DownstreamRouteCreator)];
|
return _providers[nameof(DownstreamRouteCreator)];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +31,7 @@
|
|||||||
|
|
||||||
private bool IsServiceDiscovery(ServiceProviderConfiguration config)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1,61 +1,63 @@
|
|||||||
namespace Ocelot.ManualTest
|
namespace Ocelot.ManualTest
|
||||||
{
|
{
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Ocelot.DependencyInjection;
|
using Ocelot.DependencyInjection;
|
||||||
using Ocelot.Middleware;
|
using Ocelot.Middleware;
|
||||||
|
using System;
|
||||||
public class Program
|
using IdentityServer4.AccessTokenValidation;
|
||||||
{
|
|
||||||
public static void Main(string[] args)
|
public class Program
|
||||||
{
|
{
|
||||||
new WebHostBuilder()
|
public static void Main(string[] args)
|
||||||
.UseKestrel()
|
{
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
new WebHostBuilder()
|
||||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
.UseKestrel()
|
||||||
{
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
config
|
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||||
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
|
{
|
||||||
.AddJsonFile("appsettings.json", true, true)
|
config
|
||||||
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
|
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
|
||||||
.AddJsonFile("ocelot.json")
|
.AddJsonFile("appsettings.json", true, true)
|
||||||
.AddEnvironmentVariables();
|
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
|
||||||
})
|
.AddJsonFile("ocelot.json")
|
||||||
.ConfigureServices(s => {
|
.AddEnvironmentVariables();
|
||||||
s.AddAuthentication()
|
})
|
||||||
.AddJwtBearer("TestKey", x =>
|
.ConfigureServices(s => {
|
||||||
{
|
s.AddAuthentication()
|
||||||
x.Authority = "test";
|
.AddJwtBearer("TestKey", x =>
|
||||||
x.Audience = "test";
|
{
|
||||||
});
|
x.Authority = "test";
|
||||||
|
x.Audience = "test";
|
||||||
s.AddOcelot()
|
});
|
||||||
.AddCacheManager(x =>
|
|
||||||
{
|
s.AddOcelot()
|
||||||
x.WithDictionaryHandle();
|
.AddCacheManager(x =>
|
||||||
})
|
{
|
||||||
/*.AddOpenTracing(option =>
|
x.WithDictionaryHandle();
|
||||||
{
|
})
|
||||||
option.CollectorUrl = "http://localhost:9618";
|
/*.AddOpenTracing(option =>
|
||||||
option.Service = "Ocelot.ManualTest";
|
{
|
||||||
})*/
|
option.CollectorUrl = "http://localhost:9618";
|
||||||
.AddAdministration("/administration", "secret");
|
option.Service = "Ocelot.ManualTest";
|
||||||
})
|
})*/
|
||||||
.ConfigureLogging((hostingContext, logging) =>
|
.AddAdministration("/administration", "secret");
|
||||||
{
|
})
|
||||||
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
.ConfigureLogging((hostingContext, logging) =>
|
||||||
logging.AddConsole();
|
{
|
||||||
})
|
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||||
.UseIISIntegration()
|
logging.AddConsole();
|
||||||
.Configure(app =>
|
})
|
||||||
{
|
.UseIISIntegration()
|
||||||
app.UseOcelot().Wait();
|
.Configure(app =>
|
||||||
})
|
{
|
||||||
.Build()
|
app.UseOcelot().Wait();
|
||||||
.Run();
|
})
|
||||||
}
|
.Build()
|
||||||
}
|
.Run();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,26 +1,28 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.Finder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Ocelot.Values;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||||
{
|
{
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.Finder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Ocelot.Values;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
using Ocelot.Configuration.Creator;
|
using Ocelot.Configuration.Creator;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
|
||||||
public class DownstreamRouteProviderFactoryTests
|
public class DownstreamRouteProviderFactoryTests
|
||||||
{
|
{
|
||||||
private readonly DownstreamRouteProviderFactory _factory;
|
private readonly DownstreamRouteProviderFactory _factory;
|
||||||
private IInternalConfiguration _config;
|
private IInternalConfiguration _config;
|
||||||
private IDownstreamRouteProvider _result;
|
private IDownstreamRouteProvider _result;
|
||||||
|
private Mock<IOcelotLogger> _logger;
|
||||||
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||||
|
|
||||||
public DownstreamRouteProviderFactoryTests()
|
public DownstreamRouteProviderFactoryTests()
|
||||||
{
|
{
|
||||||
@ -31,7 +33,10 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
|
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
|
||||||
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
|
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
|
||||||
var provider = services.BuildServiceProvider();
|
var provider = services.BuildServiceProvider();
|
||||||
_factory = new DownstreamRouteProviderFactory(provider);
|
_logger = new Mock<IOcelotLogger>();
|
||||||
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||||
|
_loggerFactory.Setup(x => x.CreateLogger<DownstreamRouteProviderFactory>()).Returns(_logger.Object);
|
||||||
|
_factory = new DownstreamRouteProviderFactory(provider, _loggerFactory.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -49,12 +54,34 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_downstream_route_finder_as_no_service_discovery()
|
public void should_return_downstream_route_finder_as_no_service_discovery_given_no_host()
|
||||||
{
|
{
|
||||||
var spConfig = new ServiceProviderConfigurationBuilder().Build();
|
var spConfig = new ServiceProviderConfigurationBuilder().WithHost("").WithPort(50).Build();
|
||||||
var reRoutes = new List<ReRoute>
|
var reRoutes = new List<ReRoute>();
|
||||||
{
|
|
||||||
};
|
this.Given(_ => GivenTheReRoutes(reRoutes, spConfig))
|
||||||
|
.When(_ => WhenIGet())
|
||||||
|
.Then(_ => ThenTheResultShouldBe<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_return_downstream_route_finder_given_no_service_discovery_port()
|
||||||
|
{
|
||||||
|
var spConfig = new ServiceProviderConfigurationBuilder().WithHost("localhost").WithPort(0).Build();
|
||||||
|
var reRoutes = new List<ReRoute>();
|
||||||
|
|
||||||
|
this.Given(_ => GivenTheReRoutes(reRoutes, spConfig))
|
||||||
|
.When(_ => WhenIGet())
|
||||||
|
.Then(_ => ThenTheResultShouldBe<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_return_downstream_route_finder_given_no_service_discovery_type()
|
||||||
|
{
|
||||||
|
var spConfig = new ServiceProviderConfigurationBuilder().WithHost("localhost").WithPort(50).WithType("").Build();
|
||||||
|
var reRoutes = new List<ReRoute>();
|
||||||
|
|
||||||
this.Given(_ => GivenTheReRoutes(reRoutes, spConfig))
|
this.Given(_ => GivenTheReRoutes(reRoutes, spConfig))
|
||||||
.When(_ => WhenIGet())
|
.When(_ => WhenIGet())
|
||||||
@ -65,10 +92,9 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_downstream_route_creator()
|
public void should_return_downstream_route_creator()
|
||||||
{
|
{
|
||||||
var spConfig = new ServiceProviderConfigurationBuilder().WithHost("test").WithPort(50).Build();
|
var spConfig = new ServiceProviderConfigurationBuilder().WithHost("test").WithPort(50).WithType("test").Build();
|
||||||
var reRoutes = new List<ReRoute>
|
var reRoutes = new List<ReRoute>();
|
||||||
{
|
|
||||||
};
|
|
||||||
this.Given(_ => GivenTheReRoutes(reRoutes, spConfig))
|
this.Given(_ => GivenTheReRoutes(reRoutes, spConfig))
|
||||||
.When(_ => WhenIGet())
|
.When(_ => WhenIGet())
|
||||||
.Then(_ => ThenTheResultShouldBe<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>())
|
.Then(_ => ThenTheResultShouldBe<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user