ThreeMammals / Ocelot

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

Service discovery and "PollConsul" type: ServicesAreEmptyError #1634

Closed Danimatar0 closed 12 months ago

Danimatar0 commented 1 year ago

I was trying to access a microservice called UserManagement from an ocelot api gateway but when attempting the request, i got the following error in logs: error-consul and here is my ocelot.json file :

{
  "Routes": [
    {
      "UseServiceDiscovery": true,
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "ServiceName": "UserManagement",
      "UpstreamPathTemplate": "/gateway/{url}",
      "UpstreamHttpMethod": [ "Get", "POST" ],
      "ReRoutesCaseSensitive": false,
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    }
  ],
  "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "PollConsul", // !!!
      "PollingInterval": 100
    }
  }
}

And the service was successfully registered because I can clearly see it's in consul UI dashboard in services list. I appreciate any help because it's an urgent matter! P.S: The following steps were produced without Docker, just a normal web API.

ggnaegi commented 1 year ago

Hello please check the following issue. https://github.com/ThreeMammals/Ocelot/issues/1329

Try "Consul" instead of "PollConsul"

raman-m commented 1 year ago

@Danimatar0

I appreciate any help because it's an urgent matter !

Is it still urgent? :rofl:

raman-m commented 1 year ago

@ggnaegi Hi Guillaume! It seems we have typo issue: Search for "PollConsul" in docs

If @Danimatar0 has used this configuration sample:

"ServiceDiscoveryProvider": {
    "Host": "localhost",
    "Port": 8500,
    "Type": "PollConsul", // WRONG !!!
    "PollingInterval": 100
}

But it is wrong!

I believe it should be:

"ServiceDiscoveryProvider": {
    "Host": "localhost",
    "Port": 8500,
    "Type": "Consul", // CORRECT !!!
    "PollingInterval": 100
}

What do you think?

ggnaegi commented 1 year ago

@raman-m Hi Raman When using "Type": "Consul", then the service address is retrieved from consul for every request. When using "Type":"PollConsul", the gateway should poll Consul for service addresses, but there is imho a problem: Wenn calling the endpoint the first time, you will get the error mentioned by @Danimatar0 until the interval is reached, then you will get some answers from your endpoint... But imagine if your PollingInterval is high. Maybe the "PollConsul" variant has been abandoned. I came with another solution: https://github.com/ggnaegi/Ocelot/blob/develop/src/Ocelot.Provider.Consul/PollingConsulServiceDiscoveryProvider.cs I could create a PR if you want, it's stable, used on a productive system for two years now.

In my case, short interval polling was too chatty, there are some issues there if your consul.json file is big with several routes. The references to the consul services are not stored as singletons. So, as soon, as you are querying new routes, then the gateway will try to retrieve the service addresses... For one address, ok, but then, it's the explosion...

raman-m commented 1 year ago

I could create a PR if you want, it's stable, used on a productive system for two years now.

Wow! That will be awesome! :heart_decoration: You can attach this issue to your new created PR, please! Pay attention before a PR creation you need to merge all develop branch top commits to your forked develop branch as it is feature branch now! And your feature branch has a lot of merge conflicts. I've created the PR 1 to update your feature branch:

It seems it will not be easy to resolve all these merge conflicts... The goal is to have all your changes on the top commit of ThreeMammals:develop. The simplest strategy:

OR

I can add you to Ocelot Core team :wink: and you will have ability to create feature branches inside of Ocelot repo directly! That will solve all stupid annoying life hacks with forked repo management! Would you like to join Ocelot Core team? :wink:


In my case, short interval polling was too chatty, there are some issues there if your consul.json file is big with several routes. The references to the consul services are not stored as singletons. So, as soon, as you are querying new routes, then the gateway will try to retrieve the service addresses... For one address, ok, but then, it's the explosion...

Oh, no! :cry: I see some critical performance issue here... :disappointed: Can you confirm that somehow plz? May be some sample project... Do you have some workarounds for this bug? Have you worked on this bug?

ggnaegi commented 1 year ago

Yeah, sure, I would love to be part of the team :-)

I don't have much time until wednesday, but I will prepare something next week.

As for the performance issues, they should be addressed in my forked repo.

raman-m commented 1 year ago

Yeah, sure, I would love to be part of the team :-)

Sorry, I've double checked, and I cannot add you to the team because I have no rights to manage the repo. I've already asked Tom. But he is silent for now.

Guillaume, You need to decide what forked repository will you use to create future PR: your repo or my one? I have forked Ocelot to have top commits always for linear git history. Just let me know please!

I advise you to start by resolving merge conflicts first if you are only going to use your repo.

Waiting a good news from you about your contribution!

ggnaegi commented 1 year ago

Hello @raman-m I will delete my forked repo, refork the current official repo, import the changes to the solution, make sure the tests are passing and then I will create a PR, probably tomorrow in the afternoon or on wednesday morning.

ggnaegi commented 1 year ago

By the way, the PR should address the following issues: https://github.com/ThreeMammals/Ocelot/issues/1304 https://github.com/ThreeMammals/Ocelot/issues/1329 https://github.com/ThreeMammals/Ocelot/issues/1487 Some PR are related: https://github.com/ThreeMammals/Ocelot/pull/1572 https://github.com/ThreeMammals/Ocelot/pull/1562

raman-m commented 1 year ago

@ggnaegi

Some PR are related: https://github.com/ThreeMammals/Ocelot/pull/1572 https://github.com/ThreeMammals/Ocelot/pull/1562

Well... Did you reuse the ideas from these PRs? 😜

raman-m commented 1 year ago

@Danimatar0 Could you verify the solution of linked PR #1670 please? Let me know your testing results please!

raman-m commented 12 months ago

@Danimatar0 @ggnaegi Congrats! Done!