mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 05:48:16 +08:00
Make Ocelot work with service fabric DNS and naming service for guest exe and stateless (#242)
* test for issue * added service fabric sample * working!! * changed sample naming to Ocelot * removed files we dont need * removed files we dont need * updated sample gitignore * updated sample gitignore * getting ocelot to work with service fabric using the reverse proxy * #238 - added support for service fabric discovery provider, proxies requests through naming service, wont work on partioned service fabric services yet * #238 - Manually tested service fabric using sample..all seems OK. Made some changes after testing, added docs * #238 - added docs for servic fabric
This commit is contained in:
@ -36,7 +36,6 @@ namespace Ocelot.Configuration.Builder
|
||||
private readonly List<DownstreamHostAndPort> _downstreamAddresses;
|
||||
private string _upstreamHost;
|
||||
private string _key;
|
||||
|
||||
public DownstreamReRouteBuilder()
|
||||
{
|
||||
_downstreamAddresses = new List<DownstreamHostAndPort>();
|
||||
|
@ -4,6 +4,7 @@ namespace Ocelot.Configuration.Builder
|
||||
{
|
||||
private string _serviceDiscoveryProviderHost;
|
||||
private int _serviceDiscoveryProviderPort;
|
||||
private string _type;
|
||||
|
||||
public ServiceProviderConfigurationBuilder WithServiceDiscoveryProviderHost(string serviceDiscoveryProviderHost)
|
||||
{
|
||||
@ -17,9 +18,15 @@ namespace Ocelot.Configuration.Builder
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServiceProviderConfigurationBuilder WithServiceDiscoveryProviderType(string type)
|
||||
{
|
||||
_type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServiceProviderConfiguration Build()
|
||||
{
|
||||
return new ServiceProviderConfiguration(_serviceDiscoveryProviderHost,_serviceDiscoveryProviderPort);
|
||||
return new ServiceProviderConfiguration(_type, _serviceDiscoveryProviderHost, _serviceDiscoveryProviderPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,12 +7,14 @@ namespace Ocelot.Configuration.Creator
|
||||
{
|
||||
public ServiceProviderConfiguration Create(FileGlobalConfiguration globalConfiguration)
|
||||
{
|
||||
//todo log or return error here dont just default to something that wont work..
|
||||
var serviceProviderPort = globalConfiguration?.ServiceDiscoveryProvider?.Port ?? 0;
|
||||
|
||||
return new ServiceProviderConfigurationBuilder()
|
||||
.WithServiceDiscoveryProviderHost(globalConfiguration?.ServiceDiscoveryProvider?.Host)
|
||||
.WithServiceDiscoveryProviderPort(serviceProviderPort)
|
||||
.Build();
|
||||
.WithServiceDiscoveryProviderHost(globalConfiguration?.ServiceDiscoveryProvider?.Host)
|
||||
.WithServiceDiscoveryProviderPort(serviceProviderPort)
|
||||
.WithServiceDiscoveryProviderType(globalConfiguration?.ServiceDiscoveryProvider?.Type)
|
||||
.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,5 +4,6 @@ namespace Ocelot.Configuration.File
|
||||
{
|
||||
public string Host {get;set;}
|
||||
public int Port { get; set; }
|
||||
public string Type { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,8 @@ namespace Ocelot.Configuration.Repository
|
||||
|
||||
public ConsulFileConfigurationRepository(Cache.IOcelotCache<FileConfiguration> cache, ServiceProviderConfiguration serviceProviderConfig)
|
||||
{
|
||||
var consulHost = string.IsNullOrEmpty(serviceProviderConfig?.ServiceProviderHost) ? "localhost" : serviceProviderConfig?.ServiceProviderHost;
|
||||
var consulPort = serviceProviderConfig?.ServiceProviderPort ?? 8500;
|
||||
var consulHost = string.IsNullOrEmpty(serviceProviderConfig?.Host) ? "localhost" : serviceProviderConfig?.Host;
|
||||
var consulPort = serviceProviderConfig?.Port ?? 8500;
|
||||
var configuration = new ConsulRegistryConfiguration(consulHost, consulPort, _ocelotConfiguration);
|
||||
_cache = cache;
|
||||
_consul = new ConsulClient(c =>
|
||||
@ -76,4 +76,4 @@ namespace Ocelot.Configuration.Repository
|
||||
return new ErrorResponse(new UnableToSetConfigInConsulError($"Unable to set FileConfiguration in consul, response status code from consul was {result.StatusCode}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,13 +2,15 @@
|
||||
{
|
||||
public class ServiceProviderConfiguration
|
||||
{
|
||||
public ServiceProviderConfiguration(string serviceProviderHost, int serviceProviderPort)
|
||||
public ServiceProviderConfiguration(string type, string host, int port)
|
||||
{
|
||||
ServiceProviderHost = serviceProviderHost;
|
||||
ServiceProviderPort = serviceProviderPort;
|
||||
Host = host;
|
||||
Port = port;
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public string ServiceProviderHost { get; private set; }
|
||||
public int ServiceProviderPort { get; private set; }
|
||||
public string Host { get; private set; }
|
||||
public int Port { get; private set; }
|
||||
public string Type { get; private set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ namespace Ocelot.Configuration.Validator
|
||||
|
||||
RuleForEach(configuration => configuration.Aggregates)
|
||||
.Must((config, aggregateReRoute) => AllReRoutesForAggregateExist(aggregateReRoute, config.ReRoutes))
|
||||
.WithMessage((config, aggregateReRoute) => $"ReRoutes for {nameof(aggregateReRoute)} {aggregateReRoute.UpstreamPathTemplate} either do not exist or do not have correct Key property");
|
||||
.WithMessage((config, aggregateReRoute) => $"ReRoutes for {nameof(aggregateReRoute)} {aggregateReRoute.UpstreamPathTemplate} either do not exist or do not have correct ServiceName property");
|
||||
|
||||
RuleForEach(configuration => configuration.Aggregates)
|
||||
.Must((config, aggregateReRoute) => DoesNotContainReRoutesWithSpecificRequestIdKeys(aggregateReRoute, config.ReRoutes))
|
||||
|
@ -99,7 +99,6 @@ namespace Ocelot.DependencyInjection
|
||||
_services.TryAddSingleton<ILoadBalancerFactory, LoadBalancerFactory>();
|
||||
_services.TryAddSingleton<ILoadBalancerHouse, LoadBalancerHouse>();
|
||||
_services.TryAddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
_services.TryAddSingleton<IUrlBuilder, UrlBuilder>();
|
||||
_services.TryAddSingleton<IRemoveOutputHeaders, RemoveOutputHeaders>();
|
||||
_services.TryAddSingleton<IOcelotConfigurationProvider, OcelotConfigurationProvider>();
|
||||
_services.TryAddSingleton<IClaimToThingConfigurationParser, ClaimToThingConfigurationParser>();
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Ocelot.Responses;
|
||||
/*
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
|
||||
namespace Ocelot.DownstreamUrlCreator
|
||||
@ -8,3 +9,4 @@ namespace Ocelot.DownstreamUrlCreator
|
||||
Response<DownstreamUrl> Build(string downstreamPath, string downstreamScheme, ServiceHostAndPort downstreamHostAndPort);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
@ -5,6 +5,7 @@ using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
|
||||
namespace Ocelot.DownstreamUrlCreator.Middleware
|
||||
@ -14,16 +15,13 @@ namespace Ocelot.DownstreamUrlCreator.Middleware
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IDownstreamPathPlaceholderReplacer _replacer;
|
||||
private readonly IOcelotLogger _logger;
|
||||
private readonly IUrlBuilder _urlBuilder;
|
||||
|
||||
public DownstreamUrlCreatorMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IDownstreamPathPlaceholderReplacer replacer,
|
||||
IUrlBuilder urlBuilder)
|
||||
IDownstreamPathPlaceholderReplacer replacer)
|
||||
{
|
||||
_next = next;
|
||||
_replacer = replacer;
|
||||
_urlBuilder = urlBuilder;
|
||||
_logger = loggerFactory.CreateLogger<DownstreamUrlCreatorMiddleware>();
|
||||
}
|
||||
|
||||
@ -40,11 +38,32 @@ namespace Ocelot.DownstreamUrlCreator.Middleware
|
||||
return;
|
||||
}
|
||||
|
||||
var uriBuilder = new UriBuilder(context.DownstreamRequest.RequestUri)
|
||||
UriBuilder uriBuilder;
|
||||
|
||||
//todo - feel this is a bit crap the way we build the url dont see why we need this builder thing..maybe i blew my own brains out
|
||||
// when i originally wrote it..
|
||||
if (context.ServiceProviderConfiguration.Type == "ServiceFabric" && context.DownstreamReRoute.UseServiceDiscovery)
|
||||
{
|
||||
Path = dsPath.Data.Value,
|
||||
Scheme = context.DownstreamReRoute.DownstreamScheme
|
||||
};
|
||||
_logger.LogInformation("DownstreamUrlCreatorMiddleware - going to try set service fabric path");
|
||||
|
||||
var scheme = context.DownstreamReRoute.DownstreamScheme;
|
||||
var host = context.DownstreamRequest.RequestUri.Host;
|
||||
var port = context.DownstreamRequest.RequestUri.Port;
|
||||
var serviceFabricPath = $"/{context.DownstreamReRoute.ServiceName + dsPath.Data.Value}";
|
||||
|
||||
_logger.LogInformation("DownstreamUrlCreatorMiddleware - service fabric path is {proxyUrl}", serviceFabricPath);
|
||||
|
||||
var uri = new Uri($"{scheme}://{host}:{port}{serviceFabricPath}?cmd=instance");
|
||||
uriBuilder = new UriBuilder(uri);
|
||||
}
|
||||
else
|
||||
{
|
||||
uriBuilder = new UriBuilder(context.DownstreamRequest.RequestUri)
|
||||
{
|
||||
Path = dsPath.Data.Value,
|
||||
Scheme = context.DownstreamReRoute.DownstreamScheme
|
||||
};
|
||||
}
|
||||
|
||||
context.DownstreamRequest.RequestUri = uriBuilder.Uri;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
/*
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
@ -25,12 +26,11 @@ namespace Ocelot.DownstreamUrlCreator
|
||||
return new ErrorResponse<DownstreamUrl>(new List<Error> { new DownstreamHostNullOrEmptyError() });
|
||||
}
|
||||
|
||||
|
||||
var builder = new UriBuilder
|
||||
{
|
||||
Host = downstreamHostAndPort.DownstreamHost,
|
||||
Path = downstreamPath,
|
||||
Scheme = downstreamScheme
|
||||
Scheme = downstreamScheme,
|
||||
};
|
||||
|
||||
if (downstreamHostAndPort.DownstreamPort > 0)
|
||||
@ -44,3 +44,4 @@ namespace Ocelot.DownstreamUrlCreator
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
@ -17,6 +17,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
||||
|
||||
public async Task<Response<ServiceHostAndPort>> Lease()
|
||||
{
|
||||
//todo no point spinning a task up here, also first or default could be null..
|
||||
var service = await Task.FromResult(_services.FirstOrDefault());
|
||||
return new OkResponse<ServiceHostAndPort>(service.HostAndPort);
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ namespace Ocelot.ServiceDiscovery
|
||||
{
|
||||
if (reRoute.UseServiceDiscovery)
|
||||
{
|
||||
return GetServiceDiscoveryProvider(reRoute.ServiceName, serviceConfig.ServiceProviderHost, serviceConfig.ServiceProviderPort);
|
||||
return GetServiceDiscoveryProvider(serviceConfig, reRoute.ServiceName);
|
||||
}
|
||||
|
||||
var services = new List<Service>();
|
||||
@ -33,9 +33,15 @@ namespace Ocelot.ServiceDiscovery
|
||||
return new ConfigurationServiceProvider(services);
|
||||
}
|
||||
|
||||
private IServiceDiscoveryProvider GetServiceDiscoveryProvider(string keyOfServiceInConsul, string providerHostName, int providerPort)
|
||||
private IServiceDiscoveryProvider GetServiceDiscoveryProvider(ServiceProviderConfiguration serviceConfig, string serviceName)
|
||||
{
|
||||
var consulRegistryConfiguration = new ConsulRegistryConfiguration(providerHostName, providerPort, keyOfServiceInConsul);
|
||||
if (serviceConfig.Type == "ServiceFabric")
|
||||
{
|
||||
var config = new ServiceFabricConfiguration(serviceConfig.Host, serviceConfig.Port, serviceName);
|
||||
return new ServiceFabricServiceDiscoveryProvider(config);
|
||||
}
|
||||
|
||||
var consulRegistryConfiguration = new ConsulRegistryConfiguration(serviceConfig.Host, serviceConfig.Port, serviceName);
|
||||
return new ConsulServiceDiscoveryProvider(consulRegistryConfiguration, _factory);
|
||||
}
|
||||
}
|
||||
|
16
src/Ocelot/ServiceDiscovery/ServiceFabricConfiguration.cs
Normal file
16
src/Ocelot/ServiceDiscovery/ServiceFabricConfiguration.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace Ocelot.ServiceDiscovery
|
||||
{
|
||||
public class ServiceFabricConfiguration
|
||||
{
|
||||
public ServiceFabricConfiguration(string hostName, int port, string serviceName)
|
||||
{
|
||||
HostName = hostName;
|
||||
Port = port;
|
||||
ServiceName = serviceName;
|
||||
}
|
||||
|
||||
public string ServiceName { get; private set; }
|
||||
public string HostName { get; private set; }
|
||||
public int Port { get; private set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Values;
|
||||
|
||||
namespace Ocelot.ServiceDiscovery
|
||||
{
|
||||
public class ServiceFabricServiceDiscoveryProvider : IServiceDiscoveryProvider
|
||||
{
|
||||
private readonly ServiceFabricConfiguration _configuration;
|
||||
|
||||
public ServiceFabricServiceDiscoveryProvider(ServiceFabricConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task<List<Service>> Get()
|
||||
{
|
||||
return new List<Service>
|
||||
{
|
||||
new Service(_configuration.ServiceName,
|
||||
new ServiceHostAndPort(_configuration.HostName, _configuration.Port),
|
||||
"doesnt matter with service fabric",
|
||||
"doesnt matter with service fabric",
|
||||
new List<string>())
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user