ThreeMammals / Ocelot

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

Ocelot Issue while host in docker #1662

Closed Shonzon closed 1 year ago

Shonzon commented 1 year ago

After Hosted in Docker Ocelot did not invoke microservices properly : (getting error) Ocelot.Responder.Middleware.ResponderMiddleware[0] requestId: 0HMR0TMTEM44N:00000003, previousRequestId: no previous request id, message: Error Code: ConnectionToDownstreamServiceError Message: Error connecting to downstream service, exception: System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (marketservice:7216) ---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. Ocelot File : { "Routes": [ { "DownstreamPathTemplate": "/api/Authenticate/{everything}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7217 } ], "UpstreamPathTemplate": "/api/Authenticate/{everything}", "UpstreamHttpMethod": [ "POST", "GET" ] }, { "DownstreamPathTemplate": "/api/AmountModels/{everything}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7217 } ], "UpstreamPathTemplate": "/api/AmountModels/{everything}", "UpstreamHttpMethod": [ "POST", "GET" ] }, ], "GlobalConfiguration": { "BaseUrl": "http://apigateway:7215/" }, "LoadBalancerOptions": { "Type": "LeastConnection" } }

Docker Compose file : version: '3.4'

services: marketservice: image: ${DOCKER_REGISTRY-}marketservice build: context: . dockerfile: MarketServices\Dockerfile args:

raman-m commented 1 year ago

Dear @Shonzon,

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (marketservice:7216)

By other words, gateway cannot connect to the service behind the docker container ingress. You must place all ASP.NET projects of the services including the gateway app into configs of Docker Compose project in Visual Studio! So, you need to create Docker network for Docker engine to register all services in this network to be visible for all services of the Docker Compose project. Otherwise all network communication between services being deployed into Docker container will fail. Or you should consider deployment to public cloud services like Amazon Lambda or Azure Functions.


System.Net.Sockets.SocketException (10060):

I do not recommend to use ws-protocol for versions 17-19. You can try to route ws-traffic by Ocelot v15-16 for .NET 3.1. Current v19 (and 18, 17, etc.) doesn't support wss-protocol at all!

raman-m commented 1 year ago

Please, use the issue template while creating an issue via pressing the New Issue button! Examples of bugs: #1636, #1660, #1590. Examples of features: #1637, #1400

Because you create a bug, you need to:

Also, you need to give details on environment you use, aka Specifications paragraph! After providing all this info we will decide what to do with this issue.

P.S. Start using markdown code blocks with correct language to provide nice and easy to read configs in description.

raman-m commented 1 year ago

@Shonzon

Have you resolved your Docker issue or haven't?

raman-m commented 1 year ago

Have you find some workaround for the issue? Did you understand a root cause?

I believe your issue is related to Docker but not to Ocelot. You need to show the root cause of Ocelot to accept the issue for fixing.

Let's find some workaround within one week please! Your issue will be accepted or declined until June 23.

abdulwahid211 commented 1 year ago

I'm still having the same issues running this in docker, what's the latest update?

abdulwahid211 commented 1 year ago

Solved!

@Shonzon @raman-m

First step Make sure you remove these methods from program.cs

app.UseHttpsRedirection();
app.UseAuthorization();

Second step On your Ocelot json file, set all your routes localhost to your microservice names (docker-compose file) for example below

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/Authenticate/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "authenticationservice",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/api/Authenticate/{everything}",
      "UpstreamHttpMethod": [ "POST", "GET" ]
    }
  ]
}
raman-m commented 1 year ago

@abdulwahid211 Hi Abdul! You are right! The downstream hosts are wrong in ocelot routes when using docker-compose project. They must be the same hosts as service names in docker-compose config. I guess this is the root cause of the issue.

Also, I doubt docker-compose config. We need not specify networks config section. It should be implicit.

It is always hard to find the root cause when the people copies and pastes logs, configs with bad formatting. I ask people to upload the solution to GitHub to verify.

Anyway, thanks for your eagle eye! 🦅 You've found the root cause! ✌️

raman-m commented 1 year ago

Hi @Shonzon ! Our Ocelot community has provided you the solution! It seems we've found the root cause for your user case.

I'm going to close the issue... If you find in future that something wrong with Ocelot, you could reopen this or open a new issue.

Good luck in your endeavour with Ocelot!