0

We have developed two ASP.NET CORE (5.0) microservices and deployed them inside docker http://localhost:28621/stock http://localhost:62362/user

Both are easily accessible from browser.

We have used ocelot gateway for API gateway. When we deployed it inside docker we got following error

warn: Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware[0] requestId: 0HM8658EF6KHC:00000002, previousRequestId: no previous request id, message: DownstreamRouteFinderMiddleware setting pipeline errors. IDownstreamRouteFinder returned Error Code: UnableToFindDownstreamRouteError Message: Failed to match Route configuration for upstream path: /user, verb: GET. warn: Ocelot.Responder.Middleware.ResponderMiddleware[0] requestId: 0HM8658EF6KHC:00000002, previousRequestId: no previous request id, message: Error Code: UnableToFindDownstreamRouteError Message: Failed to match Route configuration for upstream path: /user, verb: GET. errors found in ResponderMiddleware. Setting error response for request path:/user, request method: GET -------------------------------------------------------------------------------------------------

ocelot.Development.json is as follows

 {
  "Routes": [
    {
      "DownstreamPathTemplate": "/user",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 62362
        }
      ],
      "UpstreamPathTemplate": "/user",
      "UpstreamHttpMethod": [ "Get" ]
    },
    {
      "DownstreamPathTemplate": "/stock",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 28621
        }
      ],
      "UpstreamPathTemplate": "/stock",
      "UpstreamHttpMethod": [ "Get" ]
    }

  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5021"
  }
}

startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOcelot();

    }

    public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
        await app.UseOcelot();
    }
}

program.cs

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
              Host.CreateDefaultBuilder(args)
                   .ConfigureAppConfiguration((hostingContext, config) =>
                   {
                       config.AddJsonFile($"ocelot.                                          
 {hostingContext.HostingEnvironment.EnvironmentName}.json", true, true);
                   })
                  .ConfigureWebHostDefaults(webBuilder =>
                  {
                      webBuilder.UseStartup<Startup>();
                  });
    }

Please guide us where we are doing mistake we are using .net core 5.0

Thanks

3 Answers 3

1

Can you please change Routes to ReRoutes. I fixed myself with that. Maybe you forgot to add json file in the program.cs. Here is the code,

 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) 
.ConfigureAppConfiguration((host, config) => 
{
config.AddJsonFile("Ocelot.json", false, true); 
})
.ConfigureWebHostDefaults(webBuilder => 
{ 
webBuilder .UseStartup<Startup>(); 
}); 
Sign up to request clarification or add additional context in comments.

3 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Please edit to add that additional info to the answer itself.
I tried i hope it works for you ! :)
0

Your Ocelot container cannot access the other containers' services with "DownstreamHostAndPorts" settings: {'Host': 'localhost'}. Container's 'localhost' is not the same as the host machine's 'localhost'!

To solve this issue look in this direction:

  • Create a Docker network
  • Run all three containers under the same network with --network-alias flag
  • In the Ocelot json file use the network alias names and default ports (since the containers can talk to eachother directly, there is no need to map external ports!)

Comments

0

Just advise. (I guess you run ok locally without docker - mean paths are correct - upstream downstream)

  1. Why you use Development running in docker ? Use Production.json
    Normally Dev you run on host without docker using localhost. (Naming doesn't really matter)

  2. When running in docker replace in ocelot json Host - set container name (

    http https is not valid in Host - it's not a place for url
    
  3. Routes is Ok (Servicename is used (makes sense) only with any service discovery like Consul etc) If use ReRoutes with latest Ocelot versions you'll get that Downstream Upstream error.

  4. BaseUrl should use host.docker.internal, not localhost when running in docker (I actually doesn't think you need BaseUrl - all you settings are inside routes)

  5. Check that you run in one network (all containers) and containers names match name in config json

  6. Install in docker container tab Exec something like curl to check if you see other containers/
    apt-get update

apt-get install -y curl

curl http://servicename:port../api/.... - just call your other service - should get response

use just ping
apt-get install -y iputils-ping

docker exec servicename1 ping servicename2 -c2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.