ThreeMammals / Ocelot

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

Ocelot.Provider.Kubernetes is not work #2223

Open tuannh6495 opened 2 days ago

tuannh6495 commented 2 days ago

Both Ocelot API and Price API are running on K8s There are my source code for Ocelot

packages

<PackageReference Include="Ocelot" />
<PackageReference Include="Ocelot.Provider.Kubernetes" />

Program.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Kubernetes;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration
    .AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();
builder.Services.AddOcelot()
    .AddKubernetes();

var app = builder.Build();
app.UseOcelot().Wait();
app.Run();

ocelot.json

{
  "Routes": [
    {
      "Servicename": "price-webapi",
      "DownstreamPathTemplate": "/api/price/{everything}",
      "UpstreamPathTemplate": "/api/price/{everything}",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
      "UseServiceDiscovery": true,
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    }
  ],
  "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Type": "Kube",
      "Namespace": "cafef-dev1"
    }
  }
}

When I use url from ocelot to route to price-api, I got this error:

[05:33:59 WRN] requestId: 0HN8FDP0KCCDL:00000001, previousRequestId: No PreviousRequestId, message: 'Ocelot Retry strategy for the operation of 'System.Func`1[System.Threading.Tasks.Task`1[KubeClient.Models.EndpointsV1]]' type -> Retry No 1: The predicate has identified erroneous state in the returned result. For further details, implement logging of the result's value or properties within the predicate method.'
[05:33:59 WRN] requestId: 0HN8FDP0KCCDL:00000001, previousRequestId: No PreviousRequestId, message: 'Ocelot Retry strategy for the operation of 'System.Func`1[System.Threading.Tasks.Task`1[KubeClient.Models.EndpointsV1]]' type -> Retry No 2: The predicate has identified erroneous state in the returned result. For further details, implement logging of the result's value or properties within the predicate method.'
[05:33:59 WRN] requestId: 0HN8FDP0KCCDL:00000001, previousRequestId: No PreviousRequestId, message: 'Kube provider. Namespace:cafef-dev1, Service:price-webapi; Unable to use bad result returned by Kube integration endpoint because the final result is invalid/unknown after multiple retries!'
[05:33:59 WRN] requestId: 0HN8FDP0KCCDL:00000001, previousRequestId: No PreviousRequestId, message: 'Error Code: ServicesAreEmptyError Message: There were no services in RoundRobin for 'price-webapi' during LeaseAsync operation! errors found in ResponderMiddleware. Setting error response for request path:/api/price/v1/values, request method: GET'
[05:33:59 INF] Request finished HTTP/1.1 GET http://10.5.45.161:31901/api/price/v1/values - 404 0 null 2147.6126ms

I checked in ocelot api's pod with:

curl http://price-webapi.cafef-dev1.svc.cluster.local:9100/api/price/v1/values

and got success result.

Please help me fix this issue! Thanks a lot!

raman-m commented 1 day ago

Hello, my dear friend! God will help you!

raman-m commented 1 day ago

What's your full name? What are the links to your social networks?

tuannh6495 commented 1 day ago

Hi @raman-m ! If you have any solution, please give it on here!

raman-m commented 23 hours ago

Hi @raman-m ! If you have any solution, please give it on here!

God will help you! 🙏