mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
playing around with lb factory
This commit is contained in:
parent
4a43accc46
commit
0e92976df8
@ -32,12 +32,19 @@ namespace Ocelot.Configuration.Builder
|
|||||||
private string _downstreamScheme;
|
private string _downstreamScheme;
|
||||||
private string _downstreamHost;
|
private string _downstreamHost;
|
||||||
private int _dsPort;
|
private int _dsPort;
|
||||||
|
private string _loadBalancer;
|
||||||
|
|
||||||
public ReRouteBuilder()
|
public ReRouteBuilder()
|
||||||
{
|
{
|
||||||
_additionalScopes = new List<string>();
|
_additionalScopes = new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ReRouteBuilder WithLoadBalancer(string loadBalancer)
|
||||||
|
{
|
||||||
|
_loadBalancer = loadBalancer;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public ReRouteBuilder WithDownstreamScheme(string downstreamScheme)
|
public ReRouteBuilder WithDownstreamScheme(string downstreamScheme)
|
||||||
{
|
{
|
||||||
_downstreamScheme = downstreamScheme;
|
_downstreamScheme = downstreamScheme;
|
||||||
@ -200,7 +207,7 @@ namespace Ocelot.Configuration.Builder
|
|||||||
_isAuthenticated, new AuthenticationOptions(_authenticationProvider, _authenticationProviderUrl, _scopeName,
|
_isAuthenticated, new AuthenticationOptions(_authenticationProvider, _authenticationProviderUrl, _scopeName,
|
||||||
_requireHttps, _additionalScopes, _scopeSecret), _configHeaderExtractorProperties, _claimToClaims, _routeClaimRequirement,
|
_requireHttps, _additionalScopes, _scopeSecret), _configHeaderExtractorProperties, _claimToClaims, _routeClaimRequirement,
|
||||||
_isAuthorised, _claimToQueries, _requestIdHeaderKey, _isCached, _fileCacheOptions, _serviceName,
|
_isAuthorised, _claimToQueries, _requestIdHeaderKey, _isCached, _fileCacheOptions, _serviceName,
|
||||||
_useServiceDiscovery, _serviceDiscoveryAddress, _serviceDiscoveryProvider, downstreamHostFunc, _downstreamScheme);
|
_useServiceDiscovery, _serviceDiscoveryAddress, _serviceDiscoveryProvider, downstreamHostFunc, _downstreamScheme, _loadBalancer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,13 @@ namespace Ocelot.Configuration.Creator
|
|||||||
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Provider);
|
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Provider);
|
||||||
|
|
||||||
|
|
||||||
|
//can we do the logic in this func to get the host and port from the load balancer?
|
||||||
|
//lBfactory.GetLbForThisDownstreamTemplate
|
||||||
|
//use it in the func to get the next host and port?
|
||||||
|
//how do we release it? cant callback, could access the lb and release later?
|
||||||
|
|
||||||
|
//ideal world we would get the host and port, then make the request using it, then release the connection to the lb
|
||||||
|
|
||||||
Func<HostAndPort> downstreamHostAndPortFunc = () => new HostAndPort(reRoute.DownstreamHost.Trim('/'), reRoute.DownstreamPort);
|
Func<HostAndPort> downstreamHostAndPortFunc = () => new HostAndPort(reRoute.DownstreamHost.Trim('/'), reRoute.DownstreamPort);
|
||||||
|
|
||||||
if (isAuthenticated)
|
if (isAuthenticated)
|
||||||
@ -116,7 +123,8 @@ namespace Ocelot.Configuration.Creator
|
|||||||
reRoute.RouteClaimsRequirement, isAuthorised, claimsToQueries,
|
reRoute.RouteClaimsRequirement, isAuthorised, claimsToQueries,
|
||||||
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
|
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
|
||||||
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
||||||
globalConfiguration?.ServiceDiscoveryProvider?.Address, downstreamHostAndPortFunc, reRoute.DownstreamScheme);
|
globalConfiguration?.ServiceDiscoveryProvider?.Address, downstreamHostAndPortFunc, reRoute.DownstreamScheme,
|
||||||
|
reRoute.LoadBalancer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ReRoute(new DownstreamPathTemplate(reRoute.DownstreamPathTemplate), reRoute.UpstreamTemplate,
|
return new ReRoute(new DownstreamPathTemplate(reRoute.DownstreamPathTemplate), reRoute.UpstreamTemplate,
|
||||||
@ -125,7 +133,8 @@ namespace Ocelot.Configuration.Creator
|
|||||||
reRoute.RouteClaimsRequirement, isAuthorised, new List<ClaimToThing>(),
|
reRoute.RouteClaimsRequirement, isAuthorised, new List<ClaimToThing>(),
|
||||||
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
|
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
|
||||||
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
||||||
globalConfiguration?.ServiceDiscoveryProvider?.Address, downstreamHostAndPortFunc, reRoute.DownstreamScheme);
|
globalConfiguration?.ServiceDiscoveryProvider?.Address, downstreamHostAndPortFunc, reRoute.DownstreamScheme,
|
||||||
|
reRoute.LoadBalancer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string BuildUpstreamTemplate(FileReRoute reRoute)
|
private string BuildUpstreamTemplate(FileReRoute reRoute)
|
||||||
|
@ -29,5 +29,6 @@ namespace Ocelot.Configuration.File
|
|||||||
public string DownstreamScheme {get;set;}
|
public string DownstreamScheme {get;set;}
|
||||||
public string DownstreamHost {get;set;}
|
public string DownstreamHost {get;set;}
|
||||||
public int DownstreamPort { get; set; }
|
public int DownstreamPort { get; set; }
|
||||||
|
public string LoadBalancer {get;set;}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,8 +10,10 @@ namespace Ocelot.Configuration
|
|||||||
bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToThing> configurationHeaderExtractorProperties,
|
bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToThing> configurationHeaderExtractorProperties,
|
||||||
List<ClaimToThing> claimsToClaims, Dictionary<string, string> routeClaimsRequirement, bool isAuthorised, List<ClaimToThing> claimsToQueries,
|
List<ClaimToThing> claimsToClaims, Dictionary<string, string> routeClaimsRequirement, bool isAuthorised, List<ClaimToThing> claimsToQueries,
|
||||||
string requestIdKey, bool isCached, CacheOptions fileCacheOptions, string serviceName, bool useServiceDiscovery,
|
string requestIdKey, bool isCached, CacheOptions fileCacheOptions, string serviceName, bool useServiceDiscovery,
|
||||||
string serviceDiscoveryProvider, string serviceDiscoveryAddress, Func<HostAndPort> downstreamHostAndPort, string downstreamScheme)
|
string serviceDiscoveryProvider, string serviceDiscoveryAddress, Func<HostAndPort> downstreamHostAndPort,
|
||||||
|
string downstreamScheme, string loadBalancer)
|
||||||
{
|
{
|
||||||
|
LoadBalancer = loadBalancer;
|
||||||
DownstreamPathTemplate = downstreamPathTemplate;
|
DownstreamPathTemplate = downstreamPathTemplate;
|
||||||
UpstreamTemplate = upstreamTemplate;
|
UpstreamTemplate = upstreamTemplate;
|
||||||
UpstreamHttpMethod = upstreamHttpMethod;
|
UpstreamHttpMethod = upstreamHttpMethod;
|
||||||
@ -36,7 +38,6 @@ namespace Ocelot.Configuration
|
|||||||
DownstreamHostAndPort = downstreamHostAndPort;
|
DownstreamHostAndPort = downstreamHostAndPort;
|
||||||
DownstreamScheme = downstreamScheme;
|
DownstreamScheme = downstreamScheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DownstreamPathTemplate DownstreamPathTemplate { get; private set; }
|
public DownstreamPathTemplate DownstreamPathTemplate { get; private set; }
|
||||||
public string UpstreamTemplate { get; private set; }
|
public string UpstreamTemplate { get; private set; }
|
||||||
public string UpstreamTemplatePattern { get; private set; }
|
public string UpstreamTemplatePattern { get; private set; }
|
||||||
@ -57,5 +58,6 @@ namespace Ocelot.Configuration
|
|||||||
public string ServiceDiscoveryAddress { get; private set;}
|
public string ServiceDiscoveryAddress { get; private set;}
|
||||||
public Func<HostAndPort> DownstreamHostAndPort {get;private set;}
|
public Func<HostAndPort> DownstreamHostAndPort {get;private set;}
|
||||||
public string DownstreamScheme {get;private set;}
|
public string DownstreamScheme {get;private set;}
|
||||||
|
public string LoadBalancer {get;private set;}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -51,6 +51,8 @@ namespace Ocelot.DownstreamUrlCreator.Middleware
|
|||||||
|
|
||||||
//lease the next address from the lb
|
//lease the next address from the lb
|
||||||
|
|
||||||
|
//this could return the load balancer which you call next on, that gives you the host and port then you can call release in a try catch
|
||||||
|
//and if the call works?
|
||||||
var dsHostAndPort = DownstreamRoute.ReRoute.DownstreamHostAndPort();
|
var dsHostAndPort = DownstreamRoute.ReRoute.DownstreamHostAndPort();
|
||||||
|
|
||||||
var dsUrl = _urlBuilder.Build(dsPath.Data.Value, dsScheme, dsHostAndPort);
|
var dsUrl = _urlBuilder.Build(dsPath.Data.Value, dsScheme, dsHostAndPort);
|
||||||
|
95
test/Ocelot.UnitTests/LoadBalancerFactoryTests.cs
Normal file
95
test/Ocelot.UnitTests/LoadBalancerFactoryTests.cs
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
using System;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Ocelot.Values;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests
|
||||||
|
{
|
||||||
|
public class LoadBalancerFactoryTests
|
||||||
|
{
|
||||||
|
private ReRoute _reRoute;
|
||||||
|
private LoadBalancerFactory _factory;
|
||||||
|
private ILoadBalancer _result;
|
||||||
|
|
||||||
|
public LoadBalancerFactoryTests()
|
||||||
|
{
|
||||||
|
_factory = new LoadBalancerFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_return_no_load_balancer()
|
||||||
|
{
|
||||||
|
var reRoute = new ReRouteBuilder()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
this.Given(x => x.GivenAReRoute(reRoute))
|
||||||
|
.When(x => x.WhenIGetTheLoadBalancer())
|
||||||
|
.Then(x => x.ThenTheLoadBalancerIsReturned<NoLoadBalancer>())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_return_round_robin_load_balancer()
|
||||||
|
{
|
||||||
|
var reRoute = new ReRouteBuilder()
|
||||||
|
.WithLoadBalancer("RoundRobin")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
this.Given(x => x.GivenAReRoute(reRoute))
|
||||||
|
.When(x => x.WhenIGetTheLoadBalancer())
|
||||||
|
.Then(x => x.ThenTheLoadBalancerIsReturned<RoundRobinLoadBalancer>())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenAReRoute(ReRoute reRoute)
|
||||||
|
{
|
||||||
|
_reRoute = reRoute;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenIGetTheLoadBalancer()
|
||||||
|
{
|
||||||
|
_result = _factory.Get(_reRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheLoadBalancerIsReturned<T>()
|
||||||
|
{
|
||||||
|
_result.ShouldBeOfType<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NoLoadBalancer : ILoadBalancer
|
||||||
|
{
|
||||||
|
Response<HostAndPort> ILoadBalancer.Lease()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
Response ILoadBalancer.Release(HostAndPort hostAndPort)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ILoadBalancerFactory
|
||||||
|
{
|
||||||
|
ILoadBalancer Get(ReRoute reRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LoadBalancerFactory : ILoadBalancerFactory
|
||||||
|
{
|
||||||
|
public ILoadBalancer Get(ReRoute reRoute)
|
||||||
|
{
|
||||||
|
switch (reRoute.LoadBalancer)
|
||||||
|
{
|
||||||
|
case "RoundRobin":
|
||||||
|
return new RoundRobinLoadBalancer(null);
|
||||||
|
default:
|
||||||
|
return new NoLoadBalancer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,7 +10,7 @@ namespace Ocelot.UnitTests
|
|||||||
{
|
{
|
||||||
public class RoundRobinTests
|
public class RoundRobinTests
|
||||||
{
|
{
|
||||||
private readonly RoundRobin _roundRobin;
|
private readonly RoundRobinLoadBalancer _roundRobin;
|
||||||
private readonly List<HostAndPort> _hostAndPorts;
|
private readonly List<HostAndPort> _hostAndPorts;
|
||||||
private Response<HostAndPort> _hostAndPort;
|
private Response<HostAndPort> _hostAndPort;
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ namespace Ocelot.UnitTests
|
|||||||
new HostAndPort("127.0.0.1", 5001)
|
new HostAndPort("127.0.0.1", 5001)
|
||||||
};
|
};
|
||||||
|
|
||||||
_roundRobin = new RoundRobin(_hostAndPorts);
|
_roundRobin = new RoundRobinLoadBalancer(_hostAndPorts);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -71,12 +71,12 @@ namespace Ocelot.UnitTests
|
|||||||
Response Release(HostAndPort hostAndPort);
|
Response Release(HostAndPort hostAndPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RoundRobin : ILoadBalancer
|
public class RoundRobinLoadBalancer : ILoadBalancer
|
||||||
{
|
{
|
||||||
private readonly List<HostAndPort> _hostAndPorts;
|
private readonly List<HostAndPort> _hostAndPorts;
|
||||||
private int _last;
|
private int _last;
|
||||||
|
|
||||||
public RoundRobin(List<HostAndPort> hostAndPorts)
|
public RoundRobinLoadBalancer(List<HostAndPort> hostAndPorts)
|
||||||
{
|
{
|
||||||
_hostAndPorts = hostAndPorts;
|
_hostAndPorts = hostAndPorts;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user