ThreeMammals / Ocelot

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

Getting load balancer error with latest version #2063

Closed vikasgupta1456 closed 2 months ago

vikasgupta1456 commented 2 months ago

I am trying to upgrade to latest version of ocelot (23.2.2). But when I am hitting one of my endpoint, I get an error. This works until 23.1.0. which means '23.2.0' also has the same issue. Can you let me know what changed ?

I have routing like this:

{ 
"Routes": [ {
 "ServiceName": "myapp",
"LoadBalancerOptions": { 
  "Type": "LeastConnection"
  },
"UpstreamPathTemplate": "/document/read/{catchAll}",
"DownstreamPathTemplate": "/{catchAll}",
"DownstreamScheme": "https",
"Priority": 1
} ] }

And getting this error :

Error Code: UnableToFindLoadBalancerError
Message: Unable to find load balancer for \u0027/document/read/{catchAll}|no-host|no-host-and-port|no-svc-ns|myapp|LeastConnection|no-lb-key\u0027.
Exception: System.NullReferenceException: Object reference not set to an instance of an object.
  at Ocelot.ServiceDiscovery.ServiceDiscoveryProviderFactory.GetServiceDiscoveryProvider(ServiceProviderConfiguration config, DownstreamRoute route)
  at Ocelot.ServiceDiscovery.ServiceDiscoveryProviderFactory.Get(ServiceProviderConfiguration serviceConfig, DownstreamRoute route)
  at Ocelot.LoadBalancer.LoadBalancers.LoadBalancerFactory.Get(DownstreamRoute route, ServiceProviderConfiguration config)
  at Ocelot.LoadBalancer.LoadBalancers.LoadBalancerHouse.GetResponse(DownstreamRoute route, ServiceProviderConfiguration config)
  at Ocelot.LoadBalancer.LoadBalancers.LoadBalancerHouse.Get(DownstreamRoute route, ServiceProviderConfiguration config);
errors found in ResponderMiddleware. Setting error response for request path:/document/read/12345, request method: GET\u0027"
ggnaegi commented 2 months ago

Thanks @vikasgupta1456 for your feedback, let us check that and come back to you.

raman-m commented 2 months ago

@vikasgupta1456 Could you please share the entire JSON content of your ocelot.json file? It would be helpful to see the GlobalConfiguration section as well. However, it's best to copy and paste the entire file content here.


An error in the method at Ocelot.LoadBalancer.LoadBalancers.LoadBalancerHouse.Get(DownstreamRoute route, ServiceProviderConfiguration config); suggests there may be an issue with the ServiceProviderConfiguration config argument. It's possible that the ServiceProviderConfiguration.Type is not recognized. Additionally, the definition of "ServiceName": "myapp" within the route indicates that the application setup utilizes service discovery.

raman-m commented 2 months ago

@vikasgupta1456

I am trying to upgrade to latest version of ocelot (23.2.2). But when I am hitting one of my endpoint, I get an error. This works until 23.1.0. which means '23.2.0' also has the same issue. Can you let me know what changed ?

The response and potential solutions depend on the type of service discovery provided. In the Hornussen release, v23.1.0, the Load Balancer was updated 👇

However, it seems your issue may be related to a Global Configuration problem. It's worth considering if PR #1944 might have introduced a bug.

vikasgupta1456 commented 2 months ago

This is my ocelot.json file

{
  "Routes": [
    { 
  "ChangeDownstreamPathTemplate": {},
  "DelegatingHandlers": [],
  "DownstreamHeaderTransform": {},
  "DownstreamHostAndPorts": [],
  "DownstreamHttpMethod": null,
  "DownstreamHttpVersion": null,
  "DownstreamPathTemplate": "/{catchAll}",
  "DownstreamScheme": "https",
  "FileCacheOptions": {
    "TtlSeconds": 0,
    "Region": "",
    "Header": null
  },
  "HttpHandlerOptions": {
    "AllowAutoRedirect": false,
    "MaxConnectionsPerServer": 2127483647,
    "UseCookieContainer": false,
    "UseProxy": true,
    "UseTracing": false,
    "PooledConnectionLifetimeSeconds": null
  },
  "Key": null,
  "LoadBalancerOptions": {
    "Expiry": 2127483647,
    "Key": "",
    "Type": "LeastConnection"
  },
  "Priority": 1,
  "QoSOptions": {
    "DurationOfBreak": 1,
    "ExceptionsAllowedBeforeBreaking": 0,
    "TimeoutValue": 0
  },
  "RateLimitOptions": {
    "ClientWhitelist": [],
    "EnableRateLimiting": false,
    "Period": null,
    "PeriodTimespan": 0.0,
    "Limit": 0
  },
  "RequestIdKey": null,
  "RouteClaimsRequirement": {},
  "RouteIsCaseSensitive": false,
  "SecurityOptions": {
    "IPAllowedList": [],
    "IPBlockedList": [],
    "ExcludeAllowedFromBlocked": false
  },
  "ServiceName": "myapp",
  "ServiceNamespace": null,
  "Timeout": 0,
  "UpstreamHeaderTransform": {},
  "UpstreamHost": null,
  "UpstreamHttpMethod": [],
  "UpstreamPathTemplate": "/document/read/{catchAll}"
}
],
"GlobalConfiguration": {
  "RequestIdKey": null,
  "ServiceDiscoveryProvider": {
    "Scheme": null,
    "Host": null,
    "Port": 0,
    "Type": null,
    "Token": null,
    "ConfigurationKey": null,
    "PollingInterval": 0,
    "Namespace": null
  },
  "RateLimitOptions": {
    "ClientIdHeader": "ClientId",
    "QuotaExceededMessage": null,
    "RateLimitCounterPrefix": "ocelot",
    "DisableRateLimitHeaders": false,
    "HttpStatusCode": 429
  },
  "QoSOptions": {
    "DurationOfBreak": 1,
    "ExceptionsAllowedBeforeBreaking": 0,
    "TimeoutValue": 0
  },
  "BaseUrl": null,
  "LoadBalancerOptions": {
    "Expiry": 2127483647,
    "Key": "",
    "Type": ""
  },
  "DownstreamScheme": null,
  "HttpHandlerOptions": {
    "AllowAutoRedirect": false,
    "MaxConnectionsPerServer": 2127483647,
    "UseCookieContainer": false,
    "UseProxy": true,
    "UseTracing": false,
    "PooledConnectionLifetimeSeconds": null
  },
  "DownstreamHttpVersion": null
}
raman-m commented 2 months ago

@vikasgupta1456 Did you read my recommendations above?

raman-m commented 2 months ago
{
  "Routes": [
    { 
  "DownstreamPathTemplate": "/{catchAll}",
  "DownstreamScheme": "https",
  "Key": null,
  "LoadBalancerOptions": {
    "Type": "LeastConnection"
  },
  "ServiceName": "myapp", // This property requires to setup service discovery options in GlobalConfiguration
  "ServiceNamespace": null,
  "UpstreamPathTemplate": "/document/read/{catchAll}"
}
],
"GlobalConfiguration": {
  "ServiceDiscoveryProvider": {
    "Scheme": null,
    "Host": null,
    "Port": 0,
    "Type": null, // Developer must provide the type of service discovery provider: Consul, Kube, Eureka etc.!
    "Token": null,
    "ConfigurationKey": null,
    "PollingInterval": 0,
    "Namespace": null
  },
}

Read Service Discovery docs! Good luck in development!