.Net Core 3.1 Microservice Api Gateway Ocelot Library
First of all, I created the information that I will share here, using more than one article and document.
I will start my first article by trying to explain a small application, thinking that information increases as it is shared.
I started directly from the project without going into much detail about the general features of the Ocelot library. :)
First, I created 3 separate web API projects.
I have created a few sample lists by creating a ProductController to make certain changes to my ProductApi project.
I added my port address in Program.cs for the program to be upset.
Finally, in the properties option of my ProductApi project, I set the App URL as http://localhost:7002 / from the Debug settings.
I repeated the same process in CustomerApi. However, since my goal is to access projects on different ports via the ApiGateway that I will create, I definitely gave the port address differently.
I created a CustomerController and added 2 customers for the example.
I added the url settings to program.cs.
Finally, I set the App URL as http://localhost:7001/ from the Debug settings in the properties option for my CustomerApi project. And I ran my projects.
Now I came to my ApiGateway project which is the most important part. After installing the ocelot library that I will use in my ApiGateway project, I created my ocelot.json file.
I added the host information by adding the ocelot.json file that I will use in Program.cs.
Api Gateway project’s port as http: //localhost:7000/
After setting it up, I added the necessary services for Ocelot to ConfigureServices and Configure in startup.cs.
Since I opened all 3 projects together, I ran all the projects in Visual Studio with the <Multiple Startup Project> option.
After the necessary adjustments, I reached the result by making http://localhost: 7000/api/gateway/product request via Postman.
For CustomerApi, http://localhost: 7000/api /gateway/customer request will be made.
The purpose here is to make all requests over localhost:7000, so I make all requests from here.
I hope it will be useful to you as well as to me.