This is a simple project that aims to show how to build an API Gateway.
In a very simplified way, an API Gateway has two main functions:
- Reverse proxy
- Aggregate information from different services
To build this scenario we are using Ocelot which is a complete API Gateway but we will not use its aggregator function, we will have another project responsible for aggregating information from different services through gRPC calls.
Basically we have 4 endpoints:
- List all products
- Adds a new product to a basket
- Removes a product from a basket
- Search all products in a basket
In a production environment, the API Gateway works as a single entrypoint, so all these 4 endpoints will pass through it, for each endpoint we have a rule:
- On endpoint 1 we will forward to the Product service which will return the list of all products
- On endpoint 2 we will forward to the aggregation service, which will make a gRPC call to the Product service to ensure that the product exists and then will make a call to the Basket service to add this product in the basket
- On endpoint 3 we will foward to the Basket service to remove the product from a basket
- On endpoint 4 we will forward aggregation service, which will make a gPRC call to the Basket service to get the items in that basket and then to the Product service to get information such as Name, Description and Price
It is necessary to start all services when running the project, for this you must:
- Click on the solution with the right mouse button and select option
Configure Startup Projects...
- Select the
Multiple startup projects
and set theAction
of the four projects toStart
With this configuration, when running the solution, four services will be started:
- Ocelot => https://localhost:7097/swagger
- Aggregator => https://localhost:7067/swagger
- Product API => https://localhost:7087/swagger
- Basket API => https://localhost:7298/swagger
At first, only one browser will open with Ocelot's Swagger and through it you will be able to access all the other services, but if you wanted, you can also access each of the services separately through the addresses that were listed above.