-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from TomPallister/feature/config-in-consul
added a new implementation that stores the ocelot config in consul kv…
- Loading branch information
Showing
34 changed files
with
623 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
using Ocelot.Responses; | ||
using System.Threading.Tasks; | ||
using Ocelot.Responses; | ||
|
||
namespace Ocelot.Configuration.Provider | ||
{ | ||
public interface IOcelotConfigurationProvider | ||
{ | ||
Response<IOcelotConfiguration> Get(); | ||
Task<Response<IOcelotConfiguration>> Get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
namespace Ocelot.Configuration | ||
{ | ||
public class RateLimitRule | ||
{ | ||
public RateLimitRule(string period, double periodTimespan, long limit) | ||
{ | ||
Period = period; | ||
PeriodTimespan = periodTimespan; | ||
Limit = limit; | ||
} | ||
|
||
/// <summary> | ||
/// Rate limit period as in 1s, 1m, 1h,1d | ||
/// </summary> | ||
public string Period { get; private set; } | ||
|
||
public double PeriodTimespan { get; private set; } | ||
/// <summary> | ||
/// Maximum number of requests that a client can make in a defined period | ||
/// </summary> | ||
public long Limit { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Consul; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Ocelot.Responses; | ||
using Ocelot.ServiceDiscovery; | ||
|
||
namespace Ocelot.Configuration.Repository | ||
{ | ||
public class ConsulOcelotConfigurationRepository : IOcelotConfigurationRepository | ||
{ | ||
private readonly ConsulClient _consul; | ||
private ConsulRegistryConfiguration _configuration; | ||
private string _ocelotConfiguration = "OcelotConfiguration"; | ||
private Cache.IOcelotCache<IOcelotConfiguration> _cache; | ||
|
||
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}"); | ||
}); | ||
} | ||
|
||
public async Task<Response<IOcelotConfiguration>> Get() | ||
{ | ||
var config = _cache.Get(_ocelotConfiguration); | ||
|
||
if (config != null) | ||
{ | ||
return new OkResponse<IOcelotConfiguration>(config); | ||
} | ||
|
||
var queryResult = await _consul.KV.Get(_ocelotConfiguration); | ||
|
||
if (queryResult.Response == null) | ||
{ | ||
return new OkResponse<IOcelotConfiguration>(null); | ||
} | ||
|
||
var bytes = queryResult.Response.Value; | ||
|
||
var json = Encoding.UTF8.GetString(bytes); | ||
|
||
var consulConfig = JsonConvert.DeserializeObject<OcelotConfiguration>(json); | ||
|
||
return new OkResponse<IOcelotConfiguration>(consulConfig); | ||
} | ||
|
||
public async Task<Response> AddOrReplace(IOcelotConfiguration ocelotConfiguration) | ||
{ | ||
var json = JsonConvert.SerializeObject(ocelotConfiguration); | ||
|
||
var bytes = Encoding.UTF8.GetBytes(json); | ||
|
||
var kvPair = new KVPair(_ocelotConfiguration) | ||
{ | ||
Value = bytes | ||
}; | ||
|
||
var result = await _consul.KV.Put(kvPair); | ||
|
||
if (result.Response) | ||
{ | ||
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3)); | ||
|
||
return new OkResponse(); | ||
} | ||
|
||
return new ErrorResponse(new UnableToSetConfigInConsulError("Unable to set config in consul")); | ||
} | ||
} | ||
} |
7 changes: 4 additions & 3 deletions
7
src/Ocelot/Configuration/Repository/IOcelotConfigurationRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
using Ocelot.Responses; | ||
using System.Threading.Tasks; | ||
using Ocelot.Responses; | ||
|
||
namespace Ocelot.Configuration.Repository | ||
{ | ||
public interface IOcelotConfigurationRepository | ||
{ | ||
Response<IOcelotConfiguration> Get(); | ||
Response AddOrReplace(IOcelotConfiguration ocelotConfiguration); | ||
Task<Response<IOcelotConfiguration>> Get(); | ||
Task<Response> AddOrReplace(IOcelotConfiguration ocelotConfiguration); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Ocelot/Configuration/Repository/UnableToSetConfigInConsulError.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Ocelot.Errors; | ||
|
||
namespace Ocelot.Configuration.Repository | ||
{ | ||
public class UnableToSetConfigInConsulError : Error | ||
{ | ||
public UnableToSetConfigInConsulError(string message) | ||
: base(message, OcelotErrorCode.UnableToSetConfigInConsulError) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.