ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.38k stars 1.64k forks source link

Ocelot doens't work with Consul #213

Closed DiegoTondim closed 6 years ago

DiegoTondim commented 6 years ago

Hello,

ocelot configuration,json:

{ "ReRoutes": [ { "DownstreamPathTemplate": "/api/home", "UpstreamPathTemplate": "/home", "UpstreamHttpMethod": [ "Get", "Options" ], "ServiceName": "web", "LoadBalancer": "LeastConnection" } ], "GlobalConfiguration": { "RequestIdKey": "OcRequestId", "ServiceDiscoveryProvider": { "Provider": "Consul", "Host": "localhost", "Port": 8500 } } }

consul k/v: {"ReRoutes":[{"DownstreamPathTemplate":"/api/home","UpstreamPathTemplate":"/home","UpstreamHttpMethod":["Get","Options"],"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0,"Region":null},"ReRouteIsCaseSensitive":false,"ServiceName":"web","DownstreamScheme":null,"DownstreamHost":null,"DownstreamPort":0,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":"LeastConnection","RateLimitOptions":{"ClientWhitelist":[],"EnableRateLimiting":false,"Period":null,"PeriodTimespan":0.0,"Limit":0},"AuthenticationOptions":{"AuthenticationProviderKey":null,"AllowedScopes":[]},"HttpHandlerOptions":{"AllowAutoRedirect":true,"UseCookieContainer":true},"UseServiceDiscovery":false}],"GlobalConfiguration":{"RequestIdKey":"OcRequestId","ServiceDiscoveryProvider":{"Host":"localhost","Port":8500},"AdministrationPath":null,"RateLimitOptions":{"ClientIdHeader":"ClientId","QuotaExceededMessage":null,"RateLimitCounterPrefix":"ocelot","DisableRateLimitHeaders":false,"HttpStatusCode":429}}}

http://localhost:8500/v1/health/service/web "Service": { "ID": "web_90_0_2_224_8080", "Service": "web", "Tags": [ "version-v1" ], "Address": "localhost", "Port": 8080, "EnableTagOverride": false, "CreateIndex": 20, "ModifyIndex": 20 },

startup: var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder(); builder.SetBasePath(env.ContentRootPath) //add configuration.json
.AddJsonFile("configuration.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables();

        Configuration = builder.Build();

Action settings = (x) => { x.WithMicrosoftLogging(log => { log.AddConsole((Microsoft.Extensions.Logging.LogLevel) LogLevel.Debug);

            }).WithDictionaryHandle();
        };
        services
            .AddOcelot(Configuration)
            .AddStoreOcelotConfigurationInConsul()
            .AddCacheManager(settings);

When I access the url localhost:9000/home I have 404 error. What am I doing wrong?

TomPallister commented 6 years ago

@DiegoTondim Thank you for your interest in the project.

I can't see anything wrong at the moment :( I will try and replicate this later when I have time.

Can you tell me what version of Ocelot you are using? Also are you able to turn logging on and set the level to Trace and see if you get an errors logged?

TomPallister commented 6 years ago

@DiegoTondim I noticed a problem in configuration.json you need

"UseServiceDiscovery": true

This is to tell Ocelot that the ReRoute should use consul.

DiegoTondim commented 6 years ago

Like this @TomPallister ?

{ "ReRoutes": [ { "DownstreamPathTemplate": "/api/home", "UpstreamPathTemplate": "/home", "UpstreamHttpMethod": [ "Get", "Options" ], "ServiceName": "web", "LoadBalancer": "LeastConnection", "UseServiceDiscovery": true } ], "GlobalConfiguration": { "RequestIdKey": "OcRequestId", "ServiceDiscoveryProvider": { "Provider": "Consul", "Host": "localhost", "Port": 8500 } } }

TomPallister commented 6 years ago

@DiegoTondim yes that should work!

DiegoTondim commented 6 years ago

@TomPallister Ok... I updated to latest stable version of ocelot (.11), but doens't work.

consul k/v updated: {"ReRoutes":[{"DownstreamPathTemplate":"/api/home","UpstreamPathTemplate":"/home","UpstreamHttpMethod":["Get","Options"],"AddHeadersToRequest":{},"UpstreamHeaderTransform":{},"DownstreamHeaderTransform":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0,"Region":null},"ReRouteIsCaseSensitive":false,"ServiceName":"web","DownstreamScheme":null,"DownstreamHost":null,"DownstreamPort":0,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":"LeastConnection","RateLimitOptions":{"ClientWhitelist":[],"EnableRateLimiting":false,"Period":null,"PeriodTimespan":0.0,"Limit":0},"AuthenticationOptions":{"AuthenticationProviderKey":null,"AllowedScopes":[]},"HttpHandlerOptions":{"AllowAutoRedirect":true,"UseCookieContainer":true},"UseServiceDiscovery":true}],"GlobalConfiguration":{"RequestIdKey":"OcRequestId","ServiceDiscoveryProvider":{"Host":"localhost","Port":8500},"RateLimitOptions":{"ClientIdHeader":"ClientId","QuotaExceededMessage":null,"RateLimitCounterPrefix":"ocelot","DisableRateLimitHeaders":false,"HttpStatusCode":429}}}

configuration.json: { "ReRoutes": [ { "DownstreamPathTemplate": "/api/home", "UpstreamPathTemplate": "/home", "UpstreamHttpMethod": [ "Get", "Options" ], "ServiceName": "web", "LoadBalancer": "LeastConnection", "UseServiceDiscovery": true } ], "GlobalConfiguration": { "RequestIdKey": "OcRequestId", "ServiceDiscoveryProvider": { "Provider": "Consul", "Host": "localhost", "Port": 8500 } } } Could have a problem with port in service address? (localhost:8080/api/home)

DiegoTondim commented 6 years ago

@TomPallister I'm running ocelot application on IIS Express, can be a problem?

TomPallister commented 6 years ago

@DiegoTondim this should work :( have you tried turning on logging to see if you get any errors?

If this doesnt work the only thing I can suggest is debug Ocelot code in your application :(

DiegoTondim commented 6 years ago

@TomPallister

Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.RemoteDependency","time":"2018-01-31T19:56:22.0462310Z","tags":{"ai.application.ver":"1.0.0.0","ai.internal.sdkVersion":"rdddsc:2.4.1-1362","ai.operation.id":"9d48a414-46a961288728a4f4"},"data":{"baseType":"RemoteDependencyData","baseData":{"ver":2,"name":"GET /v1/kv/OcelotConfiguration","id":"|9d48a414-46a961288728a4f4.","data":"http://localhost:8500/v1/kv/OcelotConfiguration","duration":"00:00:00.0022007","resultCode":"200","success":true,"type":"Http","target":"localhost","properties":{"AspNetCoreEnvironment":"Development","DeveloperMode":"true"}}}} Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-01-31T19:56:22.8487923Z","tags":{"ai.operation.name":"GET /home","ai.location.ip":"::1","ai.internal.nodeName":"NT-02986","ai.application.ver":"1.0.0.0","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.operation.parentId":"|9d48a415-46a961288728a4f4.","ai.operation.id":"9d48a415-46a961288728a4f4","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Request starting HTTP/1.1 GET http://localhost:50399/home","severityLevel":"Information","properties":{"Scheme":"http","AspNetCoreEnvironment":"Development","Protocol":"HTTP/1.1","Path":"/home","DeveloperMode":"true","Host":"localhost:50399","CategoryName":"Microsoft.AspNetCore.Hosting.Internal.WebHost","Method":"GET"}}}} Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-01-31T19:56:22.8535141Z","tags":{"ai.operation.name":"GET /home","ai.location.ip":"::1","ai.internal.nodeName":"NT-02986","ai.application.ver":"1.0.0.0","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.operation.parentId":"|9d48a415-46a961288728a4f4.","ai.operation.id":"9d48a415-46a961288728a4f4","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Request finished in 4.6576ms 404","severityLevel":"Information","properties":{"AspNetCoreEnvironment":"Development","ElapsedMilliseconds":"4,6576","DeveloperMode":"true","CategoryName":"Microsoft.AspNetCore.Hosting.Internal.WebHost","StatusCode":"404"}}}} Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2018-01-31T19:56:22.8487103Z","tags":{"ai.operation.name":"GET /home","ai.location.ip":"::1","ai.internal.nodeName":"NT-02986","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.application.ver":"1.0.0.0","ai.operation.id":"9d48a415-46a961288728a4f4","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"|9d48a415-46a961288728a4f4.","name":"GET /home","duration":"00:00:00.0129932","success":false,"responseCode":"404","url":"http://localhost:50399/home","properties":{"AspNetCoreEnvironment":"Development","DeveloperMode":"true","httpMethod":"GET"}}}} Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.RemoteDependency","time":"2018-01-31T19:56:23.0585717Z","tags":{"ai.application.ver":"1.0.0.0","ai.internal.sdkVersion":"rdddsc:2.4.1-1362","ai.operation.id":"9d48a416-46a961288728a4f4"},"data":{"baseType":"RemoteDependencyData","baseData":{"ver":2,"name":"GET /v1/kv/OcelotConfiguration","id":"|9d48a416-46a961288728a4f4.","data":"http://localhost:8500/v1/kv/OcelotConfiguration","duration":"00:00:00.0109335","resultCode":"200","success":true,"type":"Http","target":"localhost","properties":{"AspNetCoreEnvironment":"Development","DeveloperMode":"true"}}}}

TomPallister commented 6 years ago

@DiegoTondim I need to see the logs from Ocelot! Unfortunately these dont help.

If you set your appsettings.json to contain.

{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Trace", "System": "Error", "Microsoft": "Error" } } }

and then add logging to the application that hosts Ocelot you can see how to do that here https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x

Ocelot will hook into the asp.net core logging services that you register.

TomPallister commented 6 years ago

@DiegoTondim this might help set up the logging https://github.com/TomPallister/Ocelot/tree/develop/test/Ocelot.ManualTest it is the sample project i use for manually testing ocelot

DiegoTondim commented 6 years ago

@TomPallister I hope thats it help to find the error :(

The thread 0x3cdc has exited with code 0 (0x0). Ocelot.Logging.OcelotDiagnosticListener:Trace: requestId: no request id, previousRequestId: no previous request id, message: MiddlewareStarting: EndOfPipeline; /api/home, Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-01-31T22:50:06.6842307Z","tags":{"ai.operation.name":"GET /api/home","ai.application.ver":"1.0.0.0","ai.operation.id":"a7511d68-4bd28c737e3592b2","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.operation.parentId":"|a7511d68-4bd28c737e3592b2.","ai.internal.nodeName":"NT-02986","ai.location.ip":"::1","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"requestId: no request id, previousRequestId: no previous request id, message: MiddlewareStarting: EndOfPipeline; /api/home,","severityLevel":"Verbose","properties":{"message":"MiddlewareStarting: EndOfPipeline; /api/home","AspNetCoreEnvironment":"Development","previousRequestId":"no previous request id","{OriginalFormat}":"requestId: {requestId}, previousRequestId: {previousRequestId}, message: {message},","CategoryName":"Ocelot.Logging.OcelotDiagnosticListener","requestId":"no request id","DeveloperMode":"true"}}}} Ocelot.Logging.OcelotDiagnosticListener:Trace: requestId: no request id, previousRequestId: no previous request id, message: MiddlewareFinished: EndOfPipeline; 404, Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-01-31T22:50:06.7211320Z","tags":{"ai.operation.name":"GET /api/home","ai.application.ver":"1.0.0.0","ai.operation.id":"a7511d68-4bd28c737e3592b2","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.operation.parentId":"|a7511d68-4bd28c737e3592b2.","ai.internal.nodeName":"NT-02986","ai.location.ip":"::1","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"requestId: no request id, previousRequestId: no previous request id, message: MiddlewareFinished: EndOfPipeline; 404,","severityLevel":"Verbose","properties":{"message":"MiddlewareFinished: EndOfPipeline; 404","AspNetCoreEnvironment":"Development","previousRequestId":"no previous request id","{OriginalFormat}":"requestId: {requestId}, previousRequestId: {previousRequestId}, message: {message},","CategoryName":"Ocelot.Logging.OcelotDiagnosticListener","requestId":"no request id","DeveloperMode":"true"}}}} Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2018-01-31T22:50:06.6344510Z","tags":{"ai.operation.name":"GET /api/home","ai.application.ver":"1.0.0.0","ai.operation.id":"a7511d68-4bd28c737e3592b2","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.internal.nodeName":"NT-02986","ai.location.ip":"::1","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"|a7511d68-4bd28c737e3592b2.","name":"GET /api/home","duration":"00:00:00.0952389","success":false,"responseCode":"404","url":"http://localhost:9000/api/home","properties":{"AspNetCoreEnvironment":"Development","httpMethod":"GET","DeveloperMode":"true"}}}} The thread 0x2bc4 has exited with code 0 (0x0). Ocelot.Logging.OcelotDiagnosticListener:Trace: requestId: no request id, previousRequestId: no previous request id, message: MiddlewareStarting: EndOfPipeline; /api/home, Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-01-31T22:52:34.5945069Z","tags":{"ai.operation.name":"GET /api/home","ai.application.ver":"1.0.0.0","ai.operation.id":"a7511d69-4bd28c737e3592b2","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.operation.parentId":"|a7511d69-4bd28c737e3592b2.","ai.internal.nodeName":"NT-02986","ai.location.ip":"::1","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"requestId: no request id, previousRequestId: no previous request id, message: MiddlewareStarting: EndOfPipeline; /api/home,","severityLevel":"Verbose","properties":{"message":"MiddlewareStarting: EndOfPipeline; /api/home","AspNetCoreEnvironment":"Development","previousRequestId":"no previous request id","{OriginalFormat}":"requestId: {requestId}, previousRequestId: {previousRequestId}, message: {message},","CategoryName":"Ocelot.Logging.OcelotDiagnosticListener","requestId":"no request id","DeveloperMode":"true"}}}} Ocelot.Logging.OcelotDiagnosticListener:Trace: requestId: no request id, previousRequestId: no previous request id, message: MiddlewareFinished: EndOfPipeline; 404, Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-01-31T22:52:34.6084909Z","tags":{"ai.operation.name":"GET /api/home","ai.application.ver":"1.0.0.0","ai.operation.id":"a7511d69-4bd28c737e3592b2","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.operation.parentId":"|a7511d69-4bd28c737e3592b2.","ai.internal.nodeName":"NT-02986","ai.location.ip":"::1","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"requestId: no request id, previousRequestId: no previous request id, message: MiddlewareFinished: EndOfPipeline; 404,","severityLevel":"Verbose","properties":{"message":"MiddlewareFinished: EndOfPipeline; 404","AspNetCoreEnvironment":"Development","previousRequestId":"no previous request id","{OriginalFormat}":"requestId: {requestId}, previousRequestId: {previousRequestId}, message: {message},","CategoryName":"Ocelot.Logging.OcelotDiagnosticListener","requestId":"no request id","DeveloperMode":"true"}}}} Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2018-01-31T22:52:34.5687756Z","tags":{"ai.operation.name":"GET /api/home","ai.application.ver":"1.0.0.0","ai.operation.id":"a7511d69-4bd28c737e3592b2","ai.internal.sdkVersion":"aspnet5c:2.1.1","ai.internal.nodeName":"NT-02986","ai.location.ip":"::1","ai.cloud.roleInstance":"NT-02986"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"|a7511d69-4bd28c737e3592b2.","name":"GET /api/home","duration":"00:00:00.0503039","success":false,"responseCode":"404","url":"http://localhost:9000/api/home","properties":{"AspNetCoreEnvironment":"Development","httpMethod":"GET","DeveloperMode":"true"}}}}

TomPallister commented 6 years ago

@DiegoTondim sorry I cannot see anything from those logs. It looks like you might be using a version of Ocelot that had broken logging? What version are you using? Try Install-Package Ocelot -Version 2.0.11 if you are not using this version already.

TomPallister commented 6 years ago

@DiegoTondim also i have written a test that shows you how everything should be set up here https://github.com/TomPallister/Ocelot/blob/develop/test/Ocelot.AcceptanceTests/ServiceDiscoveryTests.cs

The test is called should_handle_request_to_consul_for_downstream_service_and_make_request

That might help!

DiegoTondim commented 6 years ago

Hello @TomPallister ,

I found the error, I configured the startup of application with example of asp net core version 1.0, but I'm using the asp net core 2.0. So, I changed the startup class and all works.

Thank you so much for attention.

TomPallister commented 6 years ago

@DiegoTondim awesome good stuff :)