Why doesn't AddConfigStoredInConsul() work as normal Consul config? #1762
-
Expected Behavior / New FeatureThe same configuration is used. using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json");
builder.Services
.AddOcelot()
.AddConsul()
.AddConfigStoredInConsul();
var app = builder.Build();
app.UseOcelot();
app.Run(); ocelot.json It is shown below {
"Routes": [
{
"UpstreamPathTemplate": "/Ocelot/{url}",
"UpstreamHttpMethod": [
"Get",
"Post"
],
"DownstreamPathTemplate": "/{url}",
"DownstreamScheme": "http",
"ServiceName": "MagicCore.API",
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
}
],
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 15 comments 21 replies
-
Hello @ArchitectAllenGitHub could you provide more details? Thanks! |
Beta Was this translation helpful? Give feedback.
-
@ggnaegi Look at "ServiceDiscoveryProvider" options in global config! They have no a Type property defined! |
Beta Was this translation helpful? Give feedback.
-
Second, our docs for Load Balancer says nothing about coupling Service Discovery vs Load Balancer. The author was confused. We don't explain correct instructions... |
Beta Was this translation helpful? Give feedback.
-
What's the strange method? |
Beta Was this translation helpful? Give feedback.
-
@raman-m it's for the consul key value store. You could have dynamic configuration, the ocelot config being stored in consul. It starts an IHostedService instance called FileConfigurationPoller. public static IOcelotBuilder AddConsul(this IOcelotBuilder builder)
{
builder.Services
.AddSingleton(ConsulProviderFactory.Get)
.AddSingleton<IConsulClientFactory, ConsulClientFactory>()
.RemoveAll(typeof(IFileConfigurationPollerOptions))
.AddSingleton<IFileConfigurationPollerOptions, ConsulFileConfigurationPollerOption>();
return builder;
}
public static IOcelotBuilder AddConfigStoredInConsul(this IOcelotBuilder builder)
{
builder.Services
.AddSingleton(ConsulMiddlewareConfigurationProvider.Get)
.AddHostedService<FileConfigurationPoller>()
.AddSingleton<IFileConfigurationRepository, ConsulFileConfigurationRepository>();
return builder;
} |
Beta Was this translation helpful? Give feedback.
-
@ggnaegi @raman-m |
Beta Was this translation helpful? Give feedback.
-
@ArchitectAllenGitHub could you try a config like this one? like @raman-m mentioned we might have a problem with the configuration since the ServiceDiscoveryProvider type is not defined.
Ok, I'm using a docker environment, so Host would be localhost or 127.0.0.1 for you ;-) |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Yeah, but wait a minute, do you have registered your services to consul? Otherwise it's useless... The idea here is the following, you register your services to consul and then you query consul for the service addresses. I have an example here: https://github.com/ggnaegi/SwizlyPeasy.Gateway/blob/master/SwizlyPeasy.Consul/README.md But, @ArchitectAllenGitHub you are right, there is something not consistent here... |
Beta Was this translation helpful? Give feedback.
-
The service is registered, Ocelot can be forwarded normally without adding the AddConfigStoredInConsul() method. Add this method and it's 404, which is very strange |
Beta Was this translation helpful? Give feedback.
-
@ArchitectAllenGitHub Show us what have you registered in Consul Services? And, make screenshots of response & HTTP status code of direct connection from Ocelot's machine in Postman tool to your downstream service What is your Ocelot's machine? How did you deploy the machine? How do you host Ocelot app? Is it deployed to Docker image/container or not? |
Beta Was this translation helpful? Give feedback.
-
@ArchitectAllenGitHub It's very strange, and even more for me since i'm using this in production... 😆 |
Beta Was this translation helpful? Give feedback.
-
I'm running on Windows. |
Beta Was this translation helpful? Give feedback.
-
@ArchitectAllenGitHub I will try to reproduce your config and come back to you. .. |
Beta Was this translation helpful? Give feedback.
-
@ArchitectAllenGitHub {
"DownstreamPathTemplate": "/test/v{version}/all-labels-and-options",
"UpstreamPathTemplate": "/test/v{version}/all-labels-and-options",
"UpstreamHttpMethod": [
"GET"
],
"DownstreamHttpMethod": null,
"AddHeadersToRequest": {},
"UpstreamHeaderTransform": {},
"DownstreamHeaderTransform": {},
"AddClaimsToRequest": {},
"RouteClaimsRequirement": {},
"AddQueriesToRequest": {},
"ChangeDownstreamPathTemplate": {},
"RequestIdKey": null,
"FileCacheOptions": {
"TtlSeconds": 0,
"Region": null
},
"RouteIsCaseSensitive": false,
"ServiceName": "SuperTestService",
"ServiceNamespace": null,
"DownstreamScheme": "http",
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 0,
"DurationOfBreak": 0,
"TimeoutValue": 0
},
"LoadBalancerOptions": {
"Type": "RoundRobin",
"Key": null,
"Expiry": 0
},
"RateLimitOptions": {
"ClientWhitelist": [],
"EnableRateLimiting": true,
"Period": "1s",
"PeriodTimespan": 1.0,
"Limit": 5
},
"AuthenticationOptions": {
"AuthenticationProviderKey": null,
"AllowedScopes": []
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false,
"UseTracing": false,
"UseProxy": true,
"MaxConnectionsPerServer": 2147483647
},
"DownstreamHostAndPorts": [],
"UpstreamHost": null,
"Key": null,
"DelegatingHandlers": [],
"Priority": 1,
"Timeout": 0,
"DangerousAcceptAnyServerCertificateValidator": false,
"SecurityOptions": {
"IPAllowedList": [],
"IPBlockedList": [],
"ExcludeAllowedFromBlocked": false
},
"DownstreamHttpVersion": null
},
...
"DynamicRoutes": [],
"Aggregates": [],
"GlobalConfiguration": {
"RequestIdKey": null,
"ServiceDiscoveryProvider": {
"Scheme": null,
"Host": "consul",
"Port": 8500,
"Type": "PollConsul",
"Token": null,
"ConfigurationKey": null,
"PollingInterval": 3600000,
"Namespace": null
},
"RateLimitOptions": {
"ClientIdHeader": "ClientId",
"QuotaExceededMessage": null,
"RateLimitCounterPrefix": "ocelot",
"DisableRateLimitHeaders": false,
"HttpStatusCode": 429
},
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 0,
"DurationOfBreak": 0,
"TimeoutValue": 0
},
"BaseUrl": null,
"LoadBalancerOptions": {
"Type": null,
"Key": null,
"Expiry": 0
},
"DownstreamScheme": null,
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false,
"UseTracing": false,
"UseProxy": true,
"MaxConnectionsPerServer": 2147483647
},
"DownstreamHttpVersion": null
} |
Beta Was this translation helpful? Give feedback.
@ArchitectAllenGitHub
So, I was a bit lazy, I haven't used your code, but I tried a similar configuration as yours and it's working fine.
Environment: Windows, Ocelot 20.0.0, RoundRobin load balancer, 2 instances of service SuperTestService.
That's an excerpt of the ocelot file stored in consul KV-Store: