Ocelot manipulates the HttpRequest object into a state specified by its configuration until it reaches a request builder middleware where it creates a HttpRequestMessage object which is used to make a request to a downstream service. Then, when deploying to Docker, there will be four API-Gateway containers created from that same Docker image, as shown in the following extract from the docker-compose.yml file.Additionally, and as you can see in the docker-compose.override.yml file, the only difference between those API Gateway containers is the Ocelot configuration file which is different for each service container and specified at runtime through a Docker volume, as shown in the following docker-compose.override.yml file.By splitting the API Gateway into multiple API Gateways, different development teams focusing on different subsets of microservices can manage their own API Gateways by using independent Ocelot configuration files while re-using the same Ocelot Docker image.Now, if you run eShopOnContainers with the API Gateways (included by default in VS when opening eShopOnContainers-ServicesAndWebApps.sln solution or if running “docker-compose up”), the following sample routes will be performed.Because of testing or debugging reasons, if you wanted to directly access to the Catalog Docker container (only at the development environment) without passing through the API Gateway, since “catalog.api” is a DNS resolution internal to the Docker host (service discovery handled by docker-compose service names), the only way to directly access the container is through the external port published in the docker-compose.override.yml, which is provided only for development tests, such as But the application is configured so it accesses all the microservices through the API Gateways, not though the direct port “shortcuts”.As introduced previously, a very flexible way to implement requests aggregation is with custom services, by code.
That fact can be an important risk because your API Gateway service will be growing and evolving based on many different requirements from the client apps. Ocelot manipulates the HttpRequest object into a state specified by its configuration until it reaches a request builder middleware where it creates a HttpRequestMessage object which is used to make a request to a downstream service. When request reaches Ocelot middleware it inspect and manipulate request object per need, you can also write your own custom middleware {More details in next article} to modify per your project need.I will build working application to showcase Ocelot capabilities such as We will be using ASP.net core 3.1 for this project. Eventually, it will be bloated because of those different needs and effectively it could be pretty similar to a monolithic application or monolithic service. It’s useful if you don’t want to manage lots of ReRoute specific settings.The main functionality of an Ocelot API Gateway is to take incoming http requests and forward them on to a downstream service, currently as another http request. When using containers, the port specified at its dockerfile.The Host will be a service name that will depend on the service name resolution you are using. Ocelot’s describes the routing of one request to another as a ReRoute.For instance, let’s focus on one of the ReRoutes in the configuration.json from above, the configuration for the Basket microservice.The DownstreamPathTemplate, Scheme and DownstreamHostAndPorts make the internal microservice URL that this request will be forwarded to.The port is the internal port used by the service. Open Visual Studio 2019 and create a new blank solution .Usually we create separate solution for Microservices but for sake of simplicity I am keeping all services in same solution . Open OcelotConfiguration.json and update following codeChange port number per your machine/project configuration*UpstreamPath -> Exposed endpoint on GW side and where client will send requestBefore you test out make sure you have set multiple startup project in VS.Here request is being redirected to CatalogAPI.Services and we are getting response accordingly .With this we are done with foundational base setup for ocelot and we make more changes and make it generic in upcoming articles .Sharing trade secrets learned over time . The middleware that makes the request is the last thing in the Ocelot pipeline. We discuss this topic in the section Therefore, for many medium- and large-size applications, using a custom-built API Gateway product is usually a good approach, but not as a single monolithic aggregator or unique central custom API Gateway unless that API Gateway allows multiple independent configuration areas for the multiple development teams with autonomous microservices.As an example, eShopOnContainers has around 6 internal microservice-types that have to be published through the API Gateways, as shown in the following image.In the case of the Identity service, in our design we left it out of the API Gateway routing, but with Ocelot it is also possible to include it as part of the re-routing lists.All those services are currently implemented as ASP.NET Core Web API services, as you can tell because of the code.