Microservices .NET Core Ocelot

Creating a Microservices Skeleton Project in .NET Core

21-06-2024

//

3 min read

Recently, I delved into implementing microservices and developed a basic proof-of-concept project. You can find the source code here.

In this article, I'll outline the steps to create this project.

Step 1: Setting Up the Solution

Start by creating a .NET Core solution with two Web API projects:

  • Customer API (Port 8002): Responds with "Hello There!" to GET requests.
  • Orders API (Port 8003): Similarly, returns a greeting message.

These APIs serve as our foundational microservices endpoints. Below is a visual of the project structure in Visual Studio:

Image

These APIs are straightforward, responding with string messages to GET requests. Here's a snippet from the Customer API:

Image

For example, navigating to the Customer API on port 8002 yields the message "Hello There!":

Image

Similarly, the Orders API on port 8003 displays a similar greeting:

Image

Step 2: Dockerizing the Project

If Docker support was not added during project creation, integrate it now and include the project in your Docker orchestration. Here's a screenshot showing the project structure with Docker support and orchestration:

Image

Step 3: Introducing API Gateway with Ocelot

Enhance the project by adding an API Gateway using the Ocelot package. Ensure Docker support and orchestration are also configured for the API Gateway. Below is a screenshot showing the project structure with the API Gateway project:

Image

Step 4: Reviewing Docker Compose and Ocelot Configuration

Review the Docker Compose file and Ocelot configuration (ocelot.json). Here's an example of the Docker Compose content:

Image

And here's a snippet from the ocelot.json configuration file:

Image

Few points to note:

  • The Docker Compose file creates a network named "backend" and maps all three projects to it.
  • Adjust the port mappings according to your environment, as default ports may vary.

With Ocelot API Gateway in place, we achieve efficient redirection:

  • http://localhost:8002/api/order redirects to http://localhost:8004/api/order
  • http://localhost:8004/api/customer redirects to http://localhost:8003/api/order

This setup simplifies management and enhances flexibility.

Image

Summary

This quick walkthrough of a microservices POC with .NET Core and Docker demonstrates the power of modular architecture. Explore the source code here and check out this insightful YouTube playlist for more on microservices basics.

© 2024 Hiren Patel.