mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
more work towards getting service discovery working with load balancing
This commit is contained in:
parent
37aaeeed82
commit
07ca7989b0
@ -27,13 +27,19 @@ namespace Ocelot.Configuration.Creator
|
|||||||
|
|
||||||
private readonly IClaimToThingConfigurationParser _claimToThingConfigurationParser;
|
private readonly IClaimToThingConfigurationParser _claimToThingConfigurationParser;
|
||||||
private readonly ILogger<FileOcelotConfigurationCreator> _logger;
|
private readonly ILogger<FileOcelotConfigurationCreator> _logger;
|
||||||
|
private readonly ILoadBalancerFactory _loadBalanceFactory;
|
||||||
|
private readonly ILoadBalancerHouse _loadBalancerHouse;
|
||||||
|
|
||||||
public FileOcelotConfigurationCreator(
|
public FileOcelotConfigurationCreator(
|
||||||
IOptions<FileConfiguration> options,
|
IOptions<FileConfiguration> options,
|
||||||
IConfigurationValidator configurationValidator,
|
IConfigurationValidator configurationValidator,
|
||||||
IClaimToThingConfigurationParser claimToThingConfigurationParser,
|
IClaimToThingConfigurationParser claimToThingConfigurationParser,
|
||||||
ILogger<FileOcelotConfigurationCreator> logger)
|
ILogger<FileOcelotConfigurationCreator> logger,
|
||||||
|
ILoadBalancerFactory loadBalancerFactory,
|
||||||
|
ILoadBalancerHouse loadBalancerHouse)
|
||||||
{
|
{
|
||||||
|
_loadBalanceFactory = loadBalancerFactory;
|
||||||
|
_loadBalancerHouse = loadBalancerHouse;
|
||||||
_options = options;
|
_options = options;
|
||||||
_configurationValidator = configurationValidator;
|
_configurationValidator = configurationValidator;
|
||||||
_claimToThingConfigurationParser = claimToThingConfigurationParser;
|
_claimToThingConfigurationParser = claimToThingConfigurationParser;
|
||||||
@ -78,55 +84,62 @@ namespace Ocelot.Configuration.Creator
|
|||||||
return new OcelotConfiguration(reRoutes);
|
return new OcelotConfiguration(reRoutes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ReRoute SetUpReRoute(FileReRoute reRoute, FileGlobalConfiguration globalConfiguration)
|
private ReRoute SetUpReRoute(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration)
|
||||||
{
|
{
|
||||||
var globalRequestIdConfiguration = !string.IsNullOrEmpty(globalConfiguration?.RequestIdKey);
|
var globalRequestIdConfiguration = !string.IsNullOrEmpty(globalConfiguration?.RequestIdKey);
|
||||||
|
|
||||||
var upstreamTemplate = BuildUpstreamTemplate(reRoute);
|
var upstreamTemplate = BuildUpstreamTemplate(fileReRoute);
|
||||||
|
|
||||||
var isAuthenticated = !string.IsNullOrEmpty(reRoute.AuthenticationOptions?.Provider);
|
var isAuthenticated = !string.IsNullOrEmpty(fileReRoute.AuthenticationOptions?.Provider);
|
||||||
|
|
||||||
var isAuthorised = reRoute.RouteClaimsRequirement?.Count > 0;
|
var isAuthorised = fileReRoute.RouteClaimsRequirement?.Count > 0;
|
||||||
|
|
||||||
var isCached = reRoute.FileCacheOptions.TtlSeconds > 0;
|
var isCached = fileReRoute.FileCacheOptions.TtlSeconds > 0;
|
||||||
|
|
||||||
var requestIdKey = globalRequestIdConfiguration
|
var requestIdKey = globalRequestIdConfiguration
|
||||||
? globalConfiguration.RequestIdKey
|
? globalConfiguration.RequestIdKey
|
||||||
: reRoute.RequestIdKey;
|
: fileReRoute.RequestIdKey;
|
||||||
|
|
||||||
var useServiceDiscovery = !string.IsNullOrEmpty(reRoute.ServiceName)
|
var useServiceDiscovery = !string.IsNullOrEmpty(fileReRoute.ServiceName)
|
||||||
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Address)
|
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Address)
|
||||||
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Provider);
|
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Provider);
|
||||||
|
|
||||||
|
ReRoute reRoute;
|
||||||
|
|
||||||
if (isAuthenticated)
|
if (isAuthenticated)
|
||||||
{
|
{
|
||||||
var authOptionsForRoute = new AuthenticationOptions(reRoute.AuthenticationOptions.Provider,
|
var authOptionsForRoute = new AuthenticationOptions(fileReRoute.AuthenticationOptions.Provider,
|
||||||
reRoute.AuthenticationOptions.ProviderRootUrl, reRoute.AuthenticationOptions.ScopeName,
|
fileReRoute.AuthenticationOptions.ProviderRootUrl, fileReRoute.AuthenticationOptions.ScopeName,
|
||||||
reRoute.AuthenticationOptions.RequireHttps, reRoute.AuthenticationOptions.AdditionalScopes,
|
fileReRoute.AuthenticationOptions.RequireHttps, fileReRoute.AuthenticationOptions.AdditionalScopes,
|
||||||
reRoute.AuthenticationOptions.ScopeSecret);
|
fileReRoute.AuthenticationOptions.ScopeSecret);
|
||||||
|
|
||||||
var claimsToHeaders = GetAddThingsToRequest(reRoute.AddHeadersToRequest);
|
var claimsToHeaders = GetAddThingsToRequest(fileReRoute.AddHeadersToRequest);
|
||||||
var claimsToClaims = GetAddThingsToRequest(reRoute.AddClaimsToRequest);
|
var claimsToClaims = GetAddThingsToRequest(fileReRoute.AddClaimsToRequest);
|
||||||
var claimsToQueries = GetAddThingsToRequest(reRoute.AddQueriesToRequest);
|
var claimsToQueries = GetAddThingsToRequest(fileReRoute.AddQueriesToRequest);
|
||||||
|
|
||||||
return new ReRoute(new DownstreamPathTemplate(reRoute.DownstreamPathTemplate), reRoute.UpstreamTemplate,
|
reRoute = new ReRoute(new DownstreamPathTemplate(fileReRoute.DownstreamPathTemplate), fileReRoute.UpstreamTemplate,
|
||||||
reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
|
fileReRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
|
||||||
authOptionsForRoute, claimsToHeaders, claimsToClaims,
|
authOptionsForRoute, claimsToHeaders, claimsToClaims,
|
||||||
reRoute.RouteClaimsRequirement, isAuthorised, claimsToQueries,
|
fileReRoute.RouteClaimsRequirement, isAuthorised, claimsToQueries,
|
||||||
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
|
requestIdKey, isCached, new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds),
|
||||||
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
fileReRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
||||||
globalConfiguration?.ServiceDiscoveryProvider?.Address, reRoute.DownstreamScheme,
|
globalConfiguration?.ServiceDiscoveryProvider?.Address, fileReRoute.DownstreamScheme,
|
||||||
reRoute.LoadBalancer, reRoute.DownstreamHost, reRoute.DownstreamPort);
|
fileReRoute.LoadBalancer, fileReRoute.DownstreamHost, fileReRoute.DownstreamPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ReRoute(new DownstreamPathTemplate(reRoute.DownstreamPathTemplate), reRoute.UpstreamTemplate,
|
reRoute = new ReRoute(new DownstreamPathTemplate(fileReRoute.DownstreamPathTemplate), fileReRoute.UpstreamTemplate,
|
||||||
reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
|
fileReRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
|
||||||
null, new List<ClaimToThing>(), new List<ClaimToThing>(),
|
null, new List<ClaimToThing>(), new List<ClaimToThing>(),
|
||||||
reRoute.RouteClaimsRequirement, isAuthorised, new List<ClaimToThing>(),
|
fileReRoute.RouteClaimsRequirement, isAuthorised, new List<ClaimToThing>(),
|
||||||
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
|
requestIdKey, isCached, new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds),
|
||||||
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
fileReRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
||||||
globalConfiguration?.ServiceDiscoveryProvider?.Address, reRoute.DownstreamScheme,
|
globalConfiguration?.ServiceDiscoveryProvider?.Address, fileReRoute.DownstreamScheme,
|
||||||
reRoute.LoadBalancer, reRoute.DownstreamHost, reRoute.DownstreamPort);
|
fileReRoute.LoadBalancer, fileReRoute.DownstreamHost, fileReRoute.DownstreamPort);
|
||||||
|
|
||||||
|
var loadBalancer = _loadBalanceFactory.Get(reRoute);
|
||||||
|
//todo - not sure if this is the correct key, but this is probably the only unique key i can think of
|
||||||
|
_loadBalancerHouse.Add($"{fileReRoute.UpstreamTemplate}{fileReRoute.UpstreamHttpMethod}", loadBalancer);
|
||||||
|
return reRoute;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string BuildUpstreamTemplate(FileReRoute reRoute)
|
private string BuildUpstreamTemplate(FileReRoute reRoute)
|
||||||
|
@ -23,11 +23,13 @@ using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
|||||||
using Ocelot.Headers;
|
using Ocelot.Headers;
|
||||||
using Ocelot.Infrastructure.Claims.Parser;
|
using Ocelot.Infrastructure.Claims.Parser;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Ocelot.LoadBalancer.LoadBalancers;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.QueryStrings;
|
using Ocelot.QueryStrings;
|
||||||
using Ocelot.Request.Builder;
|
using Ocelot.Request.Builder;
|
||||||
using Ocelot.Requester;
|
using Ocelot.Requester;
|
||||||
using Ocelot.Responder;
|
using Ocelot.Responder;
|
||||||
|
using Ocelot.ServiceDiscovery;
|
||||||
|
|
||||||
namespace Ocelot.DependencyInjection
|
namespace Ocelot.DependencyInjection
|
||||||
{
|
{
|
||||||
@ -59,6 +61,10 @@ namespace Ocelot.DependencyInjection
|
|||||||
{
|
{
|
||||||
services.AddMvcCore().AddJsonFormatters();
|
services.AddMvcCore().AddJsonFormatters();
|
||||||
services.AddLogging();
|
services.AddLogging();
|
||||||
|
services.AddSingleton<Ocelot.ServiceDiscovery.IServiceProviderFactory, Ocelot.ServiceDiscovery.IServiceProviderFactory>();
|
||||||
|
services.AddSingleton<ILoadBalancerFactory, ILoadBalancerFactory>();
|
||||||
|
services.AddSingleton<ILoadBalancerHouse, LoadBalancerHouse>();
|
||||||
|
services.AddSingleton<IServiceProviderHouse, ServiceProviderHouse>();
|
||||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
services.AddSingleton<IUrlBuilder, UrlBuilder>();
|
services.AddSingleton<IUrlBuilder, UrlBuilder>();
|
||||||
services.AddSingleton<IRemoveOutputHeaders, RemoveOutputHeaders>();
|
services.AddSingleton<IRemoveOutputHeaders, RemoveOutputHeaders>();
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
namespace Ocelot.LoadBalancer.LoadBalancers
|
using Ocelot.Configuration;
|
||||||
|
|
||||||
|
namespace Ocelot.LoadBalancer.LoadBalancers
|
||||||
{
|
{
|
||||||
public interface ILoadBalancerFactory
|
public interface ILoadBalancerFactory
|
||||||
{
|
{
|
||||||
ILoadBalancer Get(string serviceName, string loadBalancer);
|
ILoadBalancer Get(ReRoute reRoute);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,26 +1,34 @@
|
|||||||
using Ocelot.ServiceDiscovery;
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.ServiceDiscovery;
|
||||||
|
|
||||||
namespace Ocelot.LoadBalancer.LoadBalancers
|
namespace Ocelot.LoadBalancer.LoadBalancers
|
||||||
{
|
{
|
||||||
public class LoadBalancerFactory : ILoadBalancerFactory
|
public class LoadBalancerFactory : ILoadBalancerFactory
|
||||||
{
|
{
|
||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly IServiceProviderFactory _serviceProviderFactory;
|
||||||
|
public LoadBalancerFactory(IServiceProviderFactory serviceProviderFactory)
|
||||||
public LoadBalancerFactory(IServiceProvider serviceProvider)
|
|
||||||
{
|
{
|
||||||
_serviceProvider = serviceProvider;
|
_serviceProviderFactory = serviceProviderFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ILoadBalancer Get(string serviceName, string loadBalancer)
|
public ILoadBalancer Get(ReRoute reRoute)
|
||||||
{
|
{
|
||||||
switch (loadBalancer)
|
var serviceConfig = new ServiceConfiguraion(
|
||||||
|
reRoute.ServiceName,
|
||||||
|
reRoute.DownstreamHost,
|
||||||
|
reRoute.DownstreamPort,
|
||||||
|
reRoute.UseServiceDiscovery);
|
||||||
|
|
||||||
|
var serviceProvider = _serviceProviderFactory.Get(serviceConfig);
|
||||||
|
|
||||||
|
switch (reRoute.LoadBalancer)
|
||||||
{
|
{
|
||||||
case "RoundRobin":
|
case "RoundRobin":
|
||||||
return new RoundRobinLoadBalancer(_serviceProvider.Get());
|
return new RoundRobinLoadBalancer(serviceProvider.Get());
|
||||||
case "LeastConnection":
|
case "LeastConnection":
|
||||||
return new LeastConnectionLoadBalancer(() => _serviceProvider.Get(), serviceName);
|
return new LeastConnectionLoadBalancer(() => serviceProvider.Get(), reRoute.ServiceName);
|
||||||
default:
|
default:
|
||||||
return new NoLoadBalancer(_serviceProvider.Get());
|
return new NoLoadBalancer(serviceProvider.Get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,37 +14,30 @@ namespace Ocelot.LoadBalancer.Middleware
|
|||||||
{
|
{
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
private readonly IOcelotLogger _logger;
|
private readonly IOcelotLogger _logger;
|
||||||
|
private readonly ILoadBalancerHouse _loadBalancerHouse;
|
||||||
|
|
||||||
public LoadBalancingMiddleware(RequestDelegate next,
|
public LoadBalancingMiddleware(RequestDelegate next,
|
||||||
IOcelotLoggerFactory loggerFactory,
|
IOcelotLoggerFactory loggerFactory,
|
||||||
IRequestScopedDataRepository requestScopedDataRepository)
|
IRequestScopedDataRepository requestScopedDataRepository,
|
||||||
|
ILoadBalancerHouse loadBalancerHouse)
|
||||||
: base(requestScopedDataRepository)
|
: base(requestScopedDataRepository)
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_logger = loggerFactory.CreateLogger<QueryStringBuilderMiddleware>();
|
_logger = loggerFactory.CreateLogger<QueryStringBuilderMiddleware>();
|
||||||
|
_loadBalancerHouse = loadBalancerHouse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("started calling query string builder middleware");
|
_logger.LogDebug("started calling query string builder middleware");
|
||||||
|
|
||||||
//todo - get out of di? or do this when we bootstrap?
|
var loadBalancer = _loadBalancerHouse.Get($"{DownstreamRoute.ReRoute.UpstreamTemplate}{DownstreamRoute.ReRoute.UpstreamHttpMethod}");
|
||||||
var serviceProviderFactory = new ServiceProviderFactory();
|
//todo check reponse and return error
|
||||||
var serviceConfig = new ServiceConfiguraion(
|
|
||||||
DownstreamRoute.ReRoute.ServiceName,
|
|
||||||
DownstreamRoute.ReRoute.DownstreamHost,
|
|
||||||
DownstreamRoute.ReRoute.DownstreamPort,
|
|
||||||
DownstreamRoute.ReRoute.UseServiceDiscovery);
|
|
||||||
//todo - get this out of some kind of service provider house?
|
|
||||||
var serviceProvider = serviceProviderFactory.Get(serviceConfig);
|
|
||||||
|
|
||||||
//todo - get out of di? or do this when we bootstrap?
|
var response = loadBalancer.Data.Lease();
|
||||||
var loadBalancerFactory = new LoadBalancerFactory(serviceProvider);
|
//todo check reponse and return error
|
||||||
//todo - currently instanciates a load balancer per request which is wrong,
|
|
||||||
//need some kind of load balance house! :)
|
|
||||||
var loadBalancer = loadBalancerFactory.Get(DownstreamRoute.ReRoute.ServiceName, DownstreamRoute.ReRoute.LoadBalancer);
|
|
||||||
var response = loadBalancer.Lease();
|
|
||||||
|
|
||||||
|
SetHostAndPortForThisRequest(response.Data);
|
||||||
_logger.LogDebug("calling next middleware");
|
_logger.LogDebug("calling next middleware");
|
||||||
|
|
||||||
//todo - try next middleware if we get an exception make sure we release
|
//todo - try next middleware if we get an exception make sure we release
|
||||||
@ -53,11 +46,11 @@ namespace Ocelot.LoadBalancer.Middleware
|
|||||||
{
|
{
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
|
|
||||||
loadBalancer.Release(response.Data);
|
loadBalancer.Data.Release(response.Data);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
loadBalancer.Release(response.Data);
|
loadBalancer.Data.Release(response.Data);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
|
||||||
|
namespace Ocelot.LoadBalancer.Middleware
|
||||||
|
{
|
||||||
|
public static class LoadBalancingMiddlewareExtensions
|
||||||
|
{
|
||||||
|
public static IApplicationBuilder UseLoadBalancingMiddlewareExtensions(this IApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
return builder.UseMiddleware<LoadBalancingMiddleware>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ using System.Net.Http;
|
|||||||
using Ocelot.DownstreamRouteFinder;
|
using Ocelot.DownstreamRouteFinder;
|
||||||
using Ocelot.Errors;
|
using Ocelot.Errors;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Ocelot.Values;
|
||||||
|
|
||||||
namespace Ocelot.Middleware
|
namespace Ocelot.Middleware
|
||||||
{
|
{
|
||||||
@ -69,6 +70,20 @@ namespace Ocelot.Middleware
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HostAndPort HostAndPort
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var hostAndPort = _requestScopedDataRepository.Get<HostAndPort>("HostAndPort");
|
||||||
|
return hostAndPort.Data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetHostAndPortForThisRequest(HostAndPort hostAndPort)
|
||||||
|
{
|
||||||
|
_requestScopedDataRepository.Add("HostAndPort", hostAndPort);
|
||||||
|
}
|
||||||
|
|
||||||
public void SetDownstreamRouteForThisRequest(DownstreamRoute downstreamRoute)
|
public void SetDownstreamRouteForThisRequest(DownstreamRoute downstreamRoute)
|
||||||
{
|
{
|
||||||
_requestScopedDataRepository.Add("DownstreamRoute", downstreamRoute);
|
_requestScopedDataRepository.Add("DownstreamRoute", downstreamRoute);
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
namespace Ocelot.ServiceDiscovery
|
namespace Ocelot.ServiceDiscovery
|
||||||
{
|
{
|
||||||
public interface IServiceProviderFactory
|
public interface IServiceProviderFactory
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
|
|
||||||
namespace Ocelot.ServiceDiscovery
|
|
||||||
{
|
|
||||||
public interface IServiceProviderHouse
|
|
||||||
{
|
|
||||||
Response<IServiceProvider> Get(string key);
|
|
||||||
Response Add(string key, IServiceProvider serviceProvider);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
|
|
||||||
namespace Ocelot.ServiceDiscovery
|
|
||||||
{
|
|
||||||
public class ServiceProviderHouse : IServiceProviderHouse
|
|
||||||
{
|
|
||||||
private Dictionary<string, Ocelot.ServiceDiscovery.IServiceProvider> _serviceProviders;
|
|
||||||
|
|
||||||
public ServiceProviderHouse()
|
|
||||||
{
|
|
||||||
_serviceProviders = new Dictionary<string, Ocelot.ServiceDiscovery.IServiceProvider>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Response<IServiceProvider> Get(string key)
|
|
||||||
{
|
|
||||||
IServiceProvider serviceProvider;
|
|
||||||
if(_serviceProviders.TryGetValue(key, out serviceProvider))
|
|
||||||
{
|
|
||||||
return new OkResponse<Ocelot.ServiceDiscovery.IServiceProvider>(serviceProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ErrorResponse<IServiceProvider>(new List<Ocelot.Errors.Error>()
|
|
||||||
{
|
|
||||||
new UnableToFindServiceProviderError($"unabe to find service provider for {key}")
|
|
||||||
});
|
|
||||||
}
|
|
||||||
public Response Add(string key, IServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
_serviceProviders[key] = serviceProvider;
|
|
||||||
return new OkResponse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,6 +8,7 @@ using Ocelot.Configuration.Creator;
|
|||||||
using Ocelot.Configuration.File;
|
using Ocelot.Configuration.File;
|
||||||
using Ocelot.Configuration.Parser;
|
using Ocelot.Configuration.Parser;
|
||||||
using Ocelot.Configuration.Validator;
|
using Ocelot.Configuration.Validator;
|
||||||
|
using Ocelot.LoadBalancer.LoadBalancers;
|
||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
@ -24,6 +25,8 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
private readonly Mock<IClaimToThingConfigurationParser> _configParser;
|
private readonly Mock<IClaimToThingConfigurationParser> _configParser;
|
||||||
private readonly Mock<ILogger<FileOcelotConfigurationCreator>> _logger;
|
private readonly Mock<ILogger<FileOcelotConfigurationCreator>> _logger;
|
||||||
private readonly FileOcelotConfigurationCreator _ocelotConfigurationCreator;
|
private readonly FileOcelotConfigurationCreator _ocelotConfigurationCreator;
|
||||||
|
private readonly Mock<ILoadBalancerFactory> _loadBalancerFactory;
|
||||||
|
private readonly Mock<ILoadBalancerHouse> _loadBalancerHouse;
|
||||||
|
|
||||||
public FileConfigurationCreatorTests()
|
public FileConfigurationCreatorTests()
|
||||||
{
|
{
|
||||||
@ -32,7 +35,8 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
_validator = new Mock<IConfigurationValidator>();
|
_validator = new Mock<IConfigurationValidator>();
|
||||||
_fileConfig = new Mock<IOptions<FileConfiguration>>();
|
_fileConfig = new Mock<IOptions<FileConfiguration>>();
|
||||||
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
|
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
|
||||||
_fileConfig.Object, _validator.Object, _configParser.Object, _logger.Object);
|
_fileConfig.Object, _validator.Object, _configParser.Object, _logger.Object,
|
||||||
|
_loadBalancerFactory.Object, _loadBalancerHouse.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -14,12 +14,12 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
private ReRoute _reRoute;
|
private ReRoute _reRoute;
|
||||||
private LoadBalancerFactory _factory;
|
private LoadBalancerFactory _factory;
|
||||||
private ILoadBalancer _result;
|
private ILoadBalancer _result;
|
||||||
private Mock<IServiceProvider> _serviceProvider;
|
private Mock<IServiceProviderFactory> _serviceProviderFactory;
|
||||||
|
|
||||||
public LoadBalancerFactoryTests()
|
public LoadBalancerFactoryTests()
|
||||||
{
|
{
|
||||||
_serviceProvider = new Mock<IServiceProvider>();
|
_serviceProviderFactory = new Mock<IServiceProviderFactory>();
|
||||||
_factory = new LoadBalancerFactory(_serviceProvider.Object);
|
_factory = new LoadBalancerFactory(_serviceProviderFactory.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -75,8 +75,8 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
|
|
||||||
private void ThenTheServiceProviderIsCalledCorrectly()
|
private void ThenTheServiceProviderIsCalledCorrectly()
|
||||||
{
|
{
|
||||||
_serviceProvider
|
_serviceProviderFactory
|
||||||
.Verify(x => x.Get(), Times.Once);
|
.Verify(x => x.Get(It.IsAny<ServiceConfiguraion>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenAReRoute(ReRoute reRoute)
|
private void GivenAReRoute(ReRoute reRoute)
|
||||||
@ -86,7 +86,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
|
|
||||||
private void WhenIGetTheLoadBalancer()
|
private void WhenIGetTheLoadBalancer()
|
||||||
{
|
{
|
||||||
_result = _factory.Get(_reRoute.ServiceName, _reRoute.LoadBalancer);
|
_result = _factory.Get(_reRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheLoadBalancerIsReturned<T>()
|
private void ThenTheLoadBalancerIsReturned<T>()
|
||||||
|
@ -0,0 +1,124 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Ocelot.LoadBalancer.LoadBalancers;
|
||||||
|
using Ocelot.LoadBalancer.Middleware;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Ocelot.Values;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests.LoadBalancer
|
||||||
|
{
|
||||||
|
public class LoadBalancerMiddlewareTests
|
||||||
|
{
|
||||||
|
private readonly Mock<ILoadBalancerHouse> _loadBalancerHouse;
|
||||||
|
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||||
|
private readonly Mock<ILoadBalancer> _loadBalancer;
|
||||||
|
private readonly string _url;
|
||||||
|
private readonly TestServer _server;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
private HttpResponseMessage _result;
|
||||||
|
private OkResponse<Ocelot.Request.Request> _request;
|
||||||
|
private OkResponse<string> _downstreamUrl;
|
||||||
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||||
|
|
||||||
|
public LoadBalancerMiddlewareTests()
|
||||||
|
{
|
||||||
|
_url = "http://localhost:51879";
|
||||||
|
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
||||||
|
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
||||||
|
var builder = new WebHostBuilder()
|
||||||
|
.ConfigureServices(x =>
|
||||||
|
{
|
||||||
|
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
x.AddLogging();
|
||||||
|
x.AddSingleton(_loadBalancerHouse.Object);
|
||||||
|
x.AddSingleton(_scopedRepository.Object);
|
||||||
|
})
|
||||||
|
.UseUrls(_url)
|
||||||
|
.UseKestrel()
|
||||||
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
|
.UseIISIntegration()
|
||||||
|
.UseUrls(_url)
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseLoadBalancingMiddlewareExtensions();
|
||||||
|
});
|
||||||
|
|
||||||
|
_server = new TestServer(builder);
|
||||||
|
_client = _server.CreateClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_call_scoped_data_repository_correctly()
|
||||||
|
{
|
||||||
|
var downstreamRoute = new DownstreamRoute(new List<Ocelot.DownstreamRouteFinder.UrlMatcher.UrlPathPlaceholderNameAndValue>(),
|
||||||
|
new ReRouteBuilder()
|
||||||
|
.Build());
|
||||||
|
|
||||||
|
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
|
||||||
|
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||||
|
.And(x => x.GivenTheLoadBalancerHouseReturns())
|
||||||
|
.And(x => x.GivenTheLoadBalancerReturns())
|
||||||
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
|
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheLoadBalancerReturns()
|
||||||
|
{
|
||||||
|
_loadBalancer
|
||||||
|
.Setup(x => x.Lease())
|
||||||
|
.Returns(new OkResponse<HostAndPort>(new HostAndPort("127.0.0.1", 80)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
|
{
|
||||||
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheLoadBalancerHouseReturns()
|
||||||
|
{
|
||||||
|
_loadBalancerHouse
|
||||||
|
.Setup(x => x.Get(It.IsAny<string>()))
|
||||||
|
.Returns(new OkResponse<ILoadBalancer>(_loadBalancer.Object));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||||
|
{
|
||||||
|
_scopedRepository
|
||||||
|
.Verify(x => x.Add("Request", _request.Data), Times.Once());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenICallTheMiddleware()
|
||||||
|
{
|
||||||
|
_result = _client.GetAsync(_url).Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
||||||
|
{
|
||||||
|
_downstreamUrl = new OkResponse<string>(downstreamUrl);
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(x => x.Get<string>(It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_client.Dispose();
|
||||||
|
_server.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,126 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Ocelot.LoadBalancer.LoadBalancers;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Ocelot.ServiceDiscovery;
|
|
||||||
using Ocelot.Values;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.LoadBalancer
|
|
||||||
{
|
|
||||||
public class ServiceProviderHouseTests
|
|
||||||
{
|
|
||||||
private Ocelot.ServiceDiscovery.IServiceProvider _serviceProvider;
|
|
||||||
private readonly ServiceProviderHouse _serviceProviderHouse;
|
|
||||||
private Response _addResult;
|
|
||||||
private Response<Ocelot.ServiceDiscovery.IServiceProvider> _getResult;
|
|
||||||
private string _key;
|
|
||||||
|
|
||||||
public ServiceProviderHouseTests()
|
|
||||||
{
|
|
||||||
_serviceProviderHouse = new ServiceProviderHouse();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_store_service_provider()
|
|
||||||
{
|
|
||||||
var key = "test";
|
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAServiceProvider(key, new FakeServiceProvider()))
|
|
||||||
.When(x => x.WhenIAddTheServiceProvider())
|
|
||||||
.Then(x => x.ThenItIsAdded())
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_get_service_provider()
|
|
||||||
{
|
|
||||||
var key = "test";
|
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAServiceProvider(key, new FakeServiceProvider()))
|
|
||||||
.When(x => x.WhenWeGetTheServiceProvider(key))
|
|
||||||
.Then(x => x.ThenItIsReturned())
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_store_service_providers_by_key()
|
|
||||||
{
|
|
||||||
var key = "test";
|
|
||||||
var keyTwo = "testTwo";
|
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAServiceProvider(key, new FakeServiceProvider()))
|
|
||||||
.And(x => x.GivenThereIsAServiceProvider(keyTwo, new FakeConsulServiceProvider()))
|
|
||||||
.When(x => x.WhenWeGetTheServiceProvider(key))
|
|
||||||
.Then(x => x.ThenTheServiceProviderIs<FakeServiceProvider>())
|
|
||||||
.When(x => x.WhenWeGetTheServiceProvider(keyTwo))
|
|
||||||
.Then(x => x.ThenTheServiceProviderIs<FakeConsulServiceProvider>())
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_return_error_if_no_service_provider_house_with_key()
|
|
||||||
{
|
|
||||||
this.When(x => x.WhenWeGetTheServiceProvider("test"))
|
|
||||||
.Then(x => x.ThenAnErrorIsReturned())
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenAnErrorIsReturned()
|
|
||||||
{
|
|
||||||
_getResult.IsError.ShouldBeTrue();
|
|
||||||
_getResult.Errors[0].ShouldBeOfType<UnableToFindServiceProviderError>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheServiceProviderIs<T>()
|
|
||||||
{
|
|
||||||
_getResult.Data.ShouldBeOfType<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenItIsAdded()
|
|
||||||
{
|
|
||||||
_addResult.IsError.ShouldBe(false);
|
|
||||||
_addResult.ShouldBeOfType<OkResponse>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenIAddTheServiceProvider()
|
|
||||||
{
|
|
||||||
_addResult = _serviceProviderHouse.Add(_key, _serviceProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenThereIsAServiceProvider(string key, Ocelot.ServiceDiscovery.IServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
_key = key;
|
|
||||||
_serviceProvider = serviceProvider;
|
|
||||||
WhenIAddTheServiceProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenWeGetTheServiceProvider(string key)
|
|
||||||
{
|
|
||||||
_getResult = _serviceProviderHouse.Get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenItIsReturned()
|
|
||||||
{
|
|
||||||
_getResult.Data.ShouldBe(_serviceProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
class FakeServiceProvider : Ocelot.ServiceDiscovery.IServiceProvider
|
|
||||||
{
|
|
||||||
public List<Service> Get()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class FakeConsulServiceProvider : Ocelot.ServiceDiscovery.IServiceProvider
|
|
||||||
{
|
|
||||||
public List<Service> Get()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user